Quick revision
The short answer
Compile-time weaving uses the AspectJ compiler, ajc, usually through an AspectJ Maven or Gradle plugin, to weave aspects into classes while compiling them. Post-compile or binary weaving applies aspects to existing class files or jars after compilation, and load-time weaving modifies classes as the JVM loads them, using a Java agent. Spring supports load-time weaving with @EnableLoadTimeWeaving, a weaver agent and aspects listed in META-INF/aop.xml, and @EnableTransactionManagement(mode = AdviceMode.ASPECTJ) switches transactions to the ready-made aspects in spring-aspects, which then need to be woven by one of these methods.
What to remember
In this answer, we saw the three times AspectJ can weave. Compile-time weaving uses ajc through a build plugin, post-compile weaving applies aspects to existing class files or jars, and load-time weaving rewrites classes as they load using a Java agent configured by META-INF/aop.xml. Spring supports load-time weaving with @EnableLoadTimeWeaving and its spring-instrument agent, and AdviceMode.ASPECTJ switches transactions, caching and async to the spring-aspects aspects, which must then be woven by one of these methods.
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.
Compile-time weaving uses the AspectJ compiler, ajc, usually through an AspectJ Maven or Gradle plugin, to weave aspects into classes while compiling them. Post-compile or binary weaving applies aspects to existing class files or jars after compilation, and load-time weaving modifies classes as the JVM loads them, using a Java agent. Spring supports load-time weaving with @EnableLoadTimeWeaving, a weaver agent and aspects listed in META-INF/aop.xml, and @EnableTransactionManagement(mode = AdviceMode.ASPECTJ) switches transactions to the ready-made aspects in spring-aspects, which then need to be woven by one of these methods.
Why this question is asked
Most Spring applications never leave the proxy model, so weaving can sound like an exotic topic. But it is the answer whenever a proxy is not enough: self-calls, private methods, objects Spring did not create. Interviewers ask about the three weaving times because each has very different consequences for the build, the runtime and the tests, and choosing between them is a real engineering decision. In this answer, we will recap what weaving is, look at each of the three kinds with an example, see how Spring's load-time weaving support fits in, and finish with the AspectJ mode of Spring's own annotations.
1A Quick Recap of Weaving
Weaving is connecting an aspect's advice to the join points its pointcuts select. Spring AOP weaves at runtime with proxies: it wraps beans in generated objects and never changes our classes. AspectJ weaves by changing the bytecode of the classes themselves, inserting calls to the advice at each matching join point. Once a class is woven, the advice is part of it, so it applies to self-calls, private methods, constructors, field access and objects created with new. The question is when that bytecode change happens: during compilation, after it, or while the program loads.
| Kind | When | Tooling | Typical use |
|---|---|---|---|
| Compile-time | while compiling our sources | ajc via a Maven or Gradle plugin | our own code, fastest at runtime |
| Post-compile (binary) | after compiling, on class files or jars | ajc with in-path jars | weaving third-party jars |
| Load-time | as the JVM loads each class | a Java agent plus META-INF/aop.xml | no build change, per-environment control |
2Compile-Time Weaving
With compile-time weaving, the AspectJ compiler ajc replaces or follows javac for our sources. It understands both Java and aspects, compiles them together, and writes woven class files. In practice we use a build plugin, such as an AspectJ Maven plugin or the Freefair AspectJ plugin for Gradle, and declare spring-aspects or our own aspect library as an aspect library to weave from.
The advantage is that the woven classes are ready at startup: no agent, no extra class loading work, and the result is the same in every environment because it is in the jar. The costs are a slower build, a compiler that must support our Java version, and IDE and tooling that need to understand ajc, which is where most of the day-to-day friction appears.
3Post-Compile (Binary) Weaving
Post-compile weaving, also called binary weaving, runs ajc on class files or jars that are already compiled. We give it an in-path of jars or directories and the aspects to apply, and it writes woven copies. It is useful when we want to apply aspects to code we do not compile ourselves, such as a third-party library, or when another tool, for example the Kotlin compiler or a code generator, has to produce the classes first. In build plugins it often appears as an option on the same AspectJ plugin, weaving dependencies listed as weave dependencies. Its cost is similar to compile-time weaving, plus the fact that we are now shipping modified copies of other people's jars, which must be managed carefully on upgrades.
4Load-Time Weaving
Load-time weaving, LTW, weaves classes as the JVM loads them. A Java agent, started with the -javaagent option, registers a class file transformer that receives each class's bytecode before it is defined and returns a woven version. Which aspects apply, and to which packages, is configured in META-INF/aop.xml.
The include element matters: limiting weaving to our own packages keeps startup fast and avoids weaving JDK or library classes. LTW needs no change to the build, and different environments can use different aop.xml files. The costs are slower startup, because classes are woven while loading, and the requirement that every environment, including tests and IDE run configurations, starts with the agent; forgetting it means the aspects silently do nothing.
5How Spring Supports Load-Time Weaving
Spring adds its own layer on top. @EnableLoadTimeWeaving on a configuration class registers a LoadTimeWeaver bean, which plugs the AspectJ weaver into the class loading of the application. In a standalone application, this uses Spring's own agent, started with -javaagent:spring-instrument.jar, so that Spring can add the transformer through the JVM's instrumentation API; some application servers provide their own weaving class loaders instead. With aspectjWeaving enabled, the default when an aop.xml is present, Spring reads META-INF/aop.xml and weaves the listed aspects.
- JVM starts with the agentspring-instrument or aspectjweaver
- Spring context starts
@EnableLoadTimeWeavingregisters aLoadTimeWeaver - Weaver reads META-INF/aop.xmlaspects and packages to include
- Is the class already loaded?YesNot woven: advice will not applyNoWoven as it loads
- Advice runs inside the classself-calls and private methods included
5 steps in order. Play them, or step through them one at a time.
There is a catch with timing: the weaver can only change classes that have not been loaded yet. Classes loaded before the LoadTimeWeaver is in place are not woven, which is one reason the plain AspectJ agent, active from the first class, is often simpler and more predictable.
6AspectJ Mode for Spring's Own Annotations
Spring ships AspectJ versions of its infrastructure aspects in the spring-aspects module, such as AnnotationTransactionAspect for @Transactional, AnnotationCacheAspect for caching and AnnotationAsyncExecutionAspect for @Async. Setting mode = AdviceMode.ASPECTJ on @EnableTransactionManagement, @EnableCaching or @EnableAsync tells Spring not to create proxies for those features and to configure these aspects instead. The aspects must then be woven, either at compile time by listing spring-aspects as an aspect library, or at load time. If neither happens, nothing is advised at all, which is a common mistake when switching modes.
7Follow-up questions
Which weaving should a team choose first?
Compile-time weaving if the build can support it, because the result is fixed in the artifact and behaves the same in every environment. Load-time weaving if changing the build is hard or if weaving must be controlled per environment, accepting the agent requirement everywhere.
Does load-time weaving work with Spring Boot's fat jars?
It can, with the agent on the command line, but class loading in nested jars and the timing of when classes load need care. Many teams prefer compile-time weaving with Spring Boot for that reason. Check your Boot version's documentation for current guidance.
Can I see what was woven?
Yes. The -showWeaveInfo and -verbose weaver options print each advised join point, at build time for ajc and at startup for load-time weaving. That output is the quickest way to confirm the aspects really apply.
8Conclusion
In this answer, we saw the three times AspectJ can weave. Compile-time weaving uses ajc through a build plugin, post-compile weaving applies aspects to existing class files or jars, and load-time weaving rewrites classes as they load using a Java agent configured by META-INF/aop.xml. Spring supports load-time weaving with @EnableLoadTimeWeaving and its spring-instrument agent, and AdviceMode.ASPECTJ switches transactions, caching and async to the spring-aspects aspects, which must then be woven by one of these methods.
Open with
AspectJ can weave at three different times. Compile-time weaving uses the AspectJ compiler, ajc, normally through a Maven or Gradle AspectJ plugin, so our sources and aspects are compiled together and the jar already contains woven classes.
Then, if they want more
That gives the most predictable runtime and no agent, at the cost of a slower build and tooling that has to understand ajc. Post-compile, or binary, weaving runs ajc over class files or jars that already exist, which is how you weave third-party libraries or classes produced by another compiler.
Load-time weaving changes classes as the JVM loads them: a Java agent registers a transformer, and META-INF/aop.xml says which aspects apply and which packages to include. It needs no build changes, but every environment, including tests, has to start with the agent, and startup is slower.
Spring supports load-time weaving with @EnableLoadTimeWeaving, which registers a LoadTimeWeaver and reads aop.xml, usually with the spring-instrument agent in a standalone app, and it can only weave classes that load after the weaver is in place. Separately, @EnableTransactionManagement with mode AdviceMode.ASPECTJ, and the same option on caching and async, tells Spring to use the aspects in spring-aspects instead of proxies.
Those aspects still have to be woven at compile time or load time, otherwise nothing is advised.
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 "What are compile-time, post-compile and load-time weaving in AspectJ, and how does...": https://scrivio-interview-library.netlify.app/spring-boot/aop/3b3c99152d/