In Spring AOP, @Before, @After, and @Around are advice annotations that decide when your cross-cutting code runs relative to a matched method. @Before runs ahead of the method, @After runs once the method finishes whether it returned or threw (like a finally block), and @Around wraps the whole call so you control whether, when, and with what arguments the method runs.
Use them to add logging, timing, security checks, or retries without touching business code. The advice is attached to methods through a pointcut expression.
@Before advice@After advice@Around adviceAdd the AOP starter, which brings in Spring AOP and AspectJ's annotations and weaver support. Spring Boot then enables proxy-based AOP automatically, so you do not need @EnableAspectJAutoProxy.
An aspect is a Spring bean annotated with @Aspect. It must also be a bean, usually through @Component.
@Before advice@Before runs before the target method. It cannot stop the method from running or change its return value, but throwing an exception from it prevents the target from being called.
The JoinPoint parameter exposes the method signature, arguments, and the target object. Typical uses are logging, input checks, and authorization checks.
@After advice@After (also called after-finally advice) runs after the method completes, regardless of outcome. It does not receive the return value or the exception.
Use it for cleanup that must always happen, such as clearing a ThreadLocal or releasing a lock. If you need the result or the exception, use @AfterReturning or @AfterThrowing instead, which are the other two advice types.
@Around advice@Around is the most powerful type. The advice method receives a ProceedingJoinPoint and must call proceed() itself to run the target. It must return the value that the caller will see.
Because you control proceed(), @Around can skip the call (caching), call it with different arguments through proceed(Object[]), retry it, wrap exceptions, or replace the return value. Always return the result of proceed() and rethrow exceptions, or you will change application behavior by accident.
| Advice | Runs | Sees result or exception | Can skip the target |
|---|---|---|---|
@Before | Before the method | No | Only by throwing |
@After | After, always | No | No |
@AfterReturning | After a normal return | Result, via returning | No |
@AfterThrowing | After an exception | Exception, via throwing | No |
@Around | Around the call | Yes | Yes |
Prefer the least powerful advice that does the job. @Before and @After are easier to reason about and cannot break the return value, while @Around is the right tool for timing, caching, and retries.
Spring AOP works by wrapping beans in proxies, which has consequences:
@After and @AfterReturning?@After always runs, like finally. @AfterReturning runs only when the method returns normally and can read the returned value.
@EnableAspectJAutoProxy in Spring Boot?No. Spring Boot auto-configures it when the AOP starter is on the classpath. Plain Spring needs the annotation on a configuration class.
The usual causes are a missing @Component on the aspect, a pointcut that does not match, or a self-invoked method that never passes through the proxy.
The difference is negligible for most applications. Choose by what you need to do, not by speed.
@Before runs first and cannot alter the result, @After runs like finally, and @Around wraps the call and controls proceed(), arguments, and return value. Pick the narrowest advice that works, define pointcuts carefully, and remember that Spring AOP is proxy-based, so self-invocation and non-bean objects are not intercepted.