{"id":"d438748e71a632fe","repo":"spring-projects/spring-framework","slug":"cannot-create-class-proxy-for-targetsource-with-nu","errorCode":null,"errorMessage":"Cannot create class proxy for TargetSource with null target class","messagePattern":"Cannot create class proxy for TargetSource with null target class","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactory.java","lineNumber":161,"sourceCode":"\t * @param proxyInterface the interface that the proxy should implement\n\t * @param targetSource the TargetSource that the proxy should invoke\n\t * @return the proxy object\n\t * @see #ProxyFactory(Class, org.springframework.aop.TargetSource)\n\t */\n\t@SuppressWarnings(\"unchecked\")\n\tpublic static <T> T getProxy(Class<T> proxyInterface, TargetSource targetSource) {\n\t\treturn (T) new ProxyFactory(proxyInterface, targetSource).getProxy();\n\t}\n\n\t/**\n\t * Create a proxy for the specified {@code TargetSource} that extends\n\t * the target class of the {@code TargetSource}.\n\t * @param targetSource the TargetSource that the proxy should invoke\n\t * @return the proxy object\n\t */\n\tpublic static Object getProxy(TargetSource targetSource) {\n\t\tif (targetSource.getTargetClass() == null) {\n\t\t\tthrow new IllegalArgumentException(\"Cannot create class proxy for TargetSource with null target class\");\n\t\t}\n\t\tProxyFactory proxyFactory = new ProxyFactory();\n\t\tproxyFactory.setTargetSource(targetSource);\n\t\tproxyFactory.setProxyTargetClass(true);\n\t\treturn proxyFactory.getProxy();\n\t}\n\n}\n","sourceCodeStart":143,"sourceCodeEnd":170,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactory.java#L143-L170","documentation":"ProxyFactory.getProxy(TargetSource) creates a target-class-based CGLIB proxy. It explicitly rejects a TargetSource whose getTargetClass() returns null, because there is no class to subclass for the proxy. The check is a precondition guard before setProxyTargetClass(true).","triggerScenarios":"Calling ProxyFactory.getProxy(TargetSource) where the TargetSource was built without a known target class - for example EmptyTargetSource.INSTANCE or a TargetSource whose target is null and whose class was never set.","commonSituations":"Using a TargetSource backed by a lazy/prototype target that has not yet resolved its class; passing EmptyTargetSource; building a TargetSource from a null target without setting targetClass; misconfiguration in a hot-swappable target source.","solutions":["Pass a TargetSource that resolves a non-null target class: new SingletonTargetSource(myInstance) or EmptyTargetSource.forClass(MyClass.class).","If you must proxy an interface instead, use ProxyFactory.getProxy(Class<T>, TargetSource).","Verify the bean you wrapped is non-null and its class is obtainable before constructing the TargetSource."],"exampleFix":"// before\nTargetSource ts = EmptyTargetSource.INSTANCE; // null class\nObject proxy = ProxyFactory.getProxy(ts);\n\n// after\nTargetSource ts = EmptyTargetSource.forClass(MyService.class);\nObject proxy = ProxyFactory.getProxy(ts);","handlingStrategy":"validation","validationCode":"if (targetSource.getTargetClass() == null) {\n    throw new IllegalArgumentException(\"TargetSource must expose a non-null target class: \" + targetSource);\n}\nObject proxy = ProxyFactory.getProxy(targetSource);","typeGuard":"public static boolean hasResolvableTargetClass(TargetSource ts) {\n    return ts != null && ts.getTargetClass() != null;\n}","tryCatchPattern":null,"preventionTips":["Build TargetSource from a non-null instance via SingletonTargetSource.","Use EmptyTargetSource.forClass(X.class) instead of EmptyTargetSource.INSTANCE when only the class is known.","If proxying an interface, prefer the getProxy(Class<T>, TargetSource) overload."],"tags":["aop","proxy","target-source","validation"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}