Quick revision
The short answer
The lesson of the move from hand-written decorators to run-time proxies and now to build-time generation is that the mechanism for cross-cutting concerns keeps changing, while the need to understand behaviour from the code stays. For systems meant to live ten years, I would use framework proxies only for well-understood concerns such as transactions and security, keep custom aspects rare and documented, prefer explicit composition such as decorators or functional wrappers for application-specific behaviour, and design so that the system would still work if the proxying mechanism changed.
What to remember
In this answer, we followed cross-cutting concerns from hand-written decorators through run-time proxies to build-time generation. Run-time proxies hide transactions, retries and security from readers and cause defects like self-invocation; AOT and native images favour build-time generation, which needs metadata and limits dynamic tricks. Explicit composition is readable and portable across runtime models. So for long-lived systems we use framework proxies for well-understood concerns, keep custom aspects rare and documented, and prefer designs that would still work if the proxying mechanism changed.
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.
The lesson of the move from hand-written decorators to run-time proxies and now to build-time generation is that the mechanism for cross-cutting concerns keeps changing, while the need to understand behaviour from the code stays. For systems meant to live ten years, I would use framework proxies only for well-understood concerns such as transactions and security, keep custom aspects rare and documented, prefer explicit composition such as decorators or functional wrappers for application-specific behaviour, and design so that the system would still work if the proxying mechanism changed.
Why this question is asked
Cross-cutting concerns, behaviour such as transactions, retries, security checks and metrics that applies across many classes, have been implemented in Java in three very different ways over twenty years. Each wave solved the previous one's pain and created new ones, and systems built in one era have to be maintained in the next. Interviewers ask this at a very senior level to see whether we can extract a design principle that survives the next shift. In this answer, we will recap the three eras, see what run-time proxies hide, look at what AOT and native images demand, compare explicit composition, and then set the guidelines for long-lived systems.
1A Quick Recap of the Three Eras
In the first era, developers wrote decorators and proxies by hand: a TransactionalOrderService that implemented OrderService, began a transaction, called the real service and committed. It was explicit and repetitive. Then frameworks started generating these wrappers at run time. Spring AOP creates a proxy, a generated object that looks like our bean, either with JDK dynamic proxies for interfaces or by generating a subclass with bytecode libraries; it applies advice, the added behaviour, wherever an annotation or pointcut matches. Developers wrote only @Transactional.
The current era moves generation to build time. GraalVM native images and ahead-of-time compilation work under a closed-world assumption, everything that can run must be known when building, and frameworks such as Spring with AOT processing, Quarkus and Micronaut generate the wiring and proxy classes during the build instead of at startup.
| Era | How behaviour is added | Visible in the code we read? | Main cost |
|---|---|---|---|
| Hand-written decorators | A wrapper class per concern | Yes | Repetition |
| Run-time proxies (Spring AOP) | Generated at startup from annotations | Only as an annotation | Hidden behaviour, startup time |
| Build-time generation (AOT) | Generated during the build from metadata | Only as an annotation | Fewer dynamic tricks allowed |
| Explicit composition today | Decorators, lambdas, library wrappers | Yes | Some wiring code |
2What Run-Time Proxies Hide
Run-time proxies made code shorter, but they moved behaviour out of sight. A reader looking at a service method sees an annotation at most, and sometimes not even that, when a pointcut in a different file matches the method by name. The transaction boundary, the retry policy and the security check are not in the code being read.
This hiding causes real defects. The best known is self-invocation: a call from one method to another in the same class goes to this, not to the proxy, so @Transactional or @Cacheable on the second method silently does nothing. Others are advice that does not apply to final methods, ordering between several aspects that nobody chose deliberately, and custom aspects with broad pointcuts that affect code their authors never thought about. These defects are hard to find because the code that causes them is not where the symptom appears.
3What AOT and Native Images Demand
Build-time generation changes the rules again. A native image cannot generate new classes at run time, so every proxy must be known during the build, and reflection must be declared in metadata. Spring's AOT engine handles its own proxies, such as those for @Transactional and @Configuration classes, and frameworks ship metadata for common libraries. But custom tricks, such as an aspect that matches classes discovered at run time, a library that creates proxies on the fly, or reflection on names computed from configuration, need extra hints or do not work at all.
So a system built heavily on clever run-time weaving in 2015 may be expensive to move to a native or AOT runtime in 2026. That is the pattern a senior person should notice: the more our behaviour depends on a specific generation mechanism, the more a change of mechanism costs us.
4Explicit Composition Survives Changes of Mechanism
Explicit composition means the behaviour is added by ordinary code we can read: a decorator class, a higher-order function that wraps a call, or a library API we call directly. Because it is plain Java, it works the same on the JVM, in a native image and in whatever comes next.
The first two forms show the reader what happens and in which order, need no proxy at all, and are trivially testable. The third uses a framework proxy, and that is fine: transactions are a concern every Java developer knows, the framework has handled them for twenty years, and it is supported in every runtime model. The skill is choosing which concerns earn the hidden form.
5Guidelines for Systems That Must Live Ten Years
Here are the guidelines I would set. Framework proxies are the standard for a short list of well-understood concerns: transactions, method security and possibly caching. Their known pitfalls, self-invocation above all, are part of onboarding. Custom aspects are rare: each needs a written reason, a narrow pointcut based on an annotation rather than name patterns, an owner and a test that proves it applies where intended. Application-specific behaviour such as retries for one client, metrics, feature checks and auditing is done by explicit composition, decorators or function wrappers, wired in configuration classes.
The test for any design is a thought experiment: if the proxying mechanism changed tomorrow, from run-time subclassing to build-time generation or to something we do not know yet, would this code still work, and would we know where to look? Code whose behaviour is visible and whose framework usage is limited to mainstream, documented features passes that test.
- New cross-cutting concern
- Mainstream framework concern?transactions, method securityYesUse the framework annotationNoConsider explicit composition
- Specific to one client or feature?retry, metrics, audit for one callYesDecorator or function wrapperNoConsider a custom aspect
- Custom aspect justified?written reason, narrow annotation pointcut, owner, testYesAdd it and document itNoUse explicit composition
4 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Is this an argument against Spring AOP?
No. It is an argument for using it where it is strongest. Spring's proxies for transactions and security are mature and supported in AOT mode. The risk is in custom aspects with broad pointcuts and in relying on run-time tricks that a future runtime may not support.
Don't decorators just bring back the repetition of the first era?
Some, but far less than before, because lambdas and libraries make wrappers short, and we only use them for concerns that are specific enough to deserve explicit code. The few extra lines buy visible behaviour and independence from the proxy mechanism.
How do you find hidden behaviour in an existing system?
List all aspects and their pointcuts, check which beans are proxied at startup, for example with Spring's actuator beans endpoint or debug logging, and write tests that assert the behaviour, such as a transaction being active, at the boundaries that matter.
7Conclusion
In this answer, we followed cross-cutting concerns from hand-written decorators through run-time proxies to build-time generation. Run-time proxies hide transactions, retries and security from readers and cause defects like self-invocation; AOT and native images favour build-time generation, which needs metadata and limits dynamic tricks. Explicit composition is readable and portable across runtime models. So for long-lived systems we use framework proxies for well-understood concerns, keep custom aspects rare and documented, and prefer designs that would still work if the proxying mechanism changed.
Open with
What I take from these twenty years is that the mechanism keeps changing, hand-written decorators, then run-time proxies from Spring AOP and bytecode generation, and now build-time generation for AOT and native images, while what we need, behaviour we can understand from the code, stays the same.
Then, if they want more
Run-time proxies made code shorter but hid transactions, retries and security from the reader, and gave us whole classes of bugs like self-invocation, where a call through this silently skips the advice. AOT and native images work in a closed world, so proxies and reflection have to be known at build time, and custom run-time tricks become expensive or impossible.
So for systems that must live ten years, I'd use framework proxies only for mainstream, well-understood concerns like transactions and method security, and make their pitfalls part of onboarding. Custom aspects should be rare, each with a written reason, a narrow annotation-based pointcut, an owner and a test.
Application-specific concerns, like retries or metrics around one client, I'd do with explicit composition: decorators or function wrappers wired in configuration, which work the same on any runtime. And I'd review designs with one question: if the proxy mechanism changed tomorrow, would this still work and would we know where the behaviour lives?
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 "Over two decades Java moved from hand-written proxies and decorators to frameworks...": https://scrivio-interview-library.netlify.app/java/structural-patterns/d0f3acc98e/