The @Before annotation in Spring AOP marks a method as before advice: a piece of cross-cutting code that runs before a matched method (the join point) executes. It is used for concerns such as logging, security checks, input validation and auditing, and it cannot change the return value of the target method. If the advice throws an exception, the target method is not called.
In Spring AOP a join point is always a method execution on a Spring bean. A pointcut expression decides which methods match, and the @Before advice runs just ahead of each of them.
@Aspect@Before advice@Before can and cannot do@Before compared with the other advice types@AspectSpring AOP is proxy based. You need the AOP starter on the classpath, an aspect class that is itself a Spring bean, and @EnableAspectJAutoProxy (Spring Boot enables it automatically when spring-boot-starter-aop is present).
An aspect is a class annotated with @Aspect and registered as a bean, for example with @Component. The @Aspect annotation alone does not make it a bean.
@Before adviceWhen placeOrder("book") is called through the Spring-managed bean, the output is:
The advice method receives an optional JoinPoint as its first parameter. It gives you the method signature, the arguments and the target object.
| Expression | Matches |
|---|---|
execution(* com.example.service.*.*(..)) | Any method of any class directly in the service package |
execution(* com.example..*.*(..)) | Any method in the package and its sub-packages |
within(com.example.service.OrderService) | All methods of one class |
@annotation(com.example.Audited) | Methods carrying a custom annotation |
bean(*Service) | Beans whose name ends with Service (Spring-specific) |
For reuse, declare a named pointcut and reference it from several advices:
Binding the annotation as a parameter (audited above) gives the advice direct access to the annotation attributes.
@Before can and cannot dojoinPoint.getArgs(). It can mutate mutable argument objects, but it cannot replace the argument values themselves. Use @Around for that.@Before compared with the other advice types| Advice | Runs | Can change result | Can stop the call |
|---|---|---|---|
@Before | Before the method | No | Only by throwing |
@After | After, always (like finally) | No | No |
@AfterReturning | After a normal return | Can see the value | No |
@AfterThrowing | After an exception | No | No |
@Around | Wraps the call | Yes | Yes |
When several advices apply to the same join point, control the order with @Order on the aspect classes. A lower value runs first on the way in.
Spring AOP wraps beans in JDK dynamic or CGLIB proxies, which leads to a few well-known traps:
methodA() calls this.methodB() inside the same class, the call bypasses the proxy and the @Before advice on methodB() does not run.final classes and final methods cannot be proxied with CGLIB.@Before advice modify the arguments of the target method?Not the argument references. It can change the state of mutable objects passed in, but to replace an argument you need @Around and call proceed(Object[] args) with new values.
@Before advice throws an exception?The exception goes back to the caller and the target method is not executed. Other advices of type @After and @AfterThrowing that match the join point still run.
@Before advice not running?The usual causes are: the aspect is not a Spring bean, the target is called from within its own class (self-invocation), the pointcut expression does not match, or spring-boot-starter-aop is missing from the classpath.
@Before and a servlet filter or interceptor?Filters and interceptors work at the web request level. @Before advice works at the method level and applies to any Spring bean, including services that are not reached through HTTP.
@Before runs custom code ahead of every method matched by a pointcut, which keeps logging, security and validation out of your business classes. Declare an @Aspect bean, write a precise pointcut expression, and remember that the advice cannot alter the return value and only applies to calls that pass through the Spring proxy. Use @Around when you need more control.