Spring - Bean创建中的奇怪错误 [英] Spring - Weird Error in Bean Creation

查看:156
本文介绍了Spring - Bean创建中的奇怪错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何想法为什么我得到这个例外?

Any idea why I am getting this exception?

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或构造函数中的参数。尝试编程到界面,看看错误是否消失。

I suspect that if ProdMiscDAO was an interface (is it?) you would not have this error. I believe you probably have a class that is getting proxied using cglib under the hood, performing magic, etc. and in the end it cannot be safely cast to a paramter in a setter or constructor. Try programming to an interface and see if the error goes away.

更新 ProdMiscDAO 不是界面。这是一个延伸 SqlMappedClientDaoSupport 的类。

Update: ProdMiscDAO is not an interface. It is a class that extends SqlMappedClientDaoSupport.

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

Given this, I recommend trying this:


  1. 将$ code> ProdMiscDAO 重命名为 SqlMappedProdMiscDAO

  2. SqlMappedProdMiscDAO 中提取一个接口,命名为 ProdMiscDAO (例如使用SqlMappedProdMiscDAO实现ProdMiscDAO

  3. 查看使用 SqlMappedProdMiscDAO 的所有代码,并将其更改为使用 ProdMiscDAO

  4. 配置spring来实例化一个 SqlMappedProdMiscDAO ,将其全部连接需要它的类。

  1. Rename ProdMiscDAO to SqlMappedProdMiscDAO.
  2. Extract an interface from SqlMappedProdMiscDAO named ProdMiscDAO (e.g. "class SqlMappedProdMiscDAO implements ProdMiscDAO")
  3. Go through all your code that uses SqlMappedProdMiscDAO and change it to use ProdMiscDAO.
  4. Configure spring to instantiate a SqlMappedProdMiscDAO, wiring it all the classes that need it.

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

This allows your DAO implementation to still extend SqlMappedClientDaoSupport but also to have an interface. After switching over all classes to use the interface instead of the class, Spring will not have to use cglib to proxy your DAO and the error should go away.

这篇关于Spring - Bean创建中的奇怪错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