Spring - Bean 创建中的奇怪错误

发布于 2024-08-05 20:22:00 字数 3176 浏览 4 评论 0原文

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗼ふ静 2024-08-12 20:22:00

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

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

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

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

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

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.

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

Given this, I recommend trying this:

  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.

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.

别忘他 2024-08-12 20:22:00

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

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

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

Spring uses proxies, generated at run-time from interfaces, to do things like transactions, aspects, etc. The proper Spring idiom for objects like DAOs, services, etc. is to start with an interface and create a concrete implementation. Once you have that, you're free to generate proxies from the interface as needed.

So of course you'll have a concrete DAO implementation, and that's free to extend SqlMapClientDaoSupport if you wish, but also create an interface that has your methods.

Be sure that you really need to extend SqlMapClientDaoSupport. It could be that composition and delegation is a better way to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文