spring-projects/spring-framework · error · AopConfigException

Unexpected AOP exception

Error message

Unexpected AOP exception

What it means

buildProxy() has a final catch (Throwable) at line 240 that wraps anything not already covered by the CodeGeneration/IllegalArgumentException branch. The most documented source is TargetSource.getTarget() failing (the comment at line 241), but any unexpected error during callback retrieval or enhancer setup lands here as a generic 'Unexpected AOP exception'.

Solutions

  1. Inspect the wrapped cause (ex.getCause()) - it is the real failure; fix that root issue
  2. If using a custom TargetSource, make getTarget() robust and check the underlying resource (pool, factory)
  3. For prototype/scoped targets, verify the target bean can actually be instantiated standalone

Example fix

// before
class MyTargetSource extends AbstractPoolingTargetSource { ... }
// pool exhausted -> 'Unexpected AOP exception' wrapping NoSuchElementException

// after
// increase pool size / fix target bean creation; inspect getCause() in logs
Defensive patterns

Strategy: try-catch

Validate before calling

// If using a custom TargetSource, validate getTarget() behavior in isolation:
Object t = targetSource.getTarget();
if (t == null) throw new IllegalStateException("TargetSource.getTarget() returned null");

Try / catch

try {
  Object proxy = proxyFactory.getProxy();
} catch (AopConfigException ex) {
  Throwable cause = ex.getCause();
  // inspect cause - often the real TargetSource / pool / bean-creation failure
  throw new RuntimeException("Proxy creation failed: " + cause, ex);
}

Prevention

When it happens

Trigger: A TargetSource whose getTarget() throws (custom TargetSource, pooling source exhausting, prototype bean creation failure); an unexpected runtime error inside getCallbacks()/Enhancer.create() not already classified.

Common situations: Custom TargetSource throwing on getTarget(); a pooled TargetSource (CommonsPool2TargetSource) that cannot obtain an instance; prototype target bean failing construction; classloader errors surfacing during proxy creation.

Related errors


AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09). Data as JSON: /api/errors/e68c23b408c9af62. Report an issue: GitHub.

Appendix: source

Thrown at spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java:242

			// Generate the proxy class and create a proxy instance.
			// ProxyCallbackFilter has method introspection capability with Advisor access.
			try {
				return (classOnly ? createProxyClass(enhancer) : createProxyClassAndInstance(enhancer, callbacks));
			}
			finally {
				// Reduce ProxyCallbackFilter to key-only state for its class cache role
				// in the CGLIB$CALLBACK_FILTER field, not leaking any Advisor state...
				filter.advised.reduceToAdvisorKey();
			}
		}
		catch (CodeGenerationException | IllegalArgumentException ex) {
			throw new AopConfigException("Could not generate CGLIB subclass of " + this.advised.getTargetClass() +
					": Common causes of this problem include using a final class or a non-visible class",
					ex);
		}
		catch (Throwable ex) {
			// TargetSource.getTarget() failed
			throw new AopConfigException("Unexpected AOP exception", ex);
		}
	}

	protected Class<?> createProxyClass(Enhancer enhancer) {
		enhancer.setInterceptDuringConstruction(false);
		return enhancer.createClass();
	}

	protected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {
		enhancer.setInterceptDuringConstruction(false);
		enhancer.setCallbacks(callbacks);
		return (this.constructorArgs != null && this.constructorArgTypes != null ?
				enhancer.create(this.constructorArgTypes, this.constructorArgs) :
				enhancer.create());
	}

	/**
	 * Creates the CGLIB {@link Enhancer}. Subclasses may wish to override this to return a custom

View on GitHub (pinned to 69bf83ad71)