@Aspect marks a class as an aspect: a module that packages cross-cutting behaviour such as logging, security checks, auditing or metrics, together with the rules saying where it applies. Spring detects the annotation and builds proxies so your advice runs around matching beans, which keeps that code out of your business classes.
@Aspect itself is an AspectJ annotation. Spring AOP reuses the AspectJ annotation style but implements it with runtime proxies, not AspectJ weaving.
An aspect combines two things: pointcuts that select join points (here, method executions on Spring beans) and advice that runs at them.
| Term | Meaning |
|---|---|
| Aspect | The class annotated with @Aspect |
| Join point | A point in execution; in Spring AOP always a method call |
| Pointcut | An expression selecting join points |
| Advice | Code that runs at a join point: @Before, @After, @AfterReturning, @AfterThrowing, @Around |
| Weaving | Linking aspects to targets; Spring does it at runtime via proxies |
Add the starter, then write the class.
Spring Boot auto-configures the proxying when the starter is on the classpath. In a plain Spring application, add @EnableAspectJAutoProxy to a @Configuration class.
@Aspect does not make the class a bean. Spring's component scan does not treat it as one either, so add @Component or declare it with an @Bean method. A frequent mistake is writing a correct aspect that never runs because it is not registered.
@Before runs before the method; it cannot stop the call except by throwing.@AfterReturning runs on normal return and can read the result.@AfterThrowing runs when an exception escapes.@After runs always, like finally.@Around wraps the call and controls it through ProceedingJoinPoint.Use the weakest advice that does the job. When several aspects apply to one method, set their order with @Order (lower value runs first on the way in).
This pointcut matches any bean method annotated with a custom @RequiresAdmin annotation, which makes the rule explicit at the call site.
this skips the advice.final classes and methods cannot be proxied with CGLIB.Spring Boot uses CGLIB proxies by default (spring.aop.proxy-target-class=true), so aspects work on classes without interfaces.
It belongs to AspectJ (org.aspectj.lang.annotation). Spring AOP recognises it and supplies its own proxy-based runtime.
Yes, unless you register the aspect another way, such as a @Bean method. Otherwise Spring never sees it.
Yes. Unit test the advice logic directly, and use a slice or @SpringBootTest to confirm the pointcut matches. Verify that the call goes through the proxy.
Because of the proxy model. Move the method to another bean, or inject the bean into itself, or use AspectJ weaving.
@Aspect defines a class that holds advice and pointcuts for cross-cutting concerns. With @Component and the AOP starter, Spring applies it to matching beans through proxies. Keep pointcuts specific, pick the narrowest advice type and remember the proxy limits.