简体   繁体   English

Spring无法连接bean为“Generic Dao没有特殊实现类”的情况

[英]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 . 我尝试为这个https://stackoverflow.com/a/511417实现Generic DAO的解决方案。 However for “Using this genericDAO without special implementation Class” case I get the NoSuchBeanDefinitionException. 但是对于“使用此genericDAO而没有特殊实现类”的情况,我得到了NoSuchBeanDefinitionException。

The full stacktrace http://pastebin.com/HwrjEZiX 完整的堆栈跟踪http://pastebin.com/HwrjEZiX

As I see Spring can't wiring bean for Generic Dao without special implementation class 正如我所见,Spring无法为没有特殊实现类的Generic Dao连接bean

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. https://dl.dropboxusercontent.com/u/8384811/Misc/2013-05-14_224944.jpg Spring使用JdkDynamicAopProxy连接BranchHibernateDao类,“将此genericDAO与特殊实现类一起使用”案例。

According JavaDoc it Creates a dynamic proxy, implementing the interfaces exposed by * the AopProxy. 根据JavaDoc,它创建一个动态代理,实现由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. 因此它看到了用于branchDao bean定义的BrunchDao和Crud接口的方法。

However it can't wire the branchGenericDao (“Using this genericDAO without special implementation Class” case) and don't see the Crud interface methods. 但它无法连接branchGenericDao(“使用此genericDAO而无需特殊实现类”的情况),并且看不到Crud接口方法。

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 测试源在这里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 Crud源在这里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 GenericDao来源在这里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 BranchHibernateDao源码在这里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. 当您使用Spring编写测试时,必须使用正确的Spring测试运行器。 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 . 另外,如果你想创建泛型daos并且你正在使用Hibernate,你应该真正研究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. 使用它将使你的生活比你正在尝试做的事情容易10倍,因为它会做你想做的事情,但工作量少,选择比你自己能够实现的要好。

(The answer you're following pre-dates Spring Data. I'm sure they would've used Spring Data if it existed.) (您在Spring Data之前的答案是肯定的。我相信如果它存在,他们会使用Spring Data。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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