{"record":{"id":"ac490030eb3cc40d","repo":"spring-projects/spring-framework","slug":"could-not-generate-cglib-subclass-of-common-ca","errorCode":null,"errorMessage":"Could not generate CGLIB subclass of {}: Common causes of this problem include using a final class or a non-visible class","messagePattern":"Could not generate CGLIB subclass of (.+?): Common causes of this problem include using a final class or a non-visible class","errorType":"exception","errorClass":"AopConfigException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java","lineNumber":236,"sourceCode":"\t\t\t// fixedInterceptorMap only populated at this point, after getCallbacks call above\n\t\t\tProxyCallbackFilter filter = new ProxyCallbackFilter(\n\t\t\t\t\tthis.advised.getConfigurationOnlyCopy(), this.fixedInterceptorMap, this.fixedInterceptorOffset);\n\t\t\tenhancer.setCallbackFilter(filter);\n\t\t\tenhancer.setCallbackTypes(types);\n\n\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 ?","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java#L218-L254","documentation":"When buildProxy() (line 174) drives the CGLIB Enhancer to generate a subclass of the target class, a CodeGenerationException or IllegalArgumentException from CGLIB is caught and rethrown as AopConfigException (line 235). CGLIB cannot subclass a final class, a class with no visible constructor, or a class that is package-private and not visible to the proxy class loader - hence the guidance in the message.","triggerScenarios":"Proxying (proxyTargetClass=true or no interfaces) a final class, a class with only private constructors that CGLIB cannot reach, or a class not visible to the proxy's class loader; also when there is a constructor visibility mismatch.","commonSituations":"Enabling proxyTargetClass=true against a final class (e.g. some JDK or library types); proxying a class loaded by a different classloader not visible to Spring's; Mockito-style final classes; Spring Boot AOP with proxyTargetClass=true applied to a final bean.","solutions":["Remove the 'final' modifier from the target class so CGLIB can subclass it","Switch to JDK interface proxies: provide interfaces and ensure proxyTargetClass=false (or rely on an interface)","Make the target class and its constructors public/visible to the proxy class loader","If you cannot change the class, extract an interface and proxy that instead"],"exampleFix":"// before\npublic final class MyService { ... }\n// with: @EnableAspectJAutoProxy(proxyTargetClass = true)\n\n// after\npublic class MyService { ... }  // removed final","handlingStrategy":"validation","validationCode":"// Before proxying with CGLIB, check the target is subclassable:\nClass<?> c = target.getClass();\nif (Modifier.isFinal(c.getModifiers())) {\n  throw new IllegalStateException(c.getName() + \" is final; cannot use CGLIB subclassing\");\n}","typeGuard":"private static boolean cglibSubclassable(Class<?> c) {\n  return c != null && !Modifier.isFinal(c.getModifiers())\n      && Modifier.isPublic(c.getModifiers());\n}","tryCatchPattern":null,"preventionTips":["Avoid making AOP-proxied beans final","Provide an interface and prefer JDK proxies when the class cannot be changed","Ensure the target class and its constructors are visible to the proxy class loader"],"tags":["spring-aop","cglib","proxy","final-class"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}