@Aspect is the AspectJ annotation that marks a class as an aspect, a module that holds cross-cutting logic such as logging, security checks, caching or transaction handling. Spring AOP detects classes annotated with @Aspect and applies the advice they contain to matching beans through proxies, so the cross-cutting code lives in one place instead of being copied into every service.
@Aspect alone does not make the class a Spring bean. You must also register it, usually with @Component.
AOP separates concerns that cut across many classes from the core business logic. The vocabulary is small.
| Term | Meaning |
|---|---|
| Aspect | A class with cross-cutting logic, marked with @Aspect |
| Join point | A point in execution. In Spring AOP this is always a method execution |
| Pointcut | An expression selecting which join points to advise |
| Advice | The code that runs at the join point: @Before, @After, @AfterReturning, @AfterThrowing, @Around |
| Weaving | Linking aspects with target objects. Spring AOP does this at runtime with proxies |
@Aspect tells the framework that the annotated class contains pointcuts and advice methods that it should process.
In Spring Boot, add the starter and AOP is configured automatically.
Outside Boot, add @EnableAspectJAutoProxy to a @Configuration class.
Spring creates a proxy for every bean whose methods match serviceMethods(). When you call productService.findAll(), the proxy runs the advice and then delegates to the real method.
The execution designator is the one you will use most. Other useful designators are listed here.
| Expression | Matches |
|---|---|
execution(* com.example..*Service.*(..)) | Any method of classes ending in Service |
within(com.example.shop.web..*) | Any method in a package tree |
@annotation(com.example.Audited) | Methods carrying a custom annotation |
@within(org.springframework.stereotype.Repository) | Methods in classes annotated with @Repository |
bean(*Service) | Beans whose name ends with Service (Spring specific) |
Named @Pointcut methods can be combined with &&, || and !, which keeps expressions readable and reusable.
When more than one aspect applies to the same method, use @Order on the aspect class. A lower value runs first on the way in and last on the way out.
Yes in practice. @Aspect is not a stereotype annotation, so Spring will not pick the class up by scanning unless it is also a bean through @Component or a @Bean method.
Spring AOP is proxy-based, runtime only and limited to method execution on beans. AspectJ weaves bytecode and supports more join points, at the cost of extra build or agent setup.
Yes. A Spring-managed aspect supports constructor injection like any other bean.
Use the narrowest one that works. @Around is the most powerful but needs more care because you must call proceed() and return its result.
@Aspect declares a class as an aspect that holds pointcuts and advice for cross-cutting concerns. In Spring it must also be a bean, and it works through proxies on method calls. Keep pointcuts precise, order aspects with @Order, and use AspectJ weaving only when proxy-based AOP is not enough.