Aspect, Join Point, Pointcut, and Advice in Spring AOP
These four concepts describe how Spring AOP identifies execution points in application code and applies cross-cutting behavior such as logging or transaction handling. An aspect groups the concern, a join point is a point during execution, a pointcut selects matching join points, and advice defines what runs there.
An aspect represents a cross-cutting concern, such as logging, security checks, caching, or transaction behavior. In Spring AOP, an aspect commonly combines pointcut expressions with advice methods that should execute when those expressions match.
A join point is a point in program execution where additional behavior could conceptually be applied. Spring AOP is proxy-based and primarily exposes method-execution join points for Spring-managed beans, rather than every possible join-point kind supported by the broader AspectJ model.
A pointcut is a predicate that selects join points. For example, `execution(* com.example.service..*(..))` can match method executions in a package and its subpackages. The pointcut answers "where?"; it does not itself contain the behavior to execute.
Advice is the action performed at a matched join point. Common advice types include `@Before`, `@After`, `@AfterReturning`, `@AfterThrowing`, and `@Around`. `@Around` advice can control whether and when the target method proceeds by calling `ProceedingJoinPoint.proceed()`.
Together, these abstractions separate cross-cutting behavior from business logic: the aspect organizes the concern, the pointcut identifies matching execution points, and the advice supplies the behavior applied there.
Key points
- Aspect organizes a cross-cutting concern and its AOP configuration.
- Join point represents an execution point where advice can apply; Spring AOP primarily uses method execution.
- Pointcut selects which join points match.
- Advice defines the behavior executed at matched join points.
- `@Around` advice can run code before and after the target and can control whether the target proceeds.
Interview questions
What are an aspect, join point, pointcut, and advice?
An aspect packages a cross-cutting concern such as logging or security. A join point is an execution point where AOP behavior can be applied. A pointcut selects the join points of interest, while advice contains the code that runs at those selected points. In Spring AOP, method execution is the main join-point model.
What does a pointcut expression such as `execution(* com.example.service..*(..))` select?
It is an `execution` pointcut expression that matches method executions. The `*` before the package pattern represents any return type, `com.example.service..*` covers types in that package and its subpackages, and `(..)` permits any method argument list. The exact matching behavior depends on the complete expression and the proxying context.
When would you use `@Around` advice instead of `@Before` advice?
Use `@Before` when behavior only needs to run before the target method and does not need to control the invocation. Use `@Around` when you need behavior both before and after execution, need access to the return value or exception flow, or need to decide whether the target proceeds. Around advice uses `ProceedingJoinPoint.proceed()` to invoke the target when continuation is desired.
Why might a Spring AOP advice not run when a method calls another method on the same object?
Spring AOP normally applies advice through a proxy around the target bean. An external caller reaches the proxy, which can intercept the method invocation. A direct call such as `this.otherMethod()` stays inside the target object and does not pass through that proxy. Therefore, proxy-based advice is generally not applied to that self-invocation.
How do different advice types differ in Spring AOP?
`@Before` runs before the matched method executes. `@After` runs after completion regardless of whether the method returns normally or throws, while `@AfterReturning` and `@AfterThrowing` target successful and exceptional completion respectively. `@Around` wraps the invocation and can control whether `proceed()` is called, as well as inspect or transform the result and exceptions.