{"id":"4cea8327a8463e5f","repo":"spring-projects/spring-framework","slug":"could-not-generate-cglib-subclass-of-advised-gett","errorCode":null,"errorMessage":"Could not generate CGLIB subclass of {advised.getTargetClass()}: 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/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/framework/CglibAopProxy.java#L218-L254","documentation":"Thrown by CglibAopProxy.buildProxy when the Enhancer raises CodeGenerationException or IllegalArgumentException during subclass generation. CGLIB creates a subclass of the target class, so it cannot subclass a final class, a class with no visible (non-private) constructor visible to the proxy, or classes the ClassLoader isolates. Spring wraps the cause and lists the two most common reasons in the message.","triggerScenarios":"Proxying a final class with proxyTargetClass=true (or when no interfaces are supplied); proxying a class with only private constructors; a class not visible to Spring's classloader (module visibility / non-public class in another package); missing CGLIB on the classpath in older setups.","commonSituations":"Annotating a final class or Kotlin class not marked open with @Service/@Transactional and using CGLIB (default since Spring Boot 2.x); sealed classes (Java 17); third-party final utility classes being proxied accidentally by over-broad pointcuts; GraalVM native image CGLIB constraints.","solutions":["Remove the final modifier from the target class (or mark Kotlin classes open / use the kotlin-spring/all-open plugin).","If the class cannot be changed, switch to JDK dynamic proxies by having the target implement an interface and ensuring proxyTargetClass=false / @EnableAspectJAutoProxy(proxyTargetClass=false).","Ensure the target class and its constructors are at least package-visible or public to Spring's classloader.","Narrow the pointcut so it no longer matches the unproxyable final class."],"exampleFix":"// before\n@Service\n@Transactional\npublic final class OrderService { ... }  // final -> CGLIB fails\n\n// after\n@Service\n@Transactional\npublic class OrderService { ... }  // open for subclassing","handlingStrategy":"type-guard","validationCode":"Class<?> targetClass = advised.getTargetClass();\nint mods = targetClass.getModifiers();\nif (java.lang.reflect.Modifier.isFinal(mods)) {\n  throw new IllegalStateException(targetClass + \" is final; remove final or switch to JDK interface proxy\");\n}","typeGuard":"static boolean isCglibProxyable(Class<?> c) {\n  int m = c.getModifiers();\n  return !java.lang.reflect.Modifier.isFinal(m)\n      && java.lang.reflect.Modifier.isPublic(m) || c.getDeclaringClass() == null\n      && java.util.Arrays.stream(c.getDeclaredConstructors())\n          .anyMatch(ctor -> java.lang.reflect.Modifier.isPublic(ctor.getModifiers())\n                         || java.lang.reflect.Modifier.isProtected(ctor.getModifiers()));\n}","tryCatchPattern":"try {\n  return pf.getProxy();\n} catch (AopConfigException ex) {\n  if (ex.getMessage().contains(\"Could not generate CGLIB subclass\")) {\n    // advise user: remove final or supply interfaces and use JDK proxy\n  }\n  throw ex;\n}","preventionTips":["Avoid final on classes you intend to proxy; use the kotlin-spring all-open plugin for Kotlin.","Narrow pointcuts so they don't match final utility/third-party classes.","Prefer interface-based proxies for final or sealed types."],"tags":["spring-aop","cglib","final-class","proxy","configuration"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}