Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

When does Spring use a JDK dynamic proxy and when does it use CGLIB, and what does Spring Boot do by default?

A JDK dynamic proxy is a generated class that implements the bean's interfaces, while a CGLIB proxy is a generated subclass of the bean's own class. Plain Spring, with proxyTargetClass left at false, uses a JDK proxy when the bean implements at least one interface and CGLIB otherwise. Spring Boot sets spring.aop.proxy-target-class to true by default, so it uses CGLIB proxies even for beans that implement interfaces, and CGLIB cannot proxy final classes or override final methods.

Why this question is asked

Most of the time we never notice which kind of proxy Spring created. We notice when something breaks: an injection fails with a type mismatch, a final class refuses to start, or a method on the proxy quietly skips the advice. Interviewers ask this to check that we know both mechanisms, the default rules, and the one place where Spring Boot's default differs from plain Spring. In this answer, we will recap why Spring needs proxies, see how each kind is built, go through the rule Spring uses to choose, look at Spring Boot's default and why it exists, and finish with the limits of CGLIB.

1A Quick Recap: Why Spring Needs a Proxy

Features such as @Transactional, @Cacheable and @Async are applied by wrapping the bean in a proxy, an object generated at runtime that other beans call instead of the real one. The proxy runs the extra behaviour and then calls the real object, which Spring calls the target. For this to work, the proxy must be usable wherever the bean is injected, so it must have the right type. There are two ways to give a generated object the right type: implement the same interfaces, or extend the same class.

2How a JDK Dynamic Proxy Works

The JDK has built-in support for proxies through java.lang.reflect.Proxy. Given a list of interfaces and an InvocationHandler, it generates a class at runtime that implements those interfaces and sends every method call to the handler. Spring's handler runs the advice chain and then calls the target.

Java
interface OrderService { void place(Order o); }
class OrderServiceImpl implements OrderService { public void place(Order o) { ... } }
OrderService proxy = (OrderService) Proxy.newProxyInstance(
OrderService.class.getClassLoader(),
new Class<?>[] { OrderService.class },
(p, method, args) -> { log.info("before"); return method.invoke(target, args); });
proxy.place(order); // before, then the real place()
System.out.println(proxy instanceof OrderService); // true
System.out.println(proxy instanceof OrderServiceImpl); // false
System.out.println(proxy.getClass().getName()); // jdk.proxy2.$Proxy87 (name varies)

Notice the second-to-last line. A JDK proxy is an OrderService but not an OrderServiceImpl, because it only implements the interface. It knows nothing about methods that exist only on the class. This is why injecting such a bean by its concrete class fails.

3How a CGLIB Proxy Works

CGLIB is a bytecode generation library that Spring repackages inside spring-core. It generates a subclass of the bean's class at runtime, named something like OrderServiceImpl$$SpringCGLIB$$0, and overrides each non-final, non-private method to route the call through the advice chain before calling the target. Because it is a subclass, the proxy is an instance of the class and of every interface the class implements, so it can be injected by either type.

Spring creates the proxy instance without calling the class's constructor, using a library called Objenesis, so the constructor of our bean runs only once, for the real target. The proxy instance's own fields stay uninitialised, which is fine because every advised call is forwarded to the target.

4How Spring Chooses Between Them

