{"record":{"id":"9d81b7fb437bc7ed","repo":"spring-projects/spring-framework","slug":"cannot-create-scoped-proxy-for-bean-targetbeanna","errorCode":null,"errorMessage":"Cannot create scoped proxy for bean '{targetBeanName}': Target type could not be determined at the time of proxy creation.","messagePattern":"Cannot create scoped proxy for bean '(.+?)': Target type could not be determined at the time of proxy creation\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java","lineNumber":99,"sourceCode":"\t\tthis.targetBeanName = targetBeanName;\n\t\tthis.scopedTargetSource.setTargetBeanName(targetBeanName);\n\t}\n\n\t@Override\n\tpublic void setBeanFactory(BeanFactory beanFactory) {\n\t\tif (!(beanFactory instanceof ConfigurableBeanFactory cbf)) {\n\t\t\tthrow new IllegalStateException(\"Not running in a ConfigurableBeanFactory: \" + beanFactory);\n\t\t}\n\t\tthis.scopedTargetSource.setBeanFactory(beanFactory);\n\n\t\tProxyFactory pf = new ProxyFactory();\n\t\tpf.copyFrom(this);\n\t\tpf.setTargetSource(this.scopedTargetSource);\n\n\t\tAssert.notNull(this.targetBeanName, \"Property 'targetBeanName' is required\");\n\t\tClass<?> beanType = beanFactory.getType(this.targetBeanName);\n\t\tif (beanType == null) {\n\t\t\tthrow new IllegalStateException(\"Cannot create scoped proxy for bean '\" + this.targetBeanName +\n\t\t\t\t\t\"': Target type could not be determined at the time of proxy creation.\");\n\t\t}\n\t\tif (!isProxyTargetClass() || beanType.isInterface() || Modifier.isPrivate(beanType.getModifiers())) {\n\t\t\tpf.setInterfaces(ClassUtils.getAllInterfacesForClass(beanType, cbf.getBeanClassLoader()));\n\t\t}\n\n\t\t// Add an introduction that implements only the methods on ScopedObject.\n\t\tScopedObject scopedObject = new DefaultScopedObject(cbf, this.scopedTargetSource.getTargetBeanName());\n\t\tpf.addAdvice(new DelegatingIntroductionInterceptor(scopedObject));\n\n\t\t// Add the AopInfrastructureBean marker to indicate that the scoped proxy\n\t\t// itself is not subject to auto-proxying! Only its target bean is.\n\t\tpf.addInterface(AopInfrastructureBean.class);\n\n\t\tthis.proxy = pf.getProxy(cbf.getBeanClassLoader());\n\t}\n\n","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java#L81-L117","documentation":"Thrown by ScopedProxyFactoryBean.setBeanFactory (line 97-101) when beanFactory.getType(targetBeanName) returns null, meaning the factory cannot determine the target bean's type at proxy-creation time. The proxy needs a concrete interface or class set to generate the proxy, so an unknown type is rejected with an IllegalStateException.","triggerScenarios":"Creating a scoped proxy whose target bean's type is not determinable: the target is a FactoryBean whose getObjectType() returns null (e.g. based on a not-yet-built resource), the target bean does not exist yet at the time the proxy is created, or a circular early reference prevents type resolution. Also when targetBeanName was never set or points to a non-existent bean.","commonSituations":"A scoped FactoryBean whose object type is only known after initialization (e.g. JPA/Kafka/dynamic client FactoryBeans). Circular dependency where the scoped target references the proxy. Forgetting to call setTargetBeanName. Beans defined with lazy or programmatic registration that are not yet resolvable.","solutions":["Ensure the target bean is registered and its type is determinable before the scoped proxy is created (have FactoryBean.getObjectType return a non-null class early).","Break circular dependencies so the target bean type can be resolved without needing the proxy.","Verify setTargetBeanName(...) is called with the correct bean name.","If using a FactoryBean, make getObjectType() return a stable type even before afterPropertiesSet."],"exampleFix":"// before\npublic class ClientFactoryBean implements FactoryBean<Client> {\n    public Class<?> getObjectType() { return null; } // unknown early\n}\n\n// after\npublic class ClientFactoryBean implements FactoryBean<Client> {\n    public Class<?> getObjectType() { return Client.class; } // resolvable\n}","handlingStrategy":"validation","validationCode":"if (beanFactory.getType(targetBeanName) == null) {\n    throw new IllegalStateException(\n        \"Cannot resolve type of '\" + targetBeanName + \"' before creating scoped proxy\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure FactoryBean.getObjectType() returns a stable type before proxy creation.","Break circular dependencies involving the scoped target.","Always call setTargetBeanName with a registered bean name."],"tags":["spring-aop","scope","factory-bean","circular-dependency"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}