Note that there are some explanatory texts on larger screens.

plurals
  1. POinjecting repositories in implementation class without using @autowired in spring data using xml
    primarykey
    data
    text
    <p>I am new in spring-data i want to try use the spring data without using <code>@autowired</code> 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</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;context:component-scan base-package="com.nousinfo.tutorial" /&gt; &lt;!-- Database --&gt; &lt;bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt; &lt;property name="url" value="jdbc:mysql://192.168.25.30:3306/employee" /&gt; &lt;property name="username" value="***" /&gt; &lt;property name="password" value="*****" /&gt; &lt;/bean&gt; &lt;!-- Entity Manager --&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="dataSource" ref="datasource" /&gt; &lt;property name="persistenceUnitName" value="EmployeeApp" /&gt; &lt;/bean&gt; &lt;!-- Transaction Manager --&gt; &lt;beanid="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt; &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt; &lt;/bean&gt; &lt;!--- here i having problem on injecting the bean of employeeRepositories----&gt; &lt;bean id="employeeDaoImpl" class="com.nousinfo.tutorial.repository.impl.EmployeeDAOImpl"&gt; &lt;property name="employeeRepository" ref="employeeRepository" /&gt; &lt;/bean&gt; &lt;bean id="employeeRepositories" class="com.nousinfo.tutorial.dao.EmployeeRepositories"/&gt; &lt;!-- Jpa Repositories --&gt; &lt;jpa:repositories base-package="com.nousinfo.tutorial.dao"&gt;&lt;/jpa:repositories&gt; &lt;/beans&gt; </code></pre> <p>this is my persistence.xml</p> <pre><code>&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"&gt; &lt;persistence-unit name="EmployeeApp" transaction-type="RESOURCE_LOCAL"&gt; &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt; &lt;class&gt;com.nousinfo.tutorial.model.Department&lt;/class&gt; &lt;properties&gt; &lt;property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /&gt; &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt; &lt;property name="hibernate.show_sql" value="true" /&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; </code></pre> <p></p> <p>This is my Employee Repositories</p> <pre><code>public interface EmployeeRepositories extends JpaRepository&lt;Employee, Long&gt; { public List&lt;Employee&gt; findByFirstName(String name); @Query("FROM Employee emp WHERE emp.firstName = :firstname or emp.lastName = :lastname") List&lt;Employee&gt; getEmployeesByName(@Param("lastname") String lastname, @Param("firstname") String firstname); List&lt;Employee&gt; findByLastNameOrderByFirstNameAsc(String lastname); List&lt;Employee&gt; findByLastNameOrderByFirstNameDesc(String lastname); List&lt;Employee&gt; findByDepartmentId(String departmentId); </code></pre> <p>}</p> <p>this is my implementation</p> <pre><code>public class EmployeeDAOImpl { EmployeeRepositories employeeRepositories ; public void setEmployeeRepositories (EmployeeRepositories employeeRepositories ) { this.employeeRepositories = employeeRepositories ; } public List&lt;Employee&gt; getAllEmployees() { return employeeRepositories.findAll(); } </code></pre> <p>this way i m calling the method for testing</p> <pre><code> ApplicationContext applicationContext=new ClassPathXmlApplicationContext("MyBean.xml"); EmployeeDAOImpl daOImpl=(EmployeeDAOImpl)applicationContext.getBean("employeeDaoImpl"); daOImpl.getAllEmployees(); </code></pre> <p>Exception is coming because of wrong mapping so please provide me the correct mapping i will be be thankful to u </p> <p>here is my exception </p> <pre><code> 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.&lt;init&gt;(ClassPathXmlApplicationContext.java:139) at org.springframework.context.support.ClassPathXmlApplicationContext.&lt;init&gt;(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 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload