Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

injecting repositories in implementation class without using @autowired in spring data using xml

I am new in spring-data i want to try use the spring data without using @autowired over repositories. I just want to direct inject the Repositories through xml for that i am not able to get the repositories instance in my implementation class from xml ,reason of using xml based configuration is this my previous service layer and controller does not support any annotation feature so i have to just manipulate the dao layer using spring data this is my xml configuration

      <?xml version="1.0" encoding="UTF-8"?>
        <beans xmlns="http://www.springframework.org/schema/beans"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
            xmlns:context="http://www.springframework.org/schema/context"
            xmlns:jpa="http://www.springframework.org/schema/data/jpa"
            xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-2.5.xsd">


            <context:component-scan base-package="com.nousinfo.tutorial" />
            <!-- Database -->
            <bean id="datasource"
                class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <property name="driverClassName" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://192.168.25.30:3306/employee" />
                <property name="username" value="***" />
                <property name="password" value="*****" />
            </bean>

            <!-- Entity Manager -->
            <bean id="entityManagerFactory"
                class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                <property name="dataSource" ref="datasource" />
                <property name="persistenceUnitName" value="EmployeeApp" />
            </bean>

            <!-- Transaction Manager -->
            <beanid="transactionManager"                                                                               class="org.springframework.orm.jpa.JpaTransactionManager">
                <property name="entityManagerFactory" ref="entityManagerFactory" />
            </bean>

    <!--- here i having problem on injecting the bean of employeeRepositories---->
            <bean id="employeeDaoImpl" class="com.nousinfo.tutorial.repository.impl.EmployeeDAOImpl">
                <property name="employeeRepository" ref="employeeRepository" />
            </bean>
   <bean id="employeeRepositories" class="com.nousinfo.tutorial.dao.EmployeeRepositories"/>


            <!-- Jpa Repositories -->
            <jpa:repositories base-package="com.nousinfo.tutorial.dao"></jpa:repositories>

  </beans>

this is my persistence.xml

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="EmployeeApp" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.nousinfo.tutorial.model.Department</class>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
        <property name="hibernate.hbm2ddl.auto" value="update" />
        <property name="hibernate.show_sql" value="true" />
    </properties>
</persistence-unit>

This is my Employee Repositories

public interface EmployeeRepositories extends JpaRepository<Employee, Long> {
    public List<Employee> findByFirstName(String name);

    @Query("FROM Employee emp WHERE emp.firstName = :firstname or emp.lastName = :lastname")
    List<Employee> getEmployeesByName(@Param("lastname") String lastname,
            @Param("firstname") String firstname);

    List<Employee> findByLastNameOrderByFirstNameAsc(String lastname);

    List<Employee> findByLastNameOrderByFirstNameDesc(String lastname);

    List<Employee> findByDepartmentId(String departmentId);

}

this is my implementation

public class EmployeeDAOImpl {

EmployeeRepositories employeeRepositories ;

public void setEmployeeRepositories (EmployeeRepositories employeeRepositories ) {
    this.employeeRepositories = employeeRepositories ;
}

public List<Employee> getAllEmployees() {
    return employeeRepositories.findAll();
}

this way i m calling the method for testing

 ApplicationContext applicationContext=new ClassPathXmlApplicationContext("MyBean.xml");
        EmployeeDAOImpl daOImpl=(EmployeeDAOImpl)applicationContext.getBean("employeeDaoImpl");
daOImpl.getAllEmployees();

Exception is coming because of wrong mapping so please provide me the correct mapping i will be be thankful to u

here is my exception

    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeDaoImpl' defined in class path resource [mybeans.xml]: Cannot resolve reference to bean 'employeeRepositories' while setting bean property 'employeeRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepositories' defined in class path resource [mybeans.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.nousinfo.tutorial.dao.EmployeeRepository]: Specified class is an interface
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1317)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1076)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:574)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.nousinfo.tutorial.common.basemodel.MainTest.main(MainTest.java:13)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeeRepositories' defined in class path resource [mybeans.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.nousinfo.tutorial.dao.EmployeeRepository]: Specified class is an interface
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:955)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:901)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    ... 15 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.nousinfo.tutorial.dao.EmployeeRepository]: Specified class is an interface
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:52)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:948)
    ... 23 more
like image 295
user1527637 Avatar asked Nov 03 '22 01:11

user1527637


1 Answers

Remove

<bean id="employeeRepositories" class="com.nousinfo.tutorial.repository.EmployeeRepositories"/>

and use:

        <bean id="employeeDaoImpl" class="com.nousinfo.tutorial.repository.impl.EmployeeDAOImpl">
            <property name="employeeDAO" ref="employeeRepositories " />
        </bean>
like image 163
Michail Nikolaev Avatar answered Nov 13 '22 10:11

Michail Nikolaev