Sub topic 9
Selecting join points with pointcut expressions
This chapter assumes you can write a basic @Aspect bean and know what advice and a join point are. In Spring AOP, a join point is always a method execution on a Spring bean. A pointcut expression decides which of those executions an aspect applies to.
The execution designator
The most common designator is execution. Its shape is:
execution(modifiers? return-type declaring-type? method-name(params) throws?)Only the return type, method name and parameters are required. The wildcard * matches any single name or type. .. matches any number of parameters, or any number of package levels.
| Expression | Matches |
|---|---|
| `execution(* com.example.service.*.*(..))` | Any method of any class directly in `service` |
| `execution(* com.example.service..*(..))` | Any method in `service` and its subpackages |
| `execution(public String find*(Long))` | Public methods starting with `find`, taking one `Long`, returning `String` |
| `execution(* *..UserService.save(..))` | `save` on `UserService` in any package |
Other designators
Several designators narrow matching by something other than the method signature:
within(com.example.service..*)limits matching to types in a package.@annotation(com.example.Audited)matches methods that carry an annotation.@within(org.springframework.stereotype.Service)matches methods of classes that carry an annotation.bean(orderService)matches by Spring bean name. This designator is specific to Spring.args(Long, ..)matches by the runtime types of the arguments.this(...)andtarget(...)match the proxy type and the target type.
Combining and naming pointcuts
Combine expressions with &&, || and !. Give a pointcut a name by annotating an empty method with @Pointcut, then reuse it by name in advice.
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 inServiceLayer() {}
@Pointcut("execution(* *..*.get*(..))")
void getters() {}
@Pointcut("inServiceLayer() && !getters()")
void serviceMutations() {}
@Before("serviceMutations()")
public void log() {
System.out.println("service method called");
}
}Named pointcuts keep expressions in one place. When an aspect in another class needs one, use the qualified form LoggingAspect.inServiceLayer(). The pointcut method must not be private in that case.
Binding arguments
A pointcut can capture values and pass them to advice. Names in args and @annotation must match the advice parameter names.
@Before("inServiceLayer() && args(id, ..)")
public void logId(Long id) {
System.out.println("called with id " + id);
}This advice runs only for service methods whose first argument is a Long.
Common errors
- Pointcut matches nothing. The target is not a Spring bean, the package is wrong, or the method is private or called from within its own class. Proxies cannot intercept those calls.
- Pointcut matches too much.
execution(* *(..))matches every method of every bean, including framework beans. Always restrict by package or annotation. - Type pattern misses subclasses.
execution(* Base.*(..))matches methods declared onBase. UseBase+to include subtypes. - Unrecognized designators. AspectJ designators such as
callorgetare not supported in Spring AOP and cause anIllegalArgumentExceptionat startup.
Exercise
Write a pointcut that matches every public method of classes annotated with @Service, but excludes methods whose names start with "get".
Show answer
@Pointcut("@within(org.springframework.stereotype.Service) && execution(public (..)) && !execution( get(..))")
Exercise
Why does execution( com.example.service..*(..)) not match a method in com.example.service.admin.AdminService?
Show answer
A single matches one package level only. The subpackage requires com.example.service...*(..), where .. spans any number of levels.