spring-projects/spring-framework · error · IllegalStateException

Failed to find advice method on deserialization

Error message

Failed to find advice method on deserialization

What it means

Thrown by readObject (line 696-704) during Java deserialization of an AbstractAspectJAdvice: defaultReadObject restores the declaring class, method name, and parameter types, then declaringClass.getMethod(name, types) is attempted. If the method no longer exists (renamed, signature changed, removed, moved class), NoSuchMethodException is wrapped in this IllegalStateException.

Source

Thrown at spring-aop/src/main/java/org/springframework/aop/aspectj/AbstractAspectJAdvice.java:702

	protected @Nullable JoinPointMatch getJoinPointMatch(ProxyMethodInvocation pmi) {
		String expression = this.pointcut.getExpression();
		return (expression != null ? (JoinPointMatch) pmi.getUserAttribute(expression) : null);
	}


	@Override
	public String toString() {
		return getClass().getName() + ": advice method [" + this.aspectJAdviceMethod + "]; " +
				"aspect name '" + this.aspectName + "'";
	}

	private void readObject(ObjectInputStream inputStream) throws IOException, ClassNotFoundException {
		inputStream.defaultReadObject();
		try {
			this.aspectJAdviceMethod = this.declaringClass.getMethod(this.methodName, this.parameterTypes);
		}
		catch (NoSuchMethodException ex) {
			throw new IllegalStateException("Failed to find advice method on deserialization", ex);
		}
	}


	/**
	 * MethodMatcher that excludes the specified advice method.
	 * @see AbstractAspectJAdvice#buildSafePointcut()
	 */
	private static class AdviceExcludingMethodMatcher extends StaticMethodMatcher {

		private final Method adviceMethod;

		public AdviceExcludingMethodMatcher(Method adviceMethod) {
			this.adviceMethod = adviceMethod;
		}

		@Override
		public boolean matches(Method method, Class<?> targetClass) {

View on GitHub (pinned to e8729d0438)

Solutions

  1. Keep the aspect method name and parameter types stable across versions that must deserialize each other's data.
  2. Clear the serialized advice cache/store after refactoring an aspect so stale entries are not deserialized.
  3. Avoid serializing advice objects (or the proxies that reference them); prefer rebuilding them from the context per node.
  4. Ensure the same aspect class version (same jar) is on both ends of serialization.

Example fix

// before: serialize an AspectJ advice into a distributed cache, then rename the advice method
cache.put("advice", aspectJAdvice); // later: advice method renamed to logEvent()
// deserialization fails: "Failed to find advice method on deserialization"
// after: clear cached serialized advice after the refactor, or keep method name stable
cache.invalidate("advice"); // then let the new context rebuild it
Defensive patterns

Strategy: try-catch

Validate before calling

// Before deserializing, confirm the advice method still exists on the declaring class.
try {
    declaringClass.getMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {
    throw new IllegalStateException(
        "Cached advice refers to missing method " + methodName + " on " + declaringClass, e);
}

Try / catch

try (ObjectInputStream in = new ObjectInputStream(bytes)) {
    AbstractAspectJAdvice advice = (AbstractAspectJAdvice) in.readObject();
} catch (IllegalStateException ex) {
    if (ex.getMessage().equals("Failed to find advice method on deserialization")) {
        // discard the stale serialized advice and rebuild from the current context
        advice = rebuildAdviceFromContext();
    } else { throw ex; }
}

Prevention

When it happens

Trigger: Deserializing a previously serialized advice (e.g. from a cache, session, or distributed cache/grid) in a deployment where the advice method's name or parameter types have changed since serialization, or the declaring class is a different version.

Common situations: Serializing Spring advice across application restarts or nodes after refactoring the aspect; version skew between the serializing and deserializing JVM (different aspect class versions); hot redeploy where the aspect class changed.

Related errors


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