Scrivio Interview library
InternalsIntermediate6 min read · updated 12 November 2026

With a JDK dynamic proxy, @Transactional on the implementation class works when the bean is injected by its interface but injecting by the concrete class fails. Why, and how does Spring Boot avoid this by default?

A JDK dynamic proxy is a generated class that implements only the bean's interfaces, so it is an OrderService but not an OrderServiceImpl. Injecting by the interface works, while injecting by the concrete class fails at startup with a BeanNotOfRequiredTypeException, because the object in the container is not of the required type. A CGLIB class-based proxy subclasses the target, so it is an instance of the concrete class and injection by class works; Spring Boot sets spring.aop.proxy-target-class=true by default to use CGLIB, with the limitation that CGLIB cannot proxy final classes or override final methods.

Why this question is asked

This problem used to be common in plain Spring applications and still appears when a team disables class-based proxies or uses @EnableTransactionManagement without Spring Boot's defaults. The error message mentions a type like jdk.proxy2.$Proxy87, which looks unrelated to our code, so it helps to know exactly what that object is. Interviewers ask this internals question to see whether we understand the two proxy mechanisms Spring uses and why Spring Boot picked one of them. In this answer, we will reproduce the failure, see what a JDK dynamic proxy is, compare it with a CGLIB proxy, look at Spring Boot's default and finish with CGLIB's own limits.

1Reproducing the Failure

Here is a service with an interface and an implementation, and two places that inject it. Class-based proxies are turned off, so Spring uses a JDK dynamic proxy because the class implements an interface.

Java
public interface OrderService { void place(Order o); }
@Service
public class OrderServiceImpl implements OrderService {
@Transactional public void place(Order o) { ... }
}
@RestController
class OrderController {
OrderController(OrderService service) { ... } // works: the proxy is an OrderService
}
@Component
class ReportJob {
ReportJob(OrderServiceImpl service) { ... } // fails at startup
}
// spring.aop.proxy-target-class=false
// BeanNotOfRequiredTypeException: Bean named 'orderServiceImpl' is expected to be of type
// 'com.example.OrderServiceImpl' but was actually of type 'jdk.proxy2.$Proxy87'

Spring Boot's failure analyser turns this into a friendlier message suggesting to inject the interface or force class-based proxies; the exact text depends on the version.

2What a JDK Dynamic Proxy Is

A JDK dynamic proxy is created with java.lang.reflect.Proxy. At runtime, the JDK generates a new class, named something like jdk.proxy2.$Proxy87, that implements a list of interfaces we give it and sends every call to an InvocationHandler. Spring's handler runs the transaction interceptor and then calls the real OrderServiceImpl.

The important part is what the generated class does not do: it does not extend OrderServiceImpl. Its superclass is java.lang.reflect.Proxy. So it is an instance of OrderService, and of Proxy, but not of OrderServiceImpl.

Java
Object bean = context.getBean("orderServiceImpl");
System.out.println(bean.getClass().getName()); // jdk.proxy2.$Proxy87
System.out.println(bean instanceof OrderService); // true
System.out.println(bean instanceof OrderServiceImpl); // false
System.out.println(bean.getClass().getSuperclass()); // class java.lang.reflect.Proxy

So when ReportJob asks for an OrderServiceImpl, the container holds an object that simply is not one, and Spring refuses to inject it. The controller, which asks for the interface, is fine.

3How a CGLIB Proxy Avoids It

CGLIB, a bytecode generation library that Spring repackages inside spring-core, creates a subclass of the target class at runtime. The generated class, named like OrderServiceImpl$$SpringCGLIB$$0, extends OrderServiceImpl and overrides its non-final methods to run the interceptors before delegating to the real bean. Because it is a subclass, it is an instance of the concrete class and of every interface the class implements.

Java
// spring.aop.proxy-target-class=true (Spring Boot default)
Object bean = context.getBean("orderServiceImpl");
System.out.println(bean.getClass().getName()); // com.example.OrderServiceImpl$$SpringCGLIB$$0
System.out.println(bean instanceof OrderService); // true
System.out.println(bean instanceof OrderServiceImpl); // true
JDK dynamic proxy versus CGLIB proxy
AspectJDK dynamic proxyCGLIB class proxy
How it is builtImplements the bean's interfacesSubclasses the bean's class
instanceof the concrete classNoYes
Inject by concrete classFails: BeanNotOfRequiredTypeExceptionWorks
Needs an interfaceYesNo
Intercepted methodsInterface methods onlyNon-private, non-final methods
Cannot handleClasses without interfacesFinal classes, final methods

Both ReportJob and OrderController now receive the same proxy without errors.

4Spring Boot's Default

Plain Spring's rule is to use a JDK proxy when the bean implements at least one interface and CGLIB otherwise, unless proxyTargetClass is set. Spring Boot changed the default many versions ago: its AOP and transaction auto-configuration sets spring.aop.proxy-target-class=true, so it uses CGLIB class proxies for every bean, interface or not.

The reason is exactly this problem. With CGLIB, injection by interface and by class both work, and moving an annotation from one class to another never changes which injection points are valid. The cost of class proxies has been negligible on modern JVMs for a long time, so the safer behaviour won.

Java
# application.properties
spring.aop.proxy-target-class=true # Spring Boot default; false restores JDK proxies for interface beans
// Without Spring Boot, the equivalent:
@EnableTransactionManagement(proxyTargetClass = true)

5CGLIB's Own Limits

A CGLIB proxy must subclass and override, so it inherits Java's rules about that. A final class cannot be subclassed at all, and Spring fails at startup when it tries to proxy one, with an error saying it could not generate a CGLIB subclass. A final method can be subclassed around but not overridden, so it is not intercepted, and a call to it on the proxy runs on the proxy instance, whose injected fields are null. Private methods are never intercepted either.

Kotlin, where classes and methods are final by default, makes these limits common; the kotlin-spring (all-open) compiler plugin opens classes annotated with Spring annotations to avoid them. And when we need a bean to be final, injecting it by an interface with a JDK proxy remains a valid choice.

6Follow-up questions

Is it still good practice to inject by interface?

Often yes, because it keeps callers independent of the implementation and allows test doubles. With Spring Boot's CGLIB default it is no longer required for proxies to work, so the choice is about design, not about avoiding errors.

Does the CGLIB proxy call the bean's constructor twice?

Historically, creating a CGLIB proxy instance ran the superclass constructor. Spring uses Objenesis to create the proxy instance without calling the constructor, so constructor side effects do not run twice; that is also why the proxy's own fields stay null.

Where else does this error appear?

With @Async, caching or any other proxy-creating feature that ends up with a JDK proxy, for example when @EnableAsync is configured without proxyTargetClass in a non-Boot application. The diagnosis and fix are the same.

7Conclusion

In this answer, we saw that a JDK dynamic proxy is a generated class that implements only the bean's interfaces and extends java.lang.reflect.Proxy, so it is not an instance of the implementation class and injecting by that class fails with BeanNotOfRequiredTypeException. A CGLIB proxy subclasses the target, so injection by class works, and Spring Boot defaults spring.aop.proxy-target-class to true for that reason. CGLIB in turn cannot proxy final classes or intercept final and private methods.

What to say in the interviewAbout 90 seconds aloud

Open with

With a JDK dynamic proxy, the JDK generates a class at runtime, something like jdk.proxy2.$Proxy87, that implements the bean's interfaces and forwards every call to Spring's invocation handler, which runs the transaction interceptor and then calls the real object.

Then, if they want more

But that class extends java.lang.reflect.Proxy, not my implementation class. So the proxy is an OrderService, but it is not an OrderServiceImpl. Injecting by the interface works, but injecting by the concrete class fails at startup with BeanNotOfRequiredTypeException, saying the bean was expected to be OrderServiceImpl but was actually the proxy type.

A CGLIB proxy works differently: it generates a subclass of the target class, OrderServiceImpl$$SpringCGLIB$$0, overriding the methods to add the interceptors. Since it's a subclass, it's an instance of both the class and its interfaces, so injection by either works.

Spring Boot sets spring.aop.proxy-target-class to true by default, so it always uses CGLIB class proxies, which is why most of us never see this error; without Boot you'd set proxyTargetClass = true on @EnableTransactionManagement.

The trade-off is CGLIB's own limits: it can't proxy final classes at all, and it can't override final or private methods, so those aren't intercepted, and a final method called on the proxy even runs with null fields. That's why Kotlin projects use the kotlin-spring plugin to open classes.

About this answer

Last updated
12 November 2026
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 "With a JDK dynamic proxy, @Transactional on the implementation class works when the bean...": https://scrivio-interview-library.netlify.app/databases/transactions/5f652c75e0/