小编典典

Spring-Bean创建中的奇怪错误

java

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

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

阅读 223

收藏
2020-11-13

共1个答案

小编典典

我怀疑如果ProdMiscDAO是接口(是吗?),您将不会遇到此错误。我相信您可能有一个正在使用cglib代理,执行魔术等操作的类,最后,它不能安全地转换为setter或构造函数中的参数。尝试对接口进行编程,看看错误是否消失。

更新ProdMiscDAO不是接口。这是一个扩展的类SqlMappedClientDaoSupport

鉴于此,我建议您尝试以下操作:

  1. 重命名ProdMiscDAOSqlMappedProdMiscDAO
  2. 从已SqlMappedProdMiscDAO命名的接口中提取一个接口ProdMiscDAO(例如“ class SqlMappedProdMiscDAO implements ProdMiscDAO”)
  3. 浏览所有使用的代码,SqlMappedProdMiscDAO然后将其更改为use ProdMiscDAO
  4. 将spring配置为实例化a SqlMappedProdMiscDAO,将其连接到需要它的所有类。

这使您的DAO实现仍可扩展,SqlMappedClientDaoSupport但也具有接口。在切换了所有类以使用接口而不是类之后,Spring将不必使用cglib代理您的DAO,并且错误应消失。

2020-11-13