@Aspect and @Pointcut in Spring AOP
`@Aspect` marks a class as an AOP aspect, while `@Pointcut` declares a reusable expression for selecting join points such as matching method executions. Together, they separate cross-cutting behavior from application business logic.
In Spring AOP, an aspect groups behavior that cuts across multiple application components, such as logging, authorization, transaction-related concerns, or metrics. Annotating a class with `@Aspect` tells Spring that the class contains advice and pointcut definitions that participate in AOP processing.
A pointcut is a predicate that identifies which join points an advice should apply to. Spring AOP primarily works with method-execution join points on Spring-managed beans. `@Pointcut` lets you give such a predicate a method name, creating a reusable named pointcut.
A pointcut method normally has an empty body and returns `void`; its name represents the pointcut expression. For example, `@Pointcut("execution(* com.example.service..*(..))")` can select methods under a service package. Advice annotations such as `@Before`, `@After`, or `@Around` can then reference that named pointcut.
The separation is useful because the selection rule can be defined once and reused by several pieces of advice. Pointcut expressions can also be combined with logical operators such as `&&`, `||`, and `!`, allowing more precise matching.
Key points
- `@Aspect` identifies a class containing cross-cutting AOP definitions.
- `@Pointcut` declares a reusable pointcut expression.
- The pointcut selects matching join points; advice defines what happens there.
- Spring AOP commonly intercepts method executions on Spring-managed beans.
- Named pointcuts reduce duplication when several advice methods share the same selection rule.
Interview questions
What is the difference between `@Aspect` and `@Pointcut`?
`@Aspect` marks a class as an aspect that contains cross-cutting AOP definitions. `@Pointcut` defines a predicate describing which join points should be matched. The pointcut does not itself perform an action; advice uses it to determine where an action should run. A named pointcut can be referenced by multiple advice methods.
Why would you create a named pointcut?
A named pointcut gives a pointcut expression a reusable name. Multiple advice methods can reference that name instead of duplicating the expression. This makes the aspect easier to read and maintain. It also lets a change to the matching rule be made in one place.
What does this pointcut expression mean: `execution(* com.example.service..*(..))`?
It matches method-execution join points whose return type can be any type. The package pattern targets types under `com.example.service` and its subpackages. The `*` after the package pattern matches any method name, while `(..)` permits any argument list. In practice, this is commonly used to apply advice across service-layer methods.
Can one pointcut be reused by multiple advice methods?
Yes. A named pointcut can be referenced by multiple advice annotations in an aspect. For example, `@Before("serviceMethods()")` and `@After("serviceMethods()")` can target the same set of join points. This separates the selection rule from the different behaviors applied to it.
What are important limitations of Spring AOP pointcuts compared with full AspectJ?
Spring AOP is proxy-based and primarily exposes method-execution join points on Spring-managed beans. Consequently, interception does not generally cover arbitrary join points such as field access. Proxy mechanics also matter for calls that bypass the proxy, such as typical self-invocation within the same object. Full AspectJ uses weaving and provides a broader join-point model.