Using proxy-target-class="true" with Spring beans
Asked Answered
P

4

29

Im using Jersey Rest and want a Jersey filter to have access to some spring beans.

however as I've discovered from other threads, Jersey does not obtain Spring beans if they are Java proxies as opposed to generated java proxies. I want to add the proxy-target-class="true"

What are the impacts of doing so and also can this just be set on a single bean or does it need to be set on all referenced beans?

Poisonous answered 22/3, 2013 at 10:30 Comment(0)
A
45

By setting proxy-target-class="true" you will be using CGLIB2 for your proxies, instead of jdk proxys.

The implications are the following, as described in the documentation:

  • final methods cannot be advised, as they cannot be overriden.

  • You will need the CGLIB 2 binaries on your classpath, whereas dynamic proxies are available with the JDK. Spring will automatically warn you when it needs CGLIB and the CGLIB library classes are not found on the classpath.

  • The constructor of your proxied object will be called twice. This is a natural consequence of the CGLIB proxy model whereby a subclass is generated for each proxied object. For each proxied instance, two objects are created: the actual proxied object and an instance of the subclass that implements the advice. This behavior is not exhibited when using JDK proxies. Usually, calling the constructor of the proxied type twice, is not an issue, as there are usually only assignments taking place and no real logic is implemented in the constructor.

Also, you should be able to make a "target-proxy" for a specific component by using

proxyMode=ScopedProxyMode.TARGET_CLASS
Appanage answered 22/3, 2013 at 10:49 Comment(8)
Note that is an error to create a aop-scoped proxy for a singleton bean.Involution
It was an example. Edited to avoid confusion.Appanage
but how exactly do i set it on a bean? For example <bean id="manager" proxy-target-class="true" class="com.fmr.af.cms.impl.ManagerImpl"> this will fail complaining about the occurance of the proxy-target referencePoisonous
and further do i need to set it on all referenced beans?Poisonous
Hi @gargc Please Help!! I am facing following exception:org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.mchange.v2.c3p0.ComboPooledDataSource]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: Cannot subclass final class class com.mchange.v2.c3p0.ComboPooledDataSourceEmasculate
@NavdeepSingh the error message is clear: Cannot subclass final class class com.mchange.v2.c3p0.ComboPooledDataSource. That class is a final class, final class cannot subclassed.Spitz
@Poisonous you can set it on a bean by putting this inside the xml element: <aop:scoped-proxy proxy-target-class="true" />Redundancy
There's an update - "the constructor is not called twice anymore since 4.0" docs.spring.io/spring-framework/reference/core/aop/…Pe
M
24

Forcing a CGLib-Proxy although the controller formally implements an interface (SpringBoot 1.2.3.RELEASE with Spring 4.1.6.RELEASE):

@Controller
@Scope( proxyMode = ScopedProxyMode.TARGET_CLASS )
public class ServiceImpl implements ServiceIntf
{ .... }

This enables valid and working @RequestMapping and @Transactional annotations

Mucous answered 6/8, 2015 at 13:10 Comment(0)
C
8

Use the following annotation in Java Spring Config class:

@EnableAspectJAutoProxy(proxyTargetClass = true)

Cephalopod answered 27/5, 2017 at 13:11 Comment(1)
doesn't work: by debugging I can see it's alway jdkProxyIntermixture
B
0

This is the way I made my test working:

MyTarget target = new MyTarget();
AspectJProxyFactory factory = new AspectJProxyFactory(target);
factory.setProxyTargetClass(true);
Burberry answered 30/9, 2022 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.