Quick revision
The short answer
I would not push teams to abandon proxy-based annotations, because @Transactional, @Cacheable and their peers are concise, widely understood and well tested, and that has real value. Instead I would set a default: use the annotations at clear service boundaries, and use explicit code such as TransactionTemplate or plain decorators where the control flow is complex or the proxy's limits bite. I would invest in training and automated checks for the known pitfalls rather than banning annotations, and keep watching how AOT processing and build-time code generation shift the balance.
What to remember
In this answer, we weighed proxy-based annotations against explicit code. Annotations are concise, consistent and universally understood, while explicit alternatives such as TransactionTemplate and decorators make control flow visible but add code and new risks. A sensible default is annotations at service boundaries and explicit code for loops, self-calls and complex logic, supported by training and automated checks rather than bans. As AOT processing and build-time generation mature, the costs shift, so the policy should be reviewed with evidence.
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.
I would not push teams to abandon proxy-based annotations, because @Transactional, @Cacheable and their peers are concise, widely understood and well tested, and that has real value. Instead I would set a default: use the annotations at clear service boundaries, and use explicit code such as TransactionTemplate or plain decorators where the control flow is complex or the proxy's limits bite. I would invest in training and automated checks for the known pitfalls rather than banning annotations, and keep watching how AOT processing and build-time code generation shift the balance.
Why this question is asked
Every senior engineer who has worked with Spring for a while has a collection of proxy stories: the transaction that never started because of a self-call, the final class that silently lost its caching, the bean that failed to inject because it was a JDK proxy. It is tempting to conclude that the magic is the problem and that explicit code would be safer. Interviewers ask this question at the most senior level because it has no single right answer; they want to hear how we weigh readability, safety, team skill and the direction of the platform. In this answer, we will recap where the confusion comes from, look honestly at what each approach costs, set out a default policy, explain how to support it with training and checks, and look at how AOT changes the picture.
1A Quick Recap of Where the Confusion Comes From
Annotations like @Transactional work through proxies, objects that Spring generates at runtime and puts in front of our beans. The proxy runs the extra behaviour and then calls our code. All the recurring confusion comes from that one mechanism. A call on this from inside the bean never reaches the proxy, so the behaviour is skipped. A CGLIB proxy is a subclass, so final classes cannot be proxied and final methods are not intercepted. A JDK proxy implements only interfaces, so injecting the concrete class fails. None of these is a bug in Spring; they are the documented consequences of runtime proxies. But they are invisible in the code, which is exactly why they keep surprising people.
2What the Annotations Give Us
The value of the annotations is real and easy to underrate. @Transactional on a service method states the intent in one word, and every Spring developer in the industry knows what it means. It is consistent: propagation, rollback rules and isolation are handled the same way everywhere. It is heavily tested by the framework and by millions of applications. And it keeps business code free of plumbing, so the method body says what the business does.
If we replaced every annotation with explicit code across hundreds of services, we would add thousands of lines of boilerplate, and each of those lines is a new chance to get commit and rollback handling wrong. Explicit is not automatically safer; it moves the risk from the framework into our own code.
3What Explicit Code Gives Us
Explicit alternatives make the control flow visible. TransactionTemplate wraps a block of code in a transaction right where we can see it, and it works regardless of self-calls or visibility. A decorator, a plain class that implements the same interface and adds behaviour before delegating, makes caching or retries visible in the wiring.
| Aspect | Proxy annotations | Explicit code |
|---|---|---|
| Amount of code | one line | more lines in every place |
| Visibility of control flow | hidden in the proxy | visible where it happens |
| Self-calls, private methods | not advised | work as written |
| Familiarity across the industry | very high | high for TransactionTemplate, varies for decorators |
| Risk of hand-written mistakes | low, framework-tested | higher, our own code |
| Best fit | simple service entry points | loops, partial failures, complex boundaries |
Notice that the explicit version is the better tool in that loop: it gives one transaction per item and lets us handle a failed item and continue, which an annotation on a self-called method could not do at all. But for the single approve() entry point, the annotation is clearer. Each approach wins in a different place.
4A Sensible Default Policy
So the policy I would set is a default with clear exceptions. Use proxy-based annotations at service boundaries: the public methods that other beans call, where the proxy is guaranteed to be in the path. Use explicit code when the boundary is inside a method, such as per-item transactions in a batch, when the behaviour must apply to a self-call, or when the logic of retries, fallbacks or rollbacks is complicated enough that a reader needs to see it.
This keeps the benefit of the annotations in the 80 percent of cases where they are simplest, and removes them from the cases that produce most of the incidents. It also gives code reviewers a concrete rule to apply instead of a matter of taste.
5Training and Checks Instead of Bans
A ban on annotations would be hard to enforce, unpopular, and would fight the entire Spring ecosystem, including Spring Security, Spring Data and Spring Cloud, which all rely on the same proxy mechanism. It is more effective to make the pitfalls well known and mechanically detectable. Onboarding includes a short, concrete session on the proxy model with the three classic failures. The shared build includes checks: IDE inspections and static analysis that flag @Transactional on private or final methods, ArchUnit rules against final classes in Spring stereotypes, and where feasible a rule against calling annotated methods on this. Integration tests verify behaviour that matters, such as asserting that a failure really rolls back.
We then measure: if proxy-related defects stay frequent after a few quarters, the policy for explicit code can widen. The decision stays tied to evidence rather than to fashion.
6How AOT and Build-Time Generation Change the Trade-Offs
The platform is also moving. Spring's AOT engine, used for GraalVM native images and for faster JVM startup, analyses the application at build time and generates code for bean definitions, and generates proxy classes ahead of time instead of at runtime, because a native image cannot create new classes while running. Projects such as Micronaut and Quarkus go further and resolve much of their dependency injection and interception at build time. Work in the JDK, such as Project Leyden, aims at earlier, build-time decisions too.
For our trade-off this means two things. The runtime cost of proxies, mostly at startup, matters less as generation moves to build time. And build-time analysis creates an opportunity to detect pitfalls before deployment, for example a self-call to an advised method. The semantics of today's proxies stay the same, so the policy above still holds, but I would review it as these tools mature and be ready to lean more on generated, inspectable code.
7Follow-up questions
Would you ever recommend AspectJ weaving to remove the pitfalls?
Only for a platform team that can support it properly. AspectJ weaving removes the self-call and private-method limits, but it adds a compiler or agent to every build, test and runtime environment. For most organisations, the default policy plus checks costs less than running AspectJ everywhere.
How do you handle a team that wants to ban annotations anyway?
I would ask for their incident data and look at it together. If their problems come from a specific pattern, such as batch jobs with self-calls, the fix is explicit code in that pattern, not a ban everywhere. If the data really shows that annotations hurt them more than they help, a team-level exception with clear reasons is acceptable.
Is TransactionTemplate really more explicit, or just different?
It is more explicit about where the transaction starts and ends, because the boundary is a block in the code. It still relies on Spring's transaction manager, so it is not less Spring. Its main benefit is that it does not depend on the call path through a proxy.
8Conclusion
In this answer, we weighed proxy-based annotations against explicit code. Annotations are concise, consistent and universally understood, while explicit alternatives such as TransactionTemplate and decorators make control flow visible but add code and new risks. A sensible default is annotations at service boundaries and explicit code for loops, self-calls and complex logic, supported by training and automated checks rather than bans. As AOT processing and build-time generation mature, the costs shift, so the policy should be reviewed with evidence.
Open with
I would not push teams away from proxy annotations as a rule. @Transactional and friends are concise, they state intent in one word, every Spring developer understands them, and they are tested far more than any code we would write by hand.
Then, if they want more
Replacing them everywhere would add a lot of boilerplate where we can get commit and rollback handling wrong. But the confusion is real, and it all comes from one mechanism: runtime proxies that only see calls coming through them and can only intercept overridable methods. So I would set a default.
Annotations at service boundaries, the public methods other beans call, where the proxy is definitely in the path.
Explicit code, such as TransactionTemplate or a plain decorator, when the boundary is inside a method, like one transaction per item in a batch, when a self-call needs the behaviour, or when retry and rollback logic is complex enough that a reader should see it.
Then I would invest in making pitfalls hard to ship: onboarding on the proxy model, IDE inspections and ArchUnit rules for final classes and private annotated methods, and integration tests that assert rollback behaviour. Banning annotations would fight the whole ecosystem, since Spring Security and Spring Data rely on the same proxies.
Finally, I would watch AOT and build-time generation, which move proxy creation to the build and could let us detect these pitfalls before deployment, and revisit the policy as that matures.
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.
- Official documentation
- Spring Framework reference, Core technologies
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 "Framework features that rely on runtime proxies cause recurring confusion...": https://scrivio-interview-library.netlify.app/spring-boot/aop/e16175c872/