@EnableAspectJAutoProxy turns on Spring's support for @Aspect classes. It registers an auto-proxy creator that wraps every bean matched by a pointcut in a proxy, so advice such as @Before, @After and @Around runs when callers invoke those beans. In plain Spring (not Spring Boot) you need it on a @Configuration class for aspects to have any effect.
Spring AOP is proxy-based: the object you inject is not your class but a generated wrapper that runs advice and then delegates to the real bean. Understanding that wrapper explains almost every surprise with the annotation, from self-invocation to final classes.
exposeProxy attribute and self-invocationWhen Spring processes @EnableAspectJAutoProxy, it imports AnnotationAwareAspectJAutoProxyCreator, a BeanPostProcessor. After each bean is created, the processor checks all @Aspect beans and Spring Advisors in the context. If any pointcut matches the bean, the processor returns a proxy instead of the original object.
Without the annotation, @Aspect classes are ordinary beans: they are created, but no advice is ever applied.
You also need the AspectJ annotation and pointcut-parsing library on the classpath, for example org.aspectj:aspectjweaver. Spring uses AspectJ's annotations and pointcut expression language, but it does not use AspectJ's compiler or weaver.
OrderService has no interface, so Spring creates a CGLIB subclass proxy. The last line shows that the injected bean is a generated class, not OrderService itself. The exact timing will differ on your machine.
Spring picks the proxy type per bean:
| Situation | Proxy used |
|---|---|
Bean implements at least one interface and proxyTargetClass = false | JDK dynamic proxy, which implements the same interfaces |
| Bean has no interface | CGLIB subclass proxy |
proxyTargetClass = true | CGLIB subclass proxy for every advised bean |
Set it explicitly when you inject by class instead of by interface:
With a JDK proxy, injecting the concrete class fails with a type mismatch, because the proxy only implements the interface. Spring Boot's AopAutoConfiguration sets class-based proxying (spring.aop.proxy-target-class) to true by default, which avoids that problem.
CGLIB proxies cannot override final classes or final methods. Advice on such methods is silently skipped, and final classes cannot be proxied at all.
exposeProxy attribute and self-invocationAdvice runs only when a call goes through the proxy. A method that calls another method on this bypasses the proxy, so the advice on the second method never fires:
Preferred fixes are to move export() into another bean or to restructure so the call crosses a bean boundary. As a last resort, set exposeProxy = true and fetch the current proxy:
This couples your business code to Spring AOP, so use it sparingly.
Spring Boot auto-configures AOP when the AspectJ weaver is on the classpath, which the AOP starter provides. In a Boot application you normally do not add @EnableAspectJAutoProxy yourself. Add it only when you need to override attributes such as exposeProxy, or when AOP auto-configuration is disabled with spring.aop.auto=false.
static methods are not.new are not proxied.If you need any of these, use full AspectJ with compile-time or load-time weaving, which does not rely on proxies.
@EnableAspectJAutoProxy in Spring Boot?Usually not. Boot enables AOP automatically when the AspectJ weaver is on the classpath. Add the annotation only to override its attributes.
Common causes are a missing @EnableAspectJAutoProxy outside Boot, an aspect that is not a Spring bean, a pointcut that does not match, a self-invocation, or a final method with a CGLIB proxy.
@EnableAspectJAutoProxy and AspectJ weaving?The annotation uses Spring's runtime proxies and AspectJ-style annotations. Real AspectJ weaving changes bytecode at compile or load time and can advise any code, not just Spring beans.
proxyTargetClass do?It forces CGLIB class-based proxies instead of JDK interface-based proxies, so injection by concrete class works.
@EnableAspectJAutoProxy registers the post-processor that wraps matching beans in proxies so @Aspect advice runs. Use proxyTargetClass to control the proxy type, remember that self-invocation bypasses advice, and rely on Spring Boot's auto-configuration instead of the annotation when you can.