Quick revision
The short answer
I would choose AspectJ weaving when advice must reach places a Spring proxy cannot: self-calls, private methods, constructors, field access, or objects Spring does not manage, such as JPA entities; or when proxy overhead or proxy identity problems really matter. Against that I weigh the costs: build plugins or agents, slower builds, weaker IDE and tooling support, harder debugging, and the need for identical weaving in every environment, including tests. For most services the proxy model, combined with good design such as extracting beans and explicit boundaries, is enough.
What to remember
In this answer, we saw that AspectJ weaving is justified when advice must reach self-calls, private methods, constructors or objects Spring does not manage, such as entities with @Configurable, or when proxy overhead or identity truly matter. Its costs are build plugins or agents, slower builds and startup, weaker tooling and harder debugging, and above all the need for identical weaving in every environment, including tests. A short decision procedure shows that most services stay with proxies plus good design.
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 choose AspectJ weaving when advice must reach places a Spring proxy cannot: self-calls, private methods, constructors, field access, or objects Spring does not manage, such as JPA entities; or when proxy overhead or proxy identity problems really matter. Against that I weigh the costs: build plugins or agents, slower builds, weaker IDE and tooling support, harder debugging, and the need for identical weaving in every environment, including tests. For most services the proxy model, combined with good design such as extracting beans and explicit boundaries, is enough.
Why this question is asked
AspectJ is often presented as the more powerful and therefore better form of AOP. Power is not free, though, and a production system has to be built, tested, debugged and upgraded by many people for years. Interviewers ask this design question to see whether we choose tools by need rather than by capability. In this answer, we will recap what each model can and cannot do, go through the situations that justify AspectJ with concrete examples, lay out the operating costs, look at the consistency requirement across environments, and finish with a decision procedure and why the proxy model usually wins.
1A Quick Recap of the Two Models
Spring AOP attaches advice through proxies, generated objects that wrap Spring beans at runtime. A proxy can only intercept calls that come through it to methods it can override, so it advises public (and, for some features since Spring 6, protected and package-private) method executions on beans, called from other beans. AspectJ instead changes the bytecode of the classes, at compile time or at load time. The advice becomes part of the class, so it applies to any join point the pointcut selects, wherever the call comes from and whoever created the object.
2When the Proxy Cannot Reach: The Strongest Reasons
The first and strongest reason for AspectJ is a requirement the proxy model cannot meet. Self-calls are one: if a transactional or cached method must work when called from inside the same class and restructuring is not acceptable, weaving makes it work. Private methods and constructors are another: an aspect that must validate every constructed instance of a value class, or record every state change through a private setter, needs construction or field join points that only AspectJ has.
The most common real case is objects not managed by Spring. JPA entities are created by Hibernate or with new, not by the container, so they never get a proxy. Spring's @Configurable, woven with spring-aspects, lets the container inject dependencies into such objects when they are constructed. Domain-driven designs that want rich entities with injected services, or auditing of every field change on entities, have no clean proxy-based solution.
Notice that without weaving the code compiles and fails only at runtime. That is typical: AspectJ features that are not woven fail silently or late, which is the theme of the costs below.
3When Proxy Overhead or Identity Matters
The second reason is the proxy itself. Each advised call through a proxy goes through an interceptor chain, which is cheap, well under a microsecond, but measurable in a very hot path with millions of calls per second, where woven advice inlined into the method is cheaper. Proxies also change identity: the injected object is not the target, so this inside the bean differs from the reference other beans hold, getClass() returns a generated class, and libraries that use reflection or equality on the object can behave differently. CGLIB proxies cannot be created for final classes, and JDK proxies break injection by concrete class. In systems where these effects cause real problems, for example a framework that registers objects by identity, AspectJ removes them.
Be careful here, though. Measure before claiming proxy overhead matters; in a typical service, the advice's own work, such as a database transaction, costs a thousand times more than the proxy dispatch.
4The Costs of Running AspectJ
Every one of these benefits comes with an operating cost. Compile-time weaving puts the AspectJ compiler into the build through a Maven or Gradle plugin, which slows compilation and must keep up with new Java versions; a project on Java 25 needs an AspectJ release that supports it. Load-time weaving needs a Java agent on every JVM, which must be added to startup scripts, container images, IDE run configurations and test runners, and it slows startup. IDE support for aspects is thinner than for plain Java: navigation, refactoring and incremental compilation may not understand woven code. Debugging is harder, because stack traces contain generated method names and the advice runs inside methods without any visible call in the source.
There is also a team cost. Few developers know AspectJ deeply, so the people who can diagnose a weaving problem become a bottleneck. Upgrades of Spring, the JDK and AspectJ must be coordinated.
| Concern | Spring proxies | AspectJ weaving |
|---|---|---|
| Self-calls, private methods, constructors | not advised | advised |
| Objects not created by Spring | not advised | advised, e.g. @Configurable |
| Per-call overhead | interceptor chain, small | inlined, smaller |
| Build and startup | unchanged | ajc plugin or agent, slower |
| IDE and debugging | standard | weaker support, harder traces |
| Consistency risk | low | every environment must weave the same way |
5Weaving Must Be the Same Everywhere
The cost that bites hardest is consistency. If production is woven and unit tests or local runs are not, the tests exercise different code from the one that ships: a transaction that works in production may be missing in tests, or the other way round. With load-time weaving this happens easily, when someone runs tests from the IDE without the agent. With compile-time weaving it happens when one module is built without the plugin. The failure mode is almost always silent: the advice simply does not run.
So a production decision for AspectJ must include making weaving part of every build and run path, and verifying it. A startup check that fails fast if an expected aspect is not woven, for example by asserting that a known woven class reports the advice, or a test that checks a self-call is transactional, turns silent misconfiguration into a visible failure.
6A Decision Procedure
Putting it together, the decision follows a short sequence of questions, and most systems stop at the first one.
- Does advice need self-calls, private methods, constructors or non-Spring objects?YesNext questionNoStay with proxies
- Can a design change avoid it?extract a bean,
TransactionTemplate, explicit codeYesStay with proxiesNoNext question - Can the platform support weaving in every build, test and runtime?YesAdopt AspectJ with checksNoStay with proxies and revisit later
3 steps in order. Play them, or step through them one at a time.
Most systems stop at the first question.
For most services the answer is no at the first step: the advice we need, transactions, caching, security, retries and metrics, belongs at service boundaries where proxies work, and the occasional self-call is better solved by extracting a bean or using TransactionTemplate. AspectJ is the right tool for a minority of systems with a real, lasting requirement, and when we choose it, we choose it for the whole system with platform ownership, not for one module.
7Follow-up questions
Is AspectJ faster than Spring AOP?
Per call, usually yes, because the advice is woven into the method instead of reached through a proxy and an interceptor chain. In most services the difference is negligible compared with the advice's own work, so performance alone is rarely a good reason to switch.
Can you use AspectJ for only one feature?
Yes. For example, transactions can use mode = AdviceMode.ASPECTJ while everything else stays proxy-based, or only @Configurable entities can be woven. Mixing works but makes the system harder to reason about, so it should be documented and tested.
What would make you move back from AspectJ to proxies?
If the requirement that justified it disappeared, for example entities no longer need injected services, or if the weaving setup kept causing environment-specific failures that cost more than the benefit. Removing weaving is a refactor that should be backed by tests for every advised behaviour.
8Conclusion
In this answer, we saw that AspectJ weaving is justified when advice must reach self-calls, private methods, constructors or objects Spring does not manage, such as entities with @Configurable, or when proxy overhead or identity truly matter. Its costs are build plugins or agents, slower builds and startup, weaker tooling and harder debugging, and above all the need for identical weaving in every environment, including tests. A short decision procedure shows that most services stay with proxies plus good design.
Open with
I would choose AspectJ only when there is a requirement the proxy model really cannot meet. The clearest cases are advice that has to apply to self-calls, private methods, constructors or field access, and objects that Spring does not manage, the classic example being JPA entities that need injected services through @Configurable, which only works with weaving of spring-aspects.
Then, if they want more
A second, weaker case is when proxy overhead or proxy identity causes real problems, such as a very hot path or a library that depends on object identity, but I would want measurements before accepting that argument, because the proxy dispatch is tiny compared with what the advice usually does.
Then I weigh the costs: the ajc compiler in the build through a plugin that must support our Java version, or a Java agent on every JVM, slower builds or startup, weaker IDE support, harder debugging with generated code in stack traces, and fewer people who can diagnose problems.
The most important cost is consistency: weaving has to be identical in every environment, including tests and IDE runs, otherwise the advice silently disappears in some of them, so I would add a fail-fast check.
For most services I would stay with proxies, because transactions, caching, security and retries live at service boundaries, and the occasional self-call is solved better by extracting a bean or TransactionTemplate.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 25
- 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 "When would you choose AspectJ weaving over Spring's proxy-based AOP for a production...": https://scrivio-interview-library.netlify.app/spring-boot/aop/64cd29957c/