Master Spring AOP Pointcut Expressions: Syntax, Examples, and Aspect Order
This article explains Spring AOP pointcut expression syntax, demonstrates how to match methods, packages, and beans with execution, within, and bean designators, shows how to combine expressions, and details controlling aspect execution order using @Order and the Ordered interface.
1. Introduction
Spring AOP (Aspect‑Oriented Programming) provides a powerful paradigm for modularizing cross‑cutting concerns. A pointcut is a collection of one or more join points where advice should be applied. Join points are points in program execution such as method execution, object instantiation, or field access.
2. Pointcut Expression Syntax
Spring AOP uses AspectJ‑style expressions to define pointcuts. The execution() designator specifies method execution join points. Basic syntax:
<code>execution(modifiers? return_type method_name(param_type1, param_type2, …)</code>Example:
<code>execution(public void com.pack.service.UserService.doSomething())</code>Wildcard * matches any character sequence, and .. matches any number of parameters:
<code>execution(* com.pack.service.*.*(..))</code>The within() designator limits join points to a specific type or package:
<code>within(com.pack.service.*)</code>It matches all methods in the com.pack.service package.
2.1 Matching Specific Methods
Typical expressions based on method signatures:
Pointcut Expression
Description
execution(* com.pack.UserService.*(..))Matches all methods in the specified package and class.
execution(* UserService.*(..))Matches all methods in the same package and specified class.
execution(public * UserService.*(..))Matches all public methods in
UserService.
execution(public User UserService.*(..))Matches public methods returning
Userobjects.
execution(public User UserService.*(User, ..))Matches public methods returning
Userwith first parameter
User.
execution(public User UserService.*(User, Integer))Matches public methods returning
Userwith specified parameters.
2.2 with Expressions
The within() expression can target all methods of classes in a package, sub‑packages, a specific class, or an interface hierarchy:
Pointcut Expression
Description
within(com.pack.*)Matches all classes in package
com.pack.*.
within(com.pack..*)Matches all classes in
com.packand its sub‑packages.
within(com.pack.UserService)Matches all methods of the specified class.
within(UserService)Matches all methods of the class in the current package.
within(IUserService+)Matches all methods of implementations of the specified interface.
2.3 Bean Expressions
The bean() designator matches methods of beans whose names fit a pattern:
Pointcut Expression
Description
bean(*Service)Matches all methods in beans whose name ends with “Service”.
bean(userService)Matches all methods in the bean named
userService.
bean(com.pack.service.*)Matches all methods of beans in the specified package.
bean(@PackAnnotation *)Matches all methods of beans annotated with
@PackAnnotation.
2.4 Combining Pointcut Expressions
Expressions can be combined with logical operators && , || , and ! . Example matching beans whose name ends with “Service” or “DAO”:
<code>bean(*Service) || bean(*DAO)</code>3. Aspect Execution Order
When multiple aspects intercept the same join point, the order determines which advice runs first. For example, a LoggingAspect should execute before a SecurityAspect to log the call prior to security checks.
3.1 Using @Order Annotation
The @Order annotation assigns a precedence value; lower values have higher priority.
<code>@Aspect
@Order(1)
@Component
public class MyAspect1 {
// first to execute
}
@Aspect
@Order(2)
@Component
public class MyAspect2 {
// executed later
}</code>3.2 Implementing Ordered Interface
Alternatively, an aspect can implement the Ordered interface to provide dynamic ordering:
<code>@Aspect
@Component
public class MyAspect1 implements Ordered {
@Override
public int getOrder() { return 1; }
// first to execute
}
@Aspect
@Component
public class MyAspect2 implements Ordered {
@Override
public int getOrder() { return 2; }
// executed later
}</code>3.3 Full Example
Below is a complete example with a LoggingAspect and a SecurityAspect where logging runs before security checks:
<code>@Aspect
@Order(1)
@Component
public class LoggingAspect {
private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);
@Before("execution(* com.pack.service.*.*(..))")
public void logBefore() {
logger.info("LoggingAspect: Logging before method execution");
}
}
@Aspect
@Order(2)
@Component
public class SecurityAspect {
private static final Logger logger = LoggerFactory.getLogger(SecurityAspect.class);
@Before("execution(* com.pack.service.*.*(..))")
public void checkSecurity() {
logger.info("SecurityAspect: Performing security check before method execution");
}
}</code>Running the application produces:
<code>INFO LoggingAspect: Logging before method execution
INFO SecurityAspect: Performing security check before method execution</code>Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.