The choice is controlled by a flag called proxyTargetClass, found on @EnableAspectJAutoProxy, @EnableTransactionManagement, @EnableCaching and @EnableAsync. In plain Spring Framework it defaults to false, and the rule is: if the bean implements at least one interface (other than marker or callback interfaces such as Serializable or Spring's Aware types), use a JDK proxy over those interfaces; if it implements none, use CGLIB. Setting proxyTargetClass to true forces CGLIB for every bean.

Step throughWhich proxy does Spring create?
  1. Is proxyTargetClass true?Spring Boot default: true
    YesUse CGLIBNoCheck the interfaces
  2. Does the bean implement a real interface?ignoring marker and callback interfaces
    YesUse a JDK dynamic proxyNoUse CGLIB
  3. Is the class or method final?only matters for CGLIB
    YesCannot proxy it; advice is lost or startup failsNoCGLIB subclass is generated

3 steps in order. Play them, or step through them one at a time.

Plain Spring takes the interface branch; Spring Boot goes straight to CGLIB.

5Spring Boot's Default and Why

Spring Boot's AOP auto-configuration sets spring.aop.proxy-target-class=true by default, and has done so since Spring Boot 2.0. So in a Boot application, beans are proxied with CGLIB even when they implement interfaces. The reason is practical: with JDK proxies, injecting a bean by its concrete class fails with BeanNotOfRequiredTypeException, and that surprised many developers. With CGLIB, injection by class or by interface both work. We can switch back with spring.aop.proxy-target-class=false in application.properties if we want interface proxies.

Spring Framework 7 adds more ways to pick the proxy type per bean, so check your version's documentation if you need a mix; the principle stays the same.

6What CGLIB Cannot Do

Because CGLIB works by subclassing, it cannot proxy a final class at all, since a final class cannot be extended, and it cannot override final methods. A final class with a @Transactional method fails at startup when Spring tries to create the proxy. A final method on a proxied class is worse: it is not intercepted, and because the call runs on the proxy instance, whose fields were never initialised, it may throw a NullPointerException on an injected field. Private methods are also never intercepted, by either kind of proxy.

This is why Kotlin projects use the kotlin-spring compiler plugin, which makes classes annotated with Spring stereotypes open, since Kotlin classes are final by default. JDK proxies do not have the final problem, because they never extend the class, but they only proxy interface methods.

7Follow-up questions

Which is faster, a JDK proxy or a CGLIB proxy?

On modern JVMs the difference is small and rarely matters for business code, because the advice itself, such as starting a transaction, costs far more than the proxy dispatch. Choose based on behaviour and design, not speed.

Do records work with CGLIB?

No. Records are implicitly final, so they cannot be subclassed. That is rarely an issue, because records are data carriers and are not usually beans with advised methods.

How can I see which proxy a bean has?

AopUtils.isJdkDynamicProxy(bean) and AopUtils.isCglibProxy(bean) tell us directly, and printing bean.getClass().getName() shows either a jdk.proxy name or a $$SpringCGLIB$$ suffix.

8Conclusion

In this answer, we saw that a JDK dynamic proxy implements the bean's interfaces and a CGLIB proxy subclasses the bean's class. Plain Spring uses JDK proxies for beans with interfaces and CGLIB for the rest, unless proxyTargetClass is true. Spring Boot sets spring.aop.proxy-target-class to true by default, so it uses CGLIB, which lets injection by class work. CGLIB cannot proxy final classes or final methods, and neither kind can intercept private methods.

What to say in the interviewAbout 80 seconds aloud

Open with

There are two ways Spring can build a proxy. A JDK dynamic proxy uses java.lang.reflect.Proxy to generate a class that implements the bean's interfaces and routes every call to an invocation handler, so the proxy is an instance of the interfaces but not of the implementation class.

Then, if they want more

A CGLIB proxy is a generated subclass of the bean's class that overrides its methods, so it is an instance of both the class and its interfaces. Spring creates CGLIB proxy instances with Objenesis, so the bean's constructor does not run a second time.

In plain Spring Framework, the proxyTargetClass flag on annotations like @EnableTransactionManagement defaults to false, and Spring uses a JDK proxy when the bean implements at least one real interface and CGLIB when it does not. Spring Boot sets spring.aop.proxy-target-class to true by default since 2.0, so in a Boot app beans get CGLIB proxies even when they have interfaces.

That avoids the classic BeanNotOfRequiredTypeException when someone injects the concrete class. The limits of CGLIB come from subclassing: it cannot proxy a final class, it cannot intercept final methods, and like JDK proxies it never intercepts private methods. That is also why Kotlin needs the kotlin-spring plugin to open classes.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 2.0, Spring Framework 7
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "When does Spring use a JDK dynamic proxy and when does it use CGLIB, and what does...": https://scrivio-interview-library.netlify.app/spring-boot/aop/764f06fa21/