Quick revision
The short answer
A JDK dynamic proxy is a generated class that implements the bean's interfaces, while a class-based (CGLIB-style) proxy is a generated subclass of the bean's class; Spring Boot uses class-based proxies by default, and those cannot advise final classes or final methods. Either way, the proxy runs the transaction logic and then calls the real target object. A @Transactional method called from another method in the same class is called on this, the target, not on the proxy, so the advice never runs; the fixes are to move the method to another bean, call it through an injected reference to the proxy, or use AspectJ weaving.
What to remember
In this answer, we saw that Spring AOP applies @Transactional through proxies. JDK dynamic proxies implement the bean's interfaces, while class-based proxies are generated subclasses, which Spring Boot uses by default and which cannot advise final classes or final methods. The proxy runs the transaction logic and then calls the target, so a call from one method to another inside the target goes through this and skips the advice. Moving the method to another bean is the cleanest fix, with self-injection, AopContext or TransactionTemplate as alternatives and AspectJ weaving for advice that works without proxies.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A JDK dynamic proxy is a generated class that implements the bean's interfaces, while a class-based (CGLIB-style) proxy is a generated subclass of the bean's class; Spring Boot uses class-based proxies by default, and those cannot advise final classes or final methods. Either way, the proxy runs the transaction logic and then calls the real target object. A @Transactional method called from another method in the same class is called on this, the target, not on the proxy, so the advice never runs; the fixes are to move the method to another bean, call it through an injected reference to the proxy, or use AspectJ weaving.
Why this question is asked
@Transactional is probably the most used annotation in Spring back ends, and the proxy behind it explains a whole family of surprises: transactions that do not start, final methods that are ignored, and beans whose class is not what we expect in the debugger. Interviewers ask this because it checks whether we know how Spring adds behaviour, not just that it does. In this answer, we will recap what a proxy is, compare JDK dynamic proxies with class-based proxies and see which one Spring Boot uses, follow a call through the proxy, reproduce the self-invocation problem, and go through the fixes.
1A Quick Recap: What a Proxy Does
A proxy is an object that stands in for another object, the target, and has the same type from the caller's point of view. Spring AOP, Spring's aspect-oriented programming support, creates a proxy for every bean that needs extra behaviour, such as a bean with @Transactional methods. Other beans get the proxy injected instead of the real object.
When a caller invokes a method on the proxy, the proxy checks whether the method needs advice, the added behaviour. For @Transactional, the advice is the transaction interceptor: it begins a transaction, calls the target's method, and commits or rolls back depending on whether it returned or threw.
2JDK Dynamic Proxies and Class-Based Proxies
Java offers a built-in way to create proxies: java.lang.reflect.Proxy generates, at run time, a class that implements a given list of interfaces and sends every call to an InvocationHandler. A JDK dynamic proxy for OrderService implements the OrderService interface, but it is not an OrderServiceImpl, so it can only be injected where the interface type is expected.
Class-based proxies are generated subclasses. Spring uses a repackaged copy of the CGLIB library to generate a class like OrderServiceImpl$$SpringCGLIB$$0 that extends OrderServiceImpl and overrides its methods to apply the advice before calling the target. Because it is a subclass, it works for beans without interfaces and can be injected as the concrete class. But a subclass cannot override what Java forbids overriding: a final class cannot be proxied at all, and final methods, as well as private ones, are never advised.
Spring Boot sets spring.aop.proxy-target-class to true by default, so it uses class-based proxies even when the bean has interfaces. The reason is practicality: injecting by concrete class then works, and teams do not get confusing errors when a bean happens to implement an interface. Plain Spring Framework without Boot still prefers JDK proxies when interfaces exist.
| Aspect | JDK dynamic proxy | Class-based (CGLIB-style) proxy |
|---|---|---|
| How it is built | Implements the bean's interfaces | Generated subclass of the bean class |
| Needs an interface | Yes | No |
| Inject as the concrete class | No, only as the interface | Yes |
| final class | Fine, only interfaces matter | Cannot be proxied |
| final or private methods | Not on the interface anyway | Never advised |
| Spring Boot default | No | Yes, proxy-target-class=true |
3Following a Call Through the Proxy
Here is what happens when a controller calls a transactional method on the proxy.
Notice that the transaction lives entirely in the proxy. The target object, our real OrderService, contains no transaction code at all. That is the point of the design: behaviour is added from outside, at run time, without changing our class. And it is also the root of the next problem.
4Why a Call Inside the Same Class Has No Transaction
Suppose importAll(), which is not transactional, calls place() for each order inside the same class.
4 messages in order. Play them, or step through them one at a time.
The caller enters through the proxy with importAll(). importAll() has no @Transactional, so the proxy simply forwards the call to the target. From then on we are executing inside the target object. The call place(o) is a normal Java call on this, and this is the target, not the proxy. The proxy, the only place that knows how to start a transaction, never sees that call. This is called self-invocation, and it affects every proxy-based annotation, including @Async, @Cacheable and @PreAuthorize.
5The Fixes and How to Choose
The cleanest fix is to move place() into another bean, for example an OrderWriter, and inject it into OrderService. The call then goes through OrderWriter's proxy and the transaction starts. It also makes the design honest: writing one order is a separate transactional operation.
The second fix is to call through the proxy explicitly: inject the bean into itself, usually with @Lazy to avoid a circular reference problem, and call self.place(o); or enable exposeProxy and call ((OrderService) AopContext.currentProxy()).place(o). Both work but hide the trick from readers. A related option is TransactionTemplate, which starts the transaction in code and needs no proxy for that call.
The third fix is AspectJ weaving, which modifies the compiled bytecode of our class, at build time or when the class loads, so the advice runs inside the method itself. Then self-calls, and even private and final methods, are advised. The cost is a more complex build and tooling, so teams usually choose it only when they need it widely.
6Follow-up questions
Why does Spring Boot default to class-based proxies?
Because it avoids surprises: beans can be injected by their concrete class, and adding an interface to a bean does not suddenly change the proxy type and break injection elsewhere. The cost is that final classes and final methods cannot be advised, which matters for languages like Kotlin where classes are final by default, hence the Kotlin all-open compiler plugin for Spring.
Does @Transactional work on a private method?
No, not with proxies. A private method cannot be called from outside the class and cannot be overridden by a subclass, so no proxy can intercept it. Spring 6 allows protected and package-visible methods on class-based proxies, check your version, but public methods are the safe and conventional choice.
Does the proxy apply when one bean calls another bean's method?
Yes. That is the normal case: the call goes to the injected reference, which is the other bean's proxy, so its advice runs. Only calls that stay inside the same object skip it.
7Conclusion
In this answer, we saw that Spring AOP applies @Transactional through proxies. JDK dynamic proxies implement the bean's interfaces, while class-based proxies are generated subclasses, which Spring Boot uses by default and which cannot advise final classes or final methods. The proxy runs the transaction logic and then calls the target, so a call from one method to another inside the target goes through this and skips the advice. Moving the method to another bean is the cleanest fix, with self-injection, AopContext or TransactionTemplate as alternatives and AspectJ weaving for advice that works without proxies.
Open with
Spring applies @Transactional by wrapping the bean in a proxy and injecting the proxy everywhere. There are two kinds. A JDK dynamic proxy is generated with java.lang.reflect.Proxy and implements the bean's interfaces, so it can only be injected by interface type.
Then, if they want more
A class-based or CGLIB-style proxy is a generated subclass of the bean class that overrides its methods, so it works without interfaces, but it can't proxy a final class and it never advises final or private methods. Spring Boot uses class-based proxies by default because proxy-target-class is true.
When a caller invokes a transactional method, the proxy's interceptor begins the transaction, calls the target method, and commits or rolls back. The catch is self-invocation: if importAll in the same class calls place, that's this.place, a plain call on the target object, so it never goes through the proxy and no transaction starts.
The same applies to @Async, @Cacheable and security annotations. The cleanest fix is to move place into a separate bean so the call crosses a proxy. Alternatives are injecting the bean into itself lazily, AopContext.currentProxy with exposeProxy, a TransactionTemplate, or AspectJ weaving if we need advice on self-calls generally.
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 official documentation for the version you use. 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 "Spring AOP uses proxies for @Transactional. Explain JDK dynamic proxies versus...": https://scrivio-interview-library.netlify.app/java/structural-patterns/3d2772204c/