Sub topic 7
How Spring decides which proxy to create
This chapter assumes you know the proxy pattern, JDK dynamic proxies and CGLIB from the earlier chapters. It also assumes you can write a basic Spring bean. Here you will learn the rule Spring applies to each bean, and the setting that changes the result.
The decision rule
Spring picks the proxy type per bean, when it wraps the bean after creation. The choice comes from DefaultAopProxyFactory, which asks two questions:
- Is
proxyTargetClassset to true? - Does the target class implement at least one interface?
A JDK proxy implements the bean's interfaces. A CGLIB proxy is a generated subclass of the bean's class. If the target is itself an interface or already a JDK proxy, Spring uses a JDK proxy.
The defaults differ by setup
| Setup | `proxyTargetClass` default | Typical result for a bean with an interface |
|---|---|---|
| Plain Spring Framework, `@EnableAspectJAutoProxy` | `false` | JDK proxy |
| Spring Boot (since 2.0) | `true` | CGLIB proxy |
In Boot, set spring.aop.proxy-target-class=false to prefer JDK proxies. In plain Spring, use @EnableAspectJAutoProxy(proxyTargetClass = true) to force CGLIB.
Observing the choice
AopUtils tells you which kind of proxy you have. This example uses ProxyFactory, which follows the same rule as the container.
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.AopUtils;
interface GreetingService {
String greet(String name);
}
class GreetingServiceImpl implements GreetingService {
public String greet(String name) {
return "Hello, " + name;
}
}
class Plain {
public String hello() {
return "hi";
}
}
public class ProxyChoiceDemo {
public static void main(String[] args) {
report("interface", new ProxyFactory(new GreetingServiceImpl()));
ProxyFactory forced = new ProxyFactory(new GreetingServiceImpl());
forced.setProxyTargetClass(true);
report("forced class", forced);
report("no interface", new ProxyFactory(new Plain()));
}
static void report(String label, ProxyFactory factory) {
factory.addAdvice((MethodInterceptor) invocation -> invocation.proceed());
Object proxy = factory.getProxy();
System.out.println(label + ": jdk=" + AopUtils.isJdkDynamicProxy(proxy)
+ ", cglib=" + AopUtils.isCglibProxy(proxy));
}
}The output is:
interface: jdk=true, cglib=false
forced class: jdk=false, cglib=true
no interface: jdk=false, cglib=trueWhy the choice matters
- A JDK proxy is not a subclass of your implementation class. Injecting the bean as
GreetingServiceImplfails, so inject the interface. - A CGLIB proxy can be injected by class or by interface, which is one reason Boot defaults to it.
- CGLIB cannot proxy a
finalclass. It also cannot interceptfinalorprivatemethods. A JDK proxy only exposes interface methods. - Modern Spring creates CGLIB proxies without needing a no-argument constructor, because it uses Objenesis.
Exercise
A Spring Boot 3 application has a @Service class OrderService that implements OrderApi. A timing aspect applies to it. Which proxy type does Spring create, and what changes if you set spring.aop.proxy-target-class=false?
Show answer
By default Boot sets proxyTargetClass to true, so Spring creates a CGLIB subclass proxy. With the property set to false, the class implements an interface, so Spring creates a JDK proxy. Code that injects OrderService by its concrete class would then fail, and it must inject OrderApi instead.