Sub topic 2
AOP vocabulary in Spring terms
Aspect-oriented programming (AOP) has its own vocabulary, and much of it comes from AspectJ, the project that defined the model. Spring reuses these terms but implements only part of the model. This chapter assumes you know core Java (interfaces, inheritance, annotations), basic Spring dependency injection, and the idea of a cross-cutting concern from the previous chapter. It does not cover pointcut syntax or proxy internals in detail. Later chapters do that.
Why the vocabulary matters
Error messages, documentation and Stack Overflow answers all use these terms. If you mix up "join point" and "pointcut", you will misread the documentation and write aspects that match the wrong things. The terms also describe what Spring can and cannot do. Spring AOP supports a narrow subset of the AspectJ model, and the vocabulary shows where the limits are.
The core terms
Each term below is defined in Spring AOP terms, not in the general AOP sense.
- Join point: a point during program execution where an aspect could apply. In Spring AOP, a join point is always the execution of a method on a Spring bean. AspectJ also supports field access, constructor calls and others. Spring does not.
- Pointcut: a predicate that selects join points. It answers the question "which method executions do I care about?" You write it as an expression, such as
execution(* com.example.service..*(..)). - Advice: the code that runs at a selected join point. The kinds are
@Before,@AfterReturning,@AfterThrowing,@Afterand@Around. They differ in when they run relative to the method. - Aspect: a module that bundles pointcuts and advice. In Spring, it is a class annotated with
@Aspectthat is also registered as a bean, for example with@Component. - Target object: the original bean whose method is being advised. Spring docs also call it the advised object.
- AOP proxy: the object Spring puts in front of the target. Callers hold a reference to the proxy. The proxy runs the matching advice and then calls the target.
- Weaving: linking aspects with target code to create an advised object. AspectJ can weave at compile time, after compilation, or at class-load time. Spring AOP weaves at runtime, when the container creates the proxy.
Two more terms appear in Spring's API and documentation:
- Advisor: Spring's internal pairing of one pointcut with one piece of advice. When you write an
@Aspect, Spring turns each advice method into an advisor. You see advisors when you debug proxies or useProxyFactory. - Introduction: advice that adds new interfaces, and an implementation of them, to a bean. A later chapter covers it.
One aspect, labeled
The following aspect contains one pointcut and one piece of advice. The comments show which term applies to each part.
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 // this class is an aspect
@Component // Spring must manage it as a bean
public class LoggingAspect {
// pointcut: selects executions of any method in com.example.service
// and its subpackages
@Pointcut("execution(* com.example.service..*(..))")
void serviceMethods() { }
// advice: runs before each join point the pointcut selects
@Before("serviceMethods()")
public void logCall(JoinPoint joinPoint) {
// joinPoint describes the specific method execution being advised
System.out.println("Calling " + joinPoint.getSignature().toShortString());
}
}The JoinPoint parameter is the runtime description of the one method call that triggered the advice. The pointcut is a rule, so it exists once. A join point is a single occurrence of a method execution, so many join points can match one pointcut.
How the terms fit together
At runtime, a caller invokes a method on what it believes is the bean. It is actually the proxy. The proxy checks whether the method execution is a join point selected by any pointcut, runs the matching advice, and calls the target.
The mapping between the vocabulary and Spring constructs is:
| Term | What it is in Spring | Example |
|---|---|---|
| Aspect | A bean whose class has `@Aspect` | `LoggingAspect` |
| Join point | A method execution on a Spring bean | `OrderService.place(...)` being called |
| Pointcut | An expression that selects join points | `execution(* com.example.service..*(..))` |
| Advice | A method on the aspect | `logCall` |
| Target object | The original, unwrapped bean | the `OrderService` instance |
| AOP proxy | The wrapper the container injects | a JDK or CGLIB proxy |
| Weaving | Proxy creation at container startup | happens while beans are initialized |
Spring AOP is not AspectJ
Spring AOP uses AspectJ's annotations (@Aspect, @Before) and its pointcut expression language. It does not use the AspectJ compiler or weaver by default. Everything runs through proxies. This has direct consequences:
- Only method executions on Spring-managed beans can be join points.
- Calls that never go through the proxy are not intercepted. Later chapters cover self-invocation and
finalmethods. - Objects you create with
neware not advised, because the container never wrapped them.
Common vocabulary mistakes
- Calling a pointcut a join point. A pointcut is the selection rule. A join point is one execution it matches.
- Expecting field or constructor interception. Spring AOP join points are method executions only. If you need more, see the AspectJ weaving chapter.
- Forgetting to make the aspect a bean.
@Aspectmarks the class as an aspect. It does not register it. Without@Componentor a@Beanmethod, Spring ignores it and nothing runs. - Confusing "advice" with "aspect". The aspect is the class. Advice is a method inside it.
- Treating the proxy and the target as the same object. They are two objects.
thisinside a bean method refers to the target. The reference other beans hold refers to the proxy. This distinction explains most "advice did not run" problems you will meet later.
Exercise
An aspect declares one pointcut matching all methods in com.example.service. During one request, 40 methods in that package are called. How many pointcuts, join points and aspects are involved, and what is the target object when OrderService.place() is called?
Show answer
There is one pointcut (the rule) and one aspect (the class that holds it). There are 40 join points, one for each method execution the pointcut matches. For the call to OrderService.place(), the target object is the original OrderService bean instance. The caller reaches it through the AOP proxy, which runs the advice before delegating.
Exercise
A developer annotates a class with @Aspect but the advice never runs, and the class is not found as a bean. What is missing?
Show answer
The class is not registered as a Spring bean. @Aspect only declares the class an aspect, so it also needs @Component, a @Bean method, or component scanning that includes it.