知道为什么我会收到此异常吗?

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
有帮助吗?

解决方案

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

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

鉴于此,我建议尝试以下方法:

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

这允许您的 DAO 实现仍然可以扩展 SqlMappedClientDaoSupport 而且还要有一个接口。将所有类切换为使用接口而不是类后,Spring 将不必使用 cglib 来代理您的 DAO,并且错误应该消失。

其他提示

Spring使用代理服务器,从界面运行时产生的,做事情喜欢交易环节等,为喜欢的DAO,服务等对象正确的春天成语是开始界面,并创建一个具体实现。一旦你的,你可以自由地根据需要从接口生成代理。

所以,当然,你也会有一个具体的DAO实现,这就是自由地扩展SqlMapClientDaoSupport如果你愿意,也创造了你的方法的接口。

要确保你确实需要延长SqlMapClientDaoSupport。这可能是成分和授权是一个更好的路要走。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top