{"id":"408ebf8f680b877e","repo":"spring-projects/spring-framework","slug":"targetsource-cannot-determine-target-class-either","errorCode":null,"errorMessage":"TargetSource cannot determine target class: Either an interface or a target is required for proxy creation.","messagePattern":"TargetSource cannot determine target class: Either an interface or a target is required for proxy creation\\.","errorType":"exception","errorClass":"AopConfigException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/framework/DefaultAopProxyFactory.java","lineNumber":64,"sourceCode":" * @see AdvisedSupport#setInterfaces\n */\npublic class DefaultAopProxyFactory implements AopProxyFactory, Serializable {\n\n\t/**\n\t * Singleton instance of this class.\n\t * @since 6.0.10\n\t */\n\tpublic static final DefaultAopProxyFactory INSTANCE = new DefaultAopProxyFactory();\n\n\tprivate static final long serialVersionUID = 7930414337282325166L;\n\n\n\t@Override\n\tpublic AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {\n\t\tif (config.isOptimize() || config.isProxyTargetClass() || !config.hasUserSuppliedInterfaces()) {\n\t\t\tClass<?> targetClass = config.getTargetClass();\n\t\t\tif (targetClass == null && config.getProxiedInterfaces().length == 0) {\n\t\t\t\tthrow new AopConfigException(\"TargetSource cannot determine target class: \" +\n\t\t\t\t\t\t\"Either an interface or a target is required for proxy creation.\");\n\t\t\t}\n\t\t\tif (targetClass == null || targetClass.isInterface() ||\n\t\t\t\t\tProxy.isProxyClass(targetClass) || ClassUtils.isLambdaClass(targetClass)) {\n\t\t\t\treturn new JdkDynamicAopProxy(config);\n\t\t\t}\n\t\t\treturn new ObjenesisCglibAopProxy(config);\n\t\t}\n\t\telse {\n\t\t\treturn new JdkDynamicAopProxy(config);\n\t\t}\n\t}\n\n}\n","sourceCodeStart":46,"sourceCodeEnd":79,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/framework/DefaultAopProxyFactory.java#L46-L79","documentation":"Thrown by DefaultAopProxyFactory.createAopProxy when, in the CGLIB branch (optimize || proxyTargetClass || no user-supplied interfaces), both the target class and the proxied interfaces are absent. To create any proxy Spring needs at least one of: a concrete target class to subclass, or interfaces to implement. With neither, neither CGLIB nor JDK proxying is possible, so the factory aborts up front.","triggerScenarios":"Building a ProxyFactory with neither setTarget/setTargetSource nor setInterfaces, then calling getProxy(); a TargetSource whose getTargetClass() returns null combined with an empty interface list; an interceptor-only proxy configured without any interface (which would be needed for a JDK proxy) and no target.","commonSituations":"Creating 'stateless' interceptor proxies without specifying an interface to expose; misconfigured AspectJ proxies; programmatic ProxyFactory that forgot to set the target or interfaces; EmptyTargetSource with no class.","solutions":["Provide a target object: proxyFactory.setTarget(targetInstance) (yields a SingletonTargetSource with a concrete class).","Or provide one or more interfaces: proxyFactory.setInterfaces(MyService.class) so a JDK proxy can be built.","Or set a target class explicitly when there is no instance yet: proxyFactory.setTargetClass(MyService.class).","Audit the ProxyFactory build sequence to ensure setTarget/setInterfaces is called before getProxy()."],"exampleFix":"// before\nProxyFactory pf = new ProxyFactory();\npf.addAdvice(myAdvice);\nObject proxy = pf.getProxy();  // throws: no target, no interfaces\n\n// after\nProxyFactory pf = new ProxyFactory();\npf.setInterfaces(MyService.class);\npf.addAdvice(myAdvice);\nMyService proxy = (MyService) pf.getProxy();","handlingStrategy":"validation","validationCode":"ProxyFactory pf = new ProxyFactory();\n// before getProxy():\nboolean hasTarget = pf.getTargetClass() != null;\nboolean hasInterfaces = pf.getProxiedInterfaces().length > 0;\nif (!hasTarget && !hasInterfaces) {\n  throw new IllegalStateException(\n    \"ProxyFactory has neither target class nor interfaces; cannot create a proxy\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always set a target or at least one interface on ProxyFactory before getProxy().","Use setTargetClass when you have no instance yet but know the class.","Add a startup integration test to catch missing-target proxy beans early."],"tags":["spring-aop","proxy-factory","target","interfaces","configuration"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}