Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring - forcing cglib proxies for @Autowired fields

I have mixed stack: EJB and Spring. For autowiring Spring to EJBs I use SpringBeanAutowiringInterceptor (not sure if that could affect the problem I have).

Whlie trying to autowire beans in a following way:

@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS, value="singleton")
@Repository
public class ClassA imlpements IfA {
    ...
}


@Scope(value="singleton")
@Repository
public class ClassB {
    @Autowired
    private ClassA classA;

    ...
}

I get error:

Could not autowire field: private ClassA ClassB.classA; nested exception is java.lang.IllegalArgumentException: Can not set ClassA field ClassB.classA to com.sun.proxy.$Proxy257. Please see server.log for more details.

As far as I've understood, the problem is that JDK proxies are used (as ClassA implements interface IfA), while I'd need cglib ones.

But I don't get it. Based on the docs, the annotation: @Scope(proxyMode=ScopedProxyMode.TARGET_CLASS, should do the job here.

Well and it's not an option for me to use interfaces (as a field type).

Any idea how to force cglib proxying here?

EDIT:

Not sure if it's relevant, but I'm running glassfish 3.1.x.

And stacktrace (partial, as I can't expose all the class names here:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'classB': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ClassA ClassB.classA; nested exception is java.lang.IllegalArgumentException: Can not set ClassA field ClassB.classA to com.sun.proxy.$Proxy257.
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1116)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:912)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
    ... 89 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ClassA ClassB.classA; nested exception is java.lang.IllegalArgumentException: Can not set ClassA field ClassB.classA to com.sun.proxy.$Proxy257. Please see server.log for more details.
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    ... 100 more
Caused by: java.lang.IllegalArgumentException: Can not set ClassA field ClassB.classA to com.sun.proxy.$Proxy257
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    at java.lang.reflect.Field.set(Field.java:741)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:510)
    ... 102 more
like image 772
Peter Butkovic Avatar asked Aug 14 '14 10:08

Peter Butkovic


People also ask

How are JDK dynamic proxies and Cglib proxies used in Spring?

From Spring documentation : 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). If the target object to be proxied implements at least one interface then a JDK dynamic proxy will be used.

How does Cglib proxy work?

Cglib and Javassist provide support for proxying classes because they can dynamically generate bytecode (i.e. class files), allowing us to extend classes at runtime in a way that Java's Proxy can implement an interface at runtime. At the core of Cglib is the Enhancer class, which is used to generate dynamic subclasses.

What are the two types of proxies used in Spring which are the limitations of the two proxy types?

Spring AOP is proxy based. Spring used two types of proxy strategy one is JDK dynamic proxy and other one is CGLIB proxy. JDK dynamic proxy is available with the JDK. It can be only proxy by interface so target class needs to implement interface.

Does Spring use Cglib?

Spring uses CGLIB, to generate proxies. Spring AOP defaults to using standard JDK dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be proxied.


1 Answers

You should not use aop scoped proxies for singletons.

Use <aop:aspectj-autoproxy proxy-target-class="true" /> or @EnableAspectJAutoProxy(proxyTargetClass=true) in your spring configuration file or class to force cglib proxies.

like image 100
Jose Luis Martin Avatar answered Oct 17 '22 06:10

Jose Luis Martin