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 (called by getClassFilter/getMethodMatcher and matching operations) when the 'expression' property was never set. Without an expression the pointcut cannot be parsed or evaluated.

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 e8729d0438)

Solutions

  1. Call setExpression("execution(...))") before any match or filter access.
  2. If configuring via XML/annotations, verify the 'expression' attribute is present and non-empty.
  3. Use the constructor/setter order: construct, setExpression, then use.

Example fix

// before
AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
pc.getClassFilter(); // throws

// after
AspectJExpressionPointcut pc = new AspectJExpressionPointcut();
pc.setExpression("execution(* com.acme..*.*(..))");
pc.getClassFilter();
Defensive patterns

Strategy: validation

Validate before calling

if (pointcut.getExpression() == null || pointcut.getExpression().isBlank()) {
    throw new IllegalStateException("expression must be set");
}

Prevention

When it happens

Trigger: Creating an AspectJExpressionPointcut via the no-arg constructor and calling getClassFilter(), getMethodMatcher(), or any match() method before calling setExpression().

Common situations: Programmatic pointcut creation where setExpression is skipped or called with null; misconfigured XML/annotation where the expression attribute is blank; bean wiring that instantiates the pointcut but never injects the expression.

Related errors


AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04). Data as JSON: /data/errors/40d725efa0c958ac.json. Report an issue: GitHub.