Spring - Bean创建中的奇怪错误

时间:2009-09-23 21:38:42

标签: java spring dependency-injection java-ee-6 cglib

知道我为什么会遇到这个例外吗?

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myService' defined in class path resource [context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [$Proxy54 implementing org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,net.sf.cglib.proxy.Factory,org.springframework.beans.factory.InitializingBean] to required type [com.mycompany.service.dao.MyDAO] for property 'myDAO'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy54 implementing org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,net.sf.cglib.proxy.Factory,org.springframework.beans.factory.InitializingBean] to required type [com.mycompany.service.dao.MyDAO] for property 'myDAO': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:671)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:610)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:499)
    ... 36 more
Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [$Proxy54 implementing org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised,net.sf.cglib.proxy.Factory,org.springframework.beans.factory.InitializingBean] to required type [com.mycompany.service.dao.MyDAO] for property 'myDAO': no matching editors or conversion strategy found
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:231)
    at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:138)
    at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:386)
... 62 more

2 个答案:

答案 0 :(得分:18)

我怀疑如果ProdMiscDAO是一个接口(是吗?),你就不会有这个错误。我相信你可能有一个类使用cglib代理,执行魔术等等,最后它无法安全地转换为setter或构造函数中的参数。尝试编程到界面,看看错误是否消失。

更新ProdMiscDAO不是界面。这是一个扩展SqlMappedClientDaoSupport的类。

鉴于此,我建议尝试这样做:

  1. ProdMiscDAO重命名为SqlMappedProdMiscDAO
  2. 从名为SqlMappedProdMiscDAO的{​​{1}}提取界面(例如“ProdMiscDAO”)
  3. 浏览使用class SqlMappedProdMiscDAO implements ProdMiscDAO的所有代码,并将其更改为使用SqlMappedProdMiscDAO
  4. 配置spring以实例化ProdMiscDAO,将所有需要它的类连接起来。
  5. 这允许您的DAO实现仍然扩展SqlMappedProdMiscDAO,但也有一个接口。在切换所有类以使用接口而不是类之后,Spring将不必使用cglib代理您的DAO并且错误应该消失。

答案 1 :(得分:4)

Spring使用从接口运行时生成的代理来执行事务,方面等事务。对于DAO,服务等对象的正确的Spring习惯用法是从接口开始并创建具体的实现。完成后,您可以根据需要自由地从界面生成代理。

当然,你将拥有一个具体的DAO实现,如果你愿意,可以自由扩展SqlMapClientDaoSupport,还可以创建一个包含你方法的接口。

确保您确实需要扩展SqlMapClientDaoSupport。可能是组合和授权是一种更好的方式。