The @Aspect annotation marks a class as an aspect in Spring AOP: a module that holds cross-cutting logic, such as logging, security checks, metrics or transaction handling, together with the rules for where that logic runs. Spring detects the class, reads its advice methods and applies them to matching beans through proxies, so business code stays free of repeated boilerplate.
@Aspect itself comes from AspectJ (org.aspectj.lang.annotation). Spring uses the AspectJ annotation style and pointcut language but implements the weaving with runtime proxies.
An aspect combines two things: advice (the code to run) and pointcuts (the expressions that say where). Annotating a class with @Aspect tells Spring AOP to treat its @Before, @After, @Around and related methods as advice.
@Aspect does not register the class as a bean. You must also make it a Spring bean, usually with @Component, and enable AOP processing.
Add the starter (Spring Boot) or the spring-aspects and AspectJ weaver dependencies:
Spring Boot enables auto-proxying automatically once the starter is present. In plain Spring, add @EnableAspectJAutoProxy to a configuration class.
The @Pointcut method names a reusable expression: every method in the com.example.shop.service package and its subpackages. The three advice methods then run before the call, after a normal return and around the whole call. With @Around you must call proceed() and return its value, or the target method never runs.
| Annotation | Runs | Typical use |
|---|---|---|
@Before | Before the method | Authorisation, input checks |
@AfterReturning | After a normal return | Audit the result |
@AfterThrowing | After an exception | Error reporting |
@After | Always, like finally | Release resources |
@Around | Around the call | Timing, caching, retries |
Spring AOP is proxy based, which has consequences:
this (self-invocation) bypasses the aspect.final cannot be proxied by the CGLIB mechanism.Keep aspects small and focused, and order several aspects with @Order. Do not hide core business rules in an aspect, because that makes the flow hard to follow.
No. The class must also be a Spring bean, for example with @Component, and AOP auto-proxying must be enabled. Spring Boot does this for you when the AOP starter is on the classpath.
Spring AOP uses runtime proxies and supports method execution on Spring beans. AspectJ weaves bytecode at compile or load time and supports more join points, such as field access and constructor calls.
Self-invocation does not pass through the proxy. Move the method to another bean or inject the bean into itself carefully.
A pointcut is an expression that selects join points, meaning the methods where advice should run. For example, execution(* com.example..*(..)) matches every method in a package tree.
@Aspect declares a class that holds cross-cutting advice and pointcuts for Spring AOP. Pair it with @Component, enable auto-proxying, and write advice with @Before, @AfterReturning, @Around and friends. Remember that proxies only intercept external calls on Spring beans, and switch to AspectJ weaving when you need more.