You create an aspect in Spring by writing a class annotated with @Aspect and @Component, enabling AspectJ-style proxying (automatic in Spring Boot, or @EnableAspectJAutoProxy otherwise), and adding advice methods tied to pointcut expressions. Spring then wraps matching beans in proxies that run your advice around their method calls.
An aspect is the right place for cross-cutting concerns such as logging, timing, auditing and security checks that would otherwise be repeated across many classes. This guide builds a working logging and timing aspect for Spring Boot 3.
An aspect packages behaviour that cuts across modules. Without it, every service method repeats the same logging or authorization code. With Spring AOP you write the behaviour once and select the target methods with a pointcut.
| Concept | Role |
|---|---|
| Aspect | The class holding the cross-cutting logic |
| Advice | The code that runs, for example before or around a method |
| Pointcut | The expression selecting which methods are affected |
| Join point | A method execution that a pointcut can match |
Spring AOP is proxy based and supports method execution on Spring beans only. For constructors, fields or non-Spring objects, use full AspectJ weaving.
In Spring Boot, add the starter and Boot configures auto-proxying for you:
In a plain Spring application, enable it explicitly and include aspectjweaver on the classpath:
The aspect below logs every call to the service package and measures its duration.
Two annotations are required: @Aspect marks the class as an aspect, and @Component (or a @Bean definition) registers it in the container. Forgetting @Component is the most common reason an aspect does nothing.
When code calls greetingService.greet("Asha"), the output looks like this:
The caller receives a proxy that runs the advice and then delegates to the real bean. The ordering between different advice types is not something to depend on.
Package-based pointcuts break when you rename packages. A custom annotation makes the intent explicit:
Now only methods marked @Timed are measured. Use @Order on the aspect class when several aspects apply to the same method; lower values run first on the way in.
this.other() inside the same bean skips the proxy, so no advice runs.private and final methods cannot be intercepted.spring-boot-starter-aop is on the classpath.@Around advice for hot paths; each call adds proxy overhead.Yes, or an equivalent bean registration. @Aspect only describes the class; Spring must manage it to apply it.
You use AspectJ's annotations and pointcut language through aspectjweaver, but you do not need the AspectJ compiler or weaver for Spring AOP.
No. Spring AOP intercepts public method calls through proxies. Use AspectJ weaving for other join points.
Add @Order(1), @Order(2) and so on to the aspect classes, or implement Ordered. The lowest value has the highest precedence.
To create an aspect in Spring, write a class with @Aspect and @Component, define pointcuts, and add advice methods such as @Before, @AfterReturning and @Around. Spring Boot enables proxying with the AOP starter. Prefer annotation-based pointcuts and remember that self-invocation is not advised.