How to Use Aspect in Spring Boot: Simple Guide with Example
In Spring Boot, you use
@Aspect to define an aspect class and @Around, @Before, or @After annotations to specify advice methods. Enable aspect support with @EnableAspectJAutoProxy in your configuration. This lets you add reusable behavior like logging or security around your methods.Syntax
An aspect in Spring Boot is a class annotated with @Aspect. Inside it, you define advice methods annotated with @Before, @After, or @Around to run code before, after, or around target methods. You use pointcut expressions to specify which methods the advice applies to.
Enable aspect support by adding @EnableAspectJAutoProxy to a configuration class.
java
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; @Configuration @EnableAspectJAutoProxy public class AppConfig { } @Aspect public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void beforeMethod() { System.out.println("Method is about to execute"); } }
Example
This example shows a Spring Boot application with a simple aspect that logs a message before any method in com.example.service package runs.
java
package com.example.demo; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.stereotype.Component; @SpringBootApplication @EnableAspectJAutoProxy public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } @Component class Service { public void doWork() { System.out.println("Doing work..."); } } @Aspect @Component class LoggingAspect { @Before("execution(* com.example.demo.Service.*(..))") public void logBefore() { System.out.println("[LOG] Method execution starting"); } }
Output
[LOG] Method execution starting
Doing work...
Common Pitfalls
- Not adding
@EnableAspectJAutoProxywill disable aspect processing. - For aspects to work on Spring beans, the target classes must be Spring-managed components (e.g., annotated with
@Component). - Using
newto create objects bypasses Spring proxying, so aspects won't apply. - Pointcut expressions must be precise; a wrong expression means advice won't run.
java
/* Wrong: No @EnableAspectJAutoProxy, aspect won't run */ @Configuration public class Config { } /* Right: Enable aspect support */ @Configuration @EnableAspectJAutoProxy public class Config { }
Quick Reference
| Annotation | Purpose |
|---|---|
| @Aspect | Marks a class as an aspect containing advice |
| @Before | Runs advice before matched method execution |
| @After | Runs advice after matched method execution |
| @Around | Runs advice before and after method execution, can control method call |
| @EnableAspectJAutoProxy | Enables processing of aspects in Spring Boot |
| execution() | Pointcut expression to match method signatures |
Key Takeaways
Use @Aspect and advice annotations like @Before to define aspect behavior.
Enable aspects with @EnableAspectJAutoProxy in your Spring Boot configuration.
Only Spring-managed beans can be advised by aspects; avoid manual object creation.
Write precise pointcut expressions to target the correct methods.
Aspects help add reusable code like logging or security without changing business logic.