SteelReed

Search

Search posts, questions…

Spring BootQuestion 21 / 26

Middle

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.

this.method()implements interfacesubclass of beanCallerProxyruns adviceOriginal beanBypasses proxyJDK proxyCGLIB, Boot default

3 points to say:

  1. JDK proxy is interface-based: it only intercepts interface methods, and injecting by concrete class fails with BeanNotOfRequiredTypeException.
  2. CGLIB is subclass-based: it works without interfaces but can't intercept final/private/static.
  3. 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.