spring-projects/spring-framework · error · AopConfigException

Unexpected AOP exception

Error message

Unexpected AOP exception

What it means

Thrown by CglibAopProxy.buildProxy in the catch(Throwable) branch, meaning an unexpected error occurred that is not CodeGenerationException or IllegalArgumentException (those map to error 74). The most common cause noted in the source comment is that TargetSource.getTarget() failed during proxy creation (getCallbacks reads the target for static/frozen optimization). Spring wraps the original throwable so the underlying cause is preserved.

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

Solutions

  1. Inspect the wrapped cause (ex.getCause()) — the real failure is always attached; fix that root cause (target init, pool sizing, missing class).
  2. If TargetSource.getTarget() legitimately may fail, use a lazy or HotSwappableTargetSource and ensure the target is available before proxy creation.
  3. Increase pool sizes / ensure resources (DB, external services) are up before the proxy bean initializes.
  4. For NoClassDefFoundError causes, resolve the missing dependency/classpath conflict.

Example fix

// before
@Bean
public TargetSource ts() {
  CommonsPool2TargetSource pool = new CommonsPool2TargetSource();
  pool.setMaxSize(0);  // no objects -> getTarget() fails at build
  return pool;
}

// after
@Bean
public TargetSource ts() {
  CommonsPool2TargetSource pool = new CommonsPool2TargetSource();
  pool.setMaxSize(8);
  pool.setTargetBeanName("expensiveTarget");
  return pool;
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
  proxy = pf.getProxy();
} catch (AopConfigException ex) {
  Throwable cause = ex.getCause();
  // branch on cause type (pool exhausted, linkage error, target init failure)
  throw new RuntimeException("Proxy creation failed: " + cause, cause);
}

Prevention

When it happens

Trigger: A TargetSource whose getTarget() throws during proxy construction (e.g. a pool exhausted, a lazy target that fails to initialize, a prototype bean whose creation fails); an unexpected Error (OutOfMemoryError, linkage errors); CGLIB internal errors not covered by the first catch.

Common situations: Pooling TargetSource (CommonsPool2TargetSource) with no available objects at proxy build time; ScopedProxy with a scope that throws; database/resource init failures inside a target's constructor; classpath/linkage issues producing NoClassDefFoundError.

Related errors


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