简体   繁体   中英

Spring unable to wiring bean for “Generic Dao without special implementation class” case

I try to implement solution for Generic DAO like this https://stackoverflow.com/a/511417 . However for “Using this genericDAO without special implementation Class” case I get the NoSuchBeanDefinitionException.

The full stacktrace http://pastebin.com/HwrjEZiX

As I see Spring can't wiring bean for Generic Dao without special implementation class

https://dl.dropboxusercontent.com/u/8384811/Misc/2013-05-14_224944.jpg Spring uses the JdkDynamicAopProxy for wiring BranchHibernateDao class, “Using this genericDAO with special implementation Class” case.

According JavaDoc it Creates a dynamic proxy, implementing the interfaces exposed by * the AopProxy. Dynamic proxies cannot be used to proxy methods defined in classes, rather than interfaces.

So it sees the methods from BrunchDao and Crud interfaces for branchDao bean definition.

However it can't wire the branchGenericDao (“Using this genericDAO without special implementation Class” case) and don't see the Crud interface methods.

I'll appreciate for any help!

Bean's wiring

<bean id="branchDao" class="org.jtalks.poulpe.model.dao.hibernate.BranchHibernateDao" parent="genericDao"/>

<bean id="branchGenericDao" class="org.jtalks.common.model.dao.hibernate.GenericDao">
<qualifier value="branchGenericDao"/>
<constructor-arg name="sessionFactory" ref="sessionFactory"/>
<constructor-arg name="type" value="org.jtalks.poulpe.model.entity.PoulpeBranch"/>
</bean>

<bean id="genericDao" abstract="true" class="org.jtalks.common.model.dao.hibernate.GenericDao">
<constructor-arg name="sessionFactory" ref="sessionFactory"/>
</bean>

Test source is here https://github.com/jtalks-org/poulpe/blob/master.senleft/poulpe-model/src/test/java/org/jtalks/poulpe/model/dao/hibernate/BranchHibernateDaoTest.java

Crud source is here https://github.com/jtalks-org/jtalks-common/blob/master.senleft/jtalks-common-model/src/main/java/org/jtalks/common/model/dao/Crud.java

GenericDao source is here https://github.com/jtalks-org/jtalks-common/blob/master.senleft/jtalks-common-model/src/main/java/org/jtalks/common/model/dao/hibernate/GenericDao.java

BranchHibernateDao source is here https://github.com/jtalks-org/poulpe/blob/master.senleft/poulpe-model/src/main/java/org/jtalks/poulpe/model/dao/hibernate/BranchHibernateDao.java

The problem is probably with your test. When you write tests with Spring, you have to use the proper Spring test runner. Try modifying your test to work more like this , or try actually implementing it and not doing it with a testing framework to see if it works.

Also, if you want to create generic daos and you're using Hibernate, you should REALLY look into Spring Data JPA . Using it will make your life 10x easier than doing what you're trying to do, since it does what you're trying to do, but with less work and better options than you'll be able to implement by yourself.

(The answer you're following pre-dates Spring Data. I'm sure they would've used Spring Data if it existed.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM