@Aspect marks a class as an aspect: a module that holds cross-cutting logic such as logging, security checks, metrics or transaction handling, written once and applied to many methods. The annotation comes from AspectJ (org.aspectj.lang.annotation.Aspect) and Spring AOP reads it, but the annotation alone does not register anything; the class must also be a Spring bean.
Inside an @Aspect class you define pointcuts (where) and advice (what to do), and Spring applies them through proxies at runtime.
@Aspect is a marker. It tells the Spring AOP infrastructure that this bean contains advice methods and pointcut declarations. Three points matter in practice:
@Aspect. Add @Component (or declare a @Bean).@EnableAspectJAutoProxy on a configuration class. Spring Boot enables it automatically when spring-boot-starter-aop is on the classpath.Enable it in a plain Spring application:
| Annotation | Runs |
|---|---|
@Before | Before the matched method |
@AfterReturning | After normal return; can read the result |
@AfterThrowing | After an exception; can read it |
@After | After the method, always (like finally) |
@Around | Around the call; you decide whether to call proceed() |
Use the weakest advice that does the job. @Around is the most powerful, but forgetting to call proceed() or to return its result silently changes behaviour.
Pointcuts select join points. In Spring AOP a join point is always a method execution on a Spring bean. Common designators:
execution(* com.example..*Service.*(..)) matches methods by signature.within(com.example.web..*) matches all methods in types of a package.@annotation(com.example.Audited) matches methods that carry an annotation.@within(org.springframework.stereotype.Service) matches types with an annotation.bean(orderService) matches by bean name (a Spring extension).Annotation-based pointcuts are often the clearest. Define the annotation, then bind it as an argument:
When several aspects match, set order with @Order(n) on the aspect class; lower values run first on the way in. Known limitations of proxy-based AOP:
this bypasses the proxy.final classes and methods cannot be proxied by CGLIB, and private methods are not advised.Yes, in most setups. @Aspect does not make the class a bean, so Spring will not use it unless you also annotate it with @Component or define it as a @Bean.
Common causes are a missing @EnableAspectJAutoProxy (outside Boot), a pointcut that does not match, self-invocation, or the target not being a Spring bean.
It uses AspectJ's annotations and pointcut expression language, but weaving is done with JDK dynamic or CGLIB proxies at runtime rather than by the AspectJ weaver.
Not with Spring AOP. Use AspectJ weaving for that.
@Aspect declares a class that bundles pointcuts and advice for a cross-cutting concern. Pair it with @Component, make sure auto-proxying is enabled, and choose pointcuts that are narrow and readable. Remember that Spring AOP works through proxies, which explains most of its limitations.