Sub topic 5
CGLIB Proxies and Subclassing
This chapter assumes you know core Java inheritance, basic Spring beans, and the JDK dynamic proxy from the previous chapter. JDK proxies need an interface. CGLIB proxies do not.
The idea: generate a subclass
CGLIB (Code Generation Library) creates a new class at runtime that extends your class. Spring ships a repackaged copy inside spring-core, so you add no dependency. The generated subclass overrides your public and protected methods. Each override runs the advice, then calls the original method on the real target object.
The proxy is-a OrderService, so you can inject it into a field of that class type. A JDK proxy cannot do this, because it only implements interfaces and is not a subclass of your class.
A class with no interface
import org.springframework.aop.framework.ProxyFactory;
import org.aopalliance.intercept.MethodInterceptor;
class OrderService {
public String place(String item) {
return "Placed " + item;
}
}
public class CglibDemo {
public static void main(String[] args) {
ProxyFactory factory = new ProxyFactory(new OrderService());
factory.addAdvice((MethodInterceptor) invocation -> {
System.out.println("calling " + invocation.getMethod().getName());
return invocation.proceed();
});
OrderService proxy = (OrderService) factory.getProxy();
System.out.println(proxy.place("book"));
System.out.println(proxy.getClass().getName());
}
}OrderService implements no interface, so ProxyFactory falls back to CGLIB. The last line prints a name like OrderService$$SpringCGLIB$$0. That name is a quick way to confirm which kind of proxy you have.
Spring Boot defaults
Spring Boot sets spring.aop.proxy-target-class=true. With that setting Spring uses CGLIB even when a bean implements interfaces. Plain Spring Framework without Boot uses a JDK proxy when interfaces exist, unless you set @EnableAspectJAutoProxy(proxyTargetClass = true).
Constraints from subclassing
Because the proxy is a subclass, Java inheritance rules set the limits:
| Class feature | Effect |
|---|---|
| `final` class | Cannot be subclassed, so no proxy can be created. |
| `final` method | Cannot be overridden, so advice is skipped. |
| `private` method | Not visible to the subclass, so advice is skipped. |
| `static` method | Belongs to the class, not the instance, so advice is skipped. |
Spring uses Objenesis to create the proxy instance without calling your constructor. This means constructor side effects do not run twice. The proxy's own fields are not initialized, which is why a final method that reads a field can see null when you call it on the proxy.
Common errors
- Making a bean class
final, which causes anAopConfigExceptionwhen Spring tries to proxy it. - Expecting advice on
privateor package-private helper methods that other classes cannot override. - Casting a proxy to the implementation class while Spring uses a JDK proxy. Inject the interface, or enable class-based proxies.
Exercise
A bean class ReportService has no interface, and one of its methods is declared final. Spring applies an @Around aspect to every method in the class. What happens when you call the final method through the injected bean?
Show answer
Spring creates a CGLIB proxy for the class, but the proxy cannot override the final method. The call runs on the proxy instance without any advice, and the proxy's fields are uninitialized, so the method may see null values. The fix is to remove final from the method.