spring-projects/spring-framework · error · IllegalStateException
Must set property 'expression' before attempting to match
Error message
Must set property 'expression' before attempting to match
What it means
Thrown by AspectJExpressionPointcut.checkExpression() when getClassFilter() or getMethodMatcher() is called before the 'expression' property has been set. The pointcut cannot match anything without an AspectJ expression string.
Solutions
- Call setExpression("execution(...)") before getClassFilter() / getMethodMatcher()
- Inject an already-configured pointcut bean instead of constructing and configuring inline
- Validate expression != null early in your configuration code
Example fix
// before
AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
pc.getClassFilter(); // throws
// after
AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
pc.setExpression("execution(* com.example..*.*(..))");
pc.getClassFilter(); Defensive patterns
Strategy: validation
Validate before calling
// Validate expression is set before matching
AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
pc.setExpression("execution(* com.example..*.*(..))");
if (pc.getExpression() == null) {
throw new IllegalStateException("Expression must be set before matching");
}
pc.getClassFilter(); Prevention
- Set the expression immediately after construction, before any match call
- Prefer defining pointcuts via annotations (@Pointcut) or XML, which validate at load time
- Inject pre-configured pointcut beans instead of building them inline
When it happens
Trigger: Creating an AspectJExpressionPointcut and immediately calling getClassFilter() or getMethodMatcher() without calling setExpression(...) first.
Common situations: Programmatic configuration that forgets to set the expression, or XML/annotation config that leaves the pointcut expression empty or null.
Related errors
- Failed to resolve pointcut expression in
- Invalid expression at location
- Number of pointcut parameter names must match number of…
- Advice must be declared inside an aspect type: Offending…
- 'argumentNames' property of AbstractAspectJAdvice contains…
AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09).
Data as JSON: /api/errors/40d725efa0c958ac.
Report an issue: GitHub.
Appendix: source
Thrown at spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJExpressionPointcut.java:188
@Override
public ClassFilter getClassFilter() {
checkExpression();
return this;
}
@Override
public MethodMatcher getMethodMatcher() {
checkExpression();
return this;
}
/**
* Check whether this pointcut is ready to match.
*/
private void checkExpression() {
if (getExpression() == null) {
throw new IllegalStateException("Must set property 'expression' before attempting to match");
}
}
/**
* Lazily build the underlying AspectJ pointcut expression.
*/
private PointcutExpression obtainPointcutExpression() {
PointcutExpression pointcutExpression = this.pointcutExpression;
if (pointcutExpression == null) {
ClassLoader pointcutClassLoader = determinePointcutClassLoader();
pointcutExpression = buildPointcutExpression(pointcutClassLoader);
this.pointcutClassLoader = pointcutClassLoader;
this.pointcutExpression = pointcutExpression;
}
return pointcutExpression;
}
/**View on GitHub (pinned to 69bf83ad71)