Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - Weird Error in Bean Creation

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
like image 343
peakit Avatar asked Sep 23 '09 21:09

peakit


People also ask

What is Bean Creation exception in Spring?

By far, the most common cause of the BeanCreationException is Spring trying to inject a bean that doesn't exist in the context. For example, BeanA is trying to inject BeanB: @Component public class BeanA { @Autowired private BeanB dependency; ...

How bean is created in Spring?

Bean life cycle is managed by the spring container. When we run the program then, first of all, the spring container gets started. After that, the container creates the instance of a bean as per the request, and then dependencies are injected. And finally, the bean is destroyed when the spring container is closed.

Are beans in Spring eagerly initialized by default yes or no?

By default, ApplicationContext implementations eagerly create and configure all singleton beans as part of the initialization process.

Why bean is not created?

Because you haven't given it any name, Spring is likely used something like MyBeanImpl as the bean name. @Component annotation supports giving bean a name, use that.


1 Answers

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.

like image 196
SingleShot Avatar answered Sep 18 '22 02:09

SingleShot