Monday, August 02, 2010

Cobertura and Spring auto proxying

If you are using Cobertrua to get coverage reports, you may run into the error message shown below (lines folded for clarity):
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'myBean' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Initialization of bean failed;
nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type
'$Proxy28 implementing net.sourceforge.cobertura.coveragedata.HasBeenInstrumented, org.springframework.aop.SpringProxy, org.springframework.aop.framework.Advised' to required type 'com.mydomain.MyDao' for property 'myDao';
nested exception is java.lang.IllegalStateException: Cannot convert value of type
[$Proxy28 implementing net.sourceforge.cobertura.coveragedata.HasBeenInstrumented, org.springframework.aop.SpringProxy, org.springframework.aop.framework.Advised] to required type [com.mydomain.MyDao]
for property 'myDao': no matching editors or conversion strategy found
 Specifically if you are using AspectJ auto proxying, like below:
    <context:annotation-config/>
    <aop:aspectj-autoproxy/>
 To fix the issue, just add the following property:

    <context:annotation-config/>
    <aop:aspectj-autoproxy proxy-target-class="true" />
That should fix the exception shown above. To know more about this property please refer to Spring documentation.

8 comments:

Anonymous said...

Sometimes it helps if you convert the property type to an interface.

Anonymous said...

Thanks, this post pointed me to the right solution. It seems that cobertura instrumentation was messing up our spring integration configuration (ServiceActivator could not be resolved in the unit tests).

The solution was to set the scoped-proxy to targetClass, eg.

Anonymous said...

I do not use aspectJ, I do not use autowire, but I do have this problem, even if I add the said line.

I noticed that this seem to happend with tests related to Hibernate, which also has it's own proxing mecanism.
The project we have that uses hibernate gets this problem, the others do not.

Florian said...

Thanks for the post, this helped a lot. This also can be the case when using <tx:annotation-driven /> that also supports the attribute proxy-target-class

Sudarshan Angirash said...

Thanks Man... You saved my day

Eugene Polyhaev said...

I followed advice from the 1st comment to interface my concrete class and it worked well. This feels like a preferred practice, because I see this in official Spring docs (following your link):

"Spring AOP uses either JDK dynamic proxies or CGLIB to create the proxy for a given target object. (JDK dynamic proxies are preferred whenever you have a choice)"

Dulsara Manupriyal said...

you are life saver, thank buddy
u did a great help :)

sql server commands cheat sheet said...

The regarded prospects would bring around more of the values for the students which must have been followed by the individual to regard about.