{"id":"e68c23b408c9af62","repo":"spring-projects/spring-framework","slug":"unexpected-aop-exception","errorCode":null,"errorMessage":"Unexpected AOP exception","messagePattern":"Unexpected AOP exception","errorType":"exception","errorClass":"AopConfigException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java","lineNumber":242,"sourceCode":"\t\t\t// Generate the proxy class and create a proxy instance.\n\t\t\t// ProxyCallbackFilter has method introspection capability with Advisor access.\n\t\t\ttry {\n\t\t\t\treturn (classOnly ? createProxyClass(enhancer) : createProxyClassAndInstance(enhancer, callbacks));\n\t\t\t}\n\t\t\tfinally {\n\t\t\t\t// Reduce ProxyCallbackFilter to key-only state for its class cache role\n\t\t\t\t// in the CGLIB$CALLBACK_FILTER field, not leaking any Advisor state...\n\t\t\t\tfilter.advised.reduceToAdvisorKey();\n\t\t\t}\n\t\t}\n\t\tcatch (CodeGenerationException | IllegalArgumentException ex) {\n\t\t\tthrow new AopConfigException(\"Could not generate CGLIB subclass of \" + this.advised.getTargetClass() +\n\t\t\t\t\t\": Common causes of this problem include using a final class or a non-visible class\",\n\t\t\t\t\tex);\n\t\t}\n\t\tcatch (Throwable ex) {\n\t\t\t// TargetSource.getTarget() failed\n\t\t\tthrow new AopConfigException(\"Unexpected AOP exception\", ex);\n\t\t}\n\t}\n\n\tprotected Class<?> createProxyClass(Enhancer enhancer) {\n\t\tenhancer.setInterceptDuringConstruction(false);\n\t\treturn enhancer.createClass();\n\t}\n\n\tprotected Object createProxyClassAndInstance(Enhancer enhancer, Callback[] callbacks) {\n\t\tenhancer.setInterceptDuringConstruction(false);\n\t\tenhancer.setCallbacks(callbacks);\n\t\treturn (this.constructorArgs != null && this.constructorArgTypes != null ?\n\t\t\t\tenhancer.create(this.constructorArgTypes, this.constructorArgs) :\n\t\t\t\tenhancer.create());\n\t}\n\n\t/**\n\t * Creates the CGLIB {@link Enhancer}. Subclasses may wish to override this to return a custom","sourceCodeStart":224,"sourceCodeEnd":260,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java#L224-L260","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the wrapped cause (ex.getCause()) — the real failure is always attached; fix that root cause (target init, pool sizing, missing class).","If TargetSource.getTarget() legitimately may fail, use a lazy or HotSwappableTargetSource and ensure the target is available before proxy creation.","Increase pool sizes / ensure resources (DB, external services) are up before the proxy bean initializes.","For NoClassDefFoundError causes, resolve the missing dependency/classpath conflict."],"exampleFix":"// before\n@Bean\npublic TargetSource ts() {\n  CommonsPool2TargetSource pool = new CommonsPool2TargetSource();\n  pool.setMaxSize(0);  // no objects -> getTarget() fails at build\n  return pool;\n}\n\n// after\n@Bean\npublic TargetSource ts() {\n  CommonsPool2TargetSource pool = new CommonsPool2TargetSource();\n  pool.setMaxSize(8);\n  pool.setTargetBeanName(\"expensiveTarget\");\n  return pool;\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  proxy = pf.getProxy();\n} catch (AopConfigException ex) {\n  Throwable cause = ex.getCause();\n  // branch on cause type (pool exhausted, linkage error, target init failure)\n  throw new RuntimeException(\"Proxy creation failed: \" + cause, cause);\n}","preventionTips":["Always inspect the wrapped cause — the real failure is attached.","Ensure TargetSource.getTarget() will succeed at proxy-build time (warm pools, available resources).","For lazy/expensive targets, use a TargetSource that defers resolution cleanly."],"tags":["spring-aop","cglib","target-source","unexpected","runtime"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}