Sub topic 2
AOP vocabulary in Spring terms
This chapter assumes you know core Java (interfaces, annotations), basic Spring beans, and the idea that Spring wraps some beans in proxies. Later chapters cover proxies in detail. Here you learn the five terms that every AOP guide uses, and how each maps to Spring.
The five core terms
Cross-cutting concerns such as logging or timing touch many classes. AOP gives each part of the solution a name.
| Term | Meaning | In Spring AOP |
|---|---|---|
| Aspect | A module that holds a cross-cutting concern | A class annotated with `@Aspect` and registered as a bean |
| Join point | A point in program execution where behavior could be added | Always a method execution on a Spring bean |
| Pointcut | A rule that selects join points | An expression such as `execution(* com.example.service..*(..))` |
| Advice | The code that runs at a selected join point | A method annotated with `@Before`, `@Around`, and so on |
| Weaving | Linking aspects with target objects | Done at runtime by creating a proxy |
The spec of AOP in general allows join points such as field reads or constructor calls. Spring AOP supports only method execution. This is a deliberate limit of the proxy approach, and the chapter on Spring AOP versus AspectJ returns to it.
How the terms fit together
An aspect contains pointcuts and advice. The pointcut answers "where?" and the advice answers "what?". Spring evaluates the pointcut against each bean's methods. When a method matches, the proxy runs the advice around the call.
Seeing the terms in code
The aspect below maps every term to a line.
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
@Pointcut("execution(* com.example.service..*(..))")
void serviceMethods() {}
@Before("serviceMethods()")
public void log(JoinPoint joinPoint) {
System.out.println("calling " + joinPoint.getSignature().toShortString());
}
}LoggingAspectis the aspect.serviceMethods()is a named pointcut. Its body is empty because only the annotation matters.logis the advice. It runs before each selected method.- The
JoinPointparameter describes the specific method call that triggered the advice. - Weaving happens when Spring creates a proxy for each matching bean at startup.
Common confusions
- A join point is a candidate location, and a pointcut selects some of them. In Spring every method execution on a bean is a candidate.
- Advice is the code, and the pointcut is the rule for where it applies. Writing
@Beforewithout a pointcut expression does not compile as an advice declaration. - Weaving is a process, not a class. In Spring it means "build a proxy that applies the advice".
Exercise
In the aspect @Around("execution(* com.example.service..*(..))") on a method time(ProceedingJoinPoint pjp), which part is the pointcut, which is the advice, and what does pjp represent?
Show answer
The string execution(* com.example.service..*(..)) is the pointcut expression. The annotated method time is the advice (of type around). pjp represents the current join point, which is the intercepted method call, and lets the advice call proceed() to run it.