views:

947

answers:

2

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.ProdMiscDAO] for property 'prodMiscDAO'; 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.ProdMiscDAO] for property 'prodMiscDAO': 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.ProdMiscDAO] for property 'prodMiscDAO': 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

+3  A: 

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.

SingleShot
Yes its not an interface. Its actually a class extending Spring's 'SqlMapClientDaoSupport'. But its coming from third party. Is it possible to avoid this error as I won't be able to edit the jar :(
peakit
Can't you extract an interface from ProdMiscDAO? For example, you could rename ProdMiscDAO to SqlMappedProdMiscDAO or SpringProdMiscDAO, then extract an interface named ProdMiscDao and have your classes use the interface? Spring would instantiate the class (which still extends SqlMapClientDaoSupport) but pass it to something that expects the interface.
SingleShot
SingleShot.. I will give this a try.. But could you please explain me more deeply whats the cause of the error? I mean I didn't get this completely.. sorry for that :)
peakit
Well... I don't completely get it either and if this doesn't pan out I will delete my answer :-) I believe you are doing some kind of AOP (because I see that in the stack trace) which is trying to "proxy" you class (wrap it in something else) to add special behavior. This is easy to do with interfaces, but when trying to proxy a class, most tools I see use "cglib" (whatever that is) to do so, and I see "cglib" mentioned in your stack trace. My suspicion is that the generated proxy is not substitutable for the class it is proxying, thus the error.
SingleShot
I am really very impressed with the quickness with which you answered.. I am sitting on this for the last 2 days (m a newbie in Spring). I will keep you posted on how this goes - after I make the change as you suggested.. thanks :)
peakit
SingleShot, how can I do Step 1 mentioned above when the class SqlMappedProdMiscDAO (name changed as you said) is inside the jar!!
peakit
Sorry, I'm confused. I thought you were able to edit ProdMiscDAO. If you aren't able to edit it, you could write a new class, with an interface, to wrap ProdMiscDAO and then have all your code use this new interface, injecting the wrapper via Spring.
SingleShot
I tried this also. But the problem is internally in the jar there is a context.xml file which is using the prodMiscDAO (or SqlMappedProdMiscDAO) and this where the error is coming..Any ways now ?
peakit
Hmmm. So you have to use a class without an interface in a 3rd-party JAR you have no control over that also loads a Spring context from a file you have no control over and which yields the above stack trace? This isn't sounding good...
SingleShot
@peakit - Did my answer really solve the problem for you? What did you end up doing?
SingleShot
Hey SingleShot, when u said "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" This helped me find that there is some problem with the proxies. With more intense and thorough look at the code I found that the proxy creator which was meant to create proxies for our beans was also creating proxies for the library beans. So, I just disabled the proxy creator, for the time being.
peakit
+2  A: 

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.

duffymo
duffymo, the problem is that this all stuff is coming from a third party jar and i won't be able to edit anything.Is there any way I can get around this problem?
peakit
I voted SingleShot's reply up. I think that's the first thing to try. His answer is excellent; I can't improve on it.
duffymo