Spring - Strange Mistake in Bean Creation

Any idea why I get 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 
+7
java spring dependency-injection java-ee-6 cglib
source share
2 answers

I suspect that if ProdMiscDAO was an interface (is that?), You would not have this error. I believe that you probably have a class that receives proxies using cglib under the hood, doing magic, etc., And in the end it cannot be safely dropped to a parameter in the setter or constructor. Try programming in the interface and see if the error disappears.

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

Given this, I recommend trying the following:

  • Rename ProdMiscDAO to SqlMappedProdMiscDAO .
  • Extract the interface from SqlMappedProdMiscDAO named ProdMiscDAO (for example, " class SqlMappedProdMiscDAO implements ProdMiscDAO ")
  • Go through all the code that uses SqlMappedProdMiscDAO and change it to use ProdMiscDAO .
  • Configure spring to instantiate SqlMappedProdMiscDAO , passing all the classes that need it.

This allows your DAO implementation to continue to extend SqlMappedClientDaoSupport , as well as have an interface. After switching all classes to using the interface instead of the spring class, you will not need to use cglib for the proxy server of your DAO, and the error should disappear.

+18
source share

Spring uses proxies created at runtime from interfaces to perform operations such as transactions, aspects, etc. A custom Spring idiom for objects such as DAOs, services, etc., should start with an interface and create a specific implementation. After that, you can create proxies from the interface as needed.

So, of course, you will have a specific DAO implementation, and you can freely extend SqlMapClientDaoSupport if you want, but also create an interface that has your methods.

Make sure you really need to extend SqlMapClientDaoSupport. Perhaps composition and delegation is the best way to go.

+4
source share

All Articles