@EnableAspectJAutoProxy is the Spring annotation that activates @Aspect-based aspect-oriented programming in a Java-configured application. Put it on a @Configuration class and Spring starts applying your aspects' advice to matching beans, so cross-cutting code such as logging, auditing or security checks stays out of your business classes.
This guide focuses on the practical setup: dependencies, defining an aspect with a reusable pointcut, ordering several aspects, and the XML equivalent you may meet in older projects.
Some logic belongs to many classes at once: logging, metrics, retries, permission checks. Copying it into every method scatters it across the code base. AOP moves it into an aspect, a class that declares what to run (advice) and where to run it (pointcut).
The core vocabulary:
| Term | Meaning |
|---|---|
| Aspect | A class annotated with @Aspect that holds advice |
| Advice | Code that runs at a join point: @Before, @AfterReturning, @AfterThrowing, @After, @Around |
| Pointcut | An expression that selects which method executions get advice |
| Join point | A point in execution; in Spring AOP always a method execution on a Spring bean |
@EnableAspectJAutoProxy is what connects these pieces to the container. It does not define aspects itself.
Outside Spring Boot, add spring-aop (pulled in by spring-context) and org.aspectj:aspectjweaver. Then enable the feature:
The older XML equivalent is the aop:aspectj-autoproxy element in the aop namespace. In Spring Boot the AOP starter brings the weaver and AopAutoConfiguration enables proxying for you, so the annotation is rarely written by hand.
The named @Pointcut method is a label you reuse across advice methods, so you change the target packages in one place. The aspect must itself be a Spring bean (@Component or a @Bean method); a bare @Aspect class is not picked up.
@Before runs before the method and cannot stop it unless it throws.@AfterReturning runs after a normal return and can read the result.@AfterThrowing runs when the method throws.@After runs always, like a finally block.@Around wraps the call, decides whether to call proceed(), and can change arguments or the result. Use the least powerful advice that does the job.When multiple aspects apply to one method, set their order with @Order or by implementing Ordered. A lower value has higher precedence: its @Around code starts first and finishes last.
@EnableAspectJAutoProxy has two attributes. proxyTargetClass forces CGLIB class-based proxies, and exposeProxy makes the current proxy available through AopContext.currentProxy().
Typical reasons an aspect does not run:
private, static or final (with class-based proxies).@EnableAspectJAutoProxy required to use @Aspect?Outside Spring Boot, yes. In Spring Boot the same effect comes from auto-configuration when the AOP starter is present.
It must be on a class processed as configuration, such as @Configuration or a @SpringBootApplication class. One occurrence per context is enough.
@Transactional and @Async?Those features use their own proxy mechanisms, but they share the same auto-proxy infrastructure. Self-invocation limits apply to all of them.
See the Spring Framework AOP reference.
@EnableAspectJAutoProxy activates annotation-driven Spring AOP. Define aspects as beans, share pointcuts through named @Pointcut methods, order aspects with @Order, and let Spring Boot enable the feature for you when possible.