How do JDK dynamic proxies and CGLIB proxies differ? Which does Spring Boot use by default?
Short answer
A JDK dynamic proxy implements the same interfaces as the bean; CGLIB creates a subclass of the bean's class. Spring Boot defaults to spring.aop.proxy-target-class=true, i.e. CGLIB even when there is an interface.
3 points to say:
- JDK proxy is interface-based: it only intercepts interface methods, and injecting by concrete class fails with
BeanNotOfRequiredTypeException. - CGLIB is subclass-based: it works without interfaces but can't intercept
final/private/static. - Calling
this.method()inside the class bypasses the proxy, so advice (e.g.@Transactional) doesn't run.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.