In Aspect-Oriented Programming, @Aspect marks a class as an aspect: a unit that gathers a cross-cutting concern, such as logging, auditing, caching or security, and applies it to many methods without changing their code. In Spring, the annotation is read by Spring AOP, which wraps matching beans in proxies and runs your advice around their methods.
The annotation only declares intent. The class still has to be a Spring bean, and auto-proxying must be enabled.
Without AOP, concerns like timing, authorisation or audit trails get copied into every service method, which mixes them with business logic. AOP separates them. The vocabulary:
| Term | Meaning |
|---|---|
| Aspect | A class annotated @Aspect that holds advice and pointcuts |
| Join point | A point in execution; in Spring AOP always a method call on a bean |
| Pointcut | An expression that selects join points |
| Advice | The code to run (before, after, around) |
| Weaving | Linking aspects to targets; Spring does it with proxies at runtime |
Add the starter in Spring Boot:
Then write the aspect:
Without Boot, put @EnableAspectJAutoProxy on a @Configuration class. In Boot it is applied automatically when the starter is present.
@Before for checks that run before the method, such as validating a precondition.@AfterReturning to inspect or log the result.@AfterThrowing to react to exceptions, for example to record metrics.@After for cleanup that must always run.@Around when you must control execution: retry, cache, time, or short-circuit.An @Around advice must return the value from proceed() and declare throws Throwable, otherwise callers get wrong results or lose exceptions.
Pointcuts compose with &&, || and !. Name them and reuse them so advice reads clearly, and keep expressions as narrow as practical because a broad execution(* *(..)) makes start-up slower and surprises other teams.
A custom annotation keeps pointcuts readable and lets teams opt in explicitly. Define the marker, then bind it in advice:
Any bean method annotated @Audited is now logged after it returns, and the business code never mentions auditing. When several aspects apply to one method, give each aspect class an @Order value so the execution order is deterministic.
this.other()) skips the proxy, so advice does not run.If an annotation like @Transactional appears not to work, the cause is almost always one of these proxy rules.
It marks a class as a container of pointcuts and advice that implement a cross-cutting concern.
No weaver is needed. Spring AOP uses AspectJ's annotations and expression parser through the aspectjweaver library that the AOP starter brings in, but it applies advice with proxies.
@Aspect says what the class is (an aspect). @Component makes Spring create it as a bean. You normally need both.
It is more powerful, not better. Prefer the simpler advice types when they are enough, because they cannot break the call flow.
@Aspect turns a class into a reusable home for cross-cutting behaviour in Spring AOP. Combine it with @Component, enable auto-proxying, write precise pointcuts, and pick the simplest advice type. Knowing that everything runs through proxies explains both its power and its limits.