{"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/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java#L81-L117","documentation":"ScopedProxyFactoryBean.setBeanFactory throws IllegalStateException when beanFactory.getType(targetBeanName) returns null, i.e. the target bean's type cannot be determined at the time the proxy is built. The proxy needs a concrete type to generate the CGLIB subclass or interface set; an unresolved type (often a FactoryBean whose getObjectType() returns null before initialisation) blocks creation.","triggerScenarios":"The scoped target is a FactoryBean whose getObjectType() returns null early in startup, or targetBeanName does not resolve to any known type when setBeanFactory runs.","commonSituations":"Scoping a FactoryBean that needs full initialisation before its type is known; circular references forcing early proxy creation; typos in targetBeanName; programmatic registration ordering issues.","solutions":["Make the target FactoryBean's getObjectType() return a deterministic type (or FactoryBean.OBJECT_TYPE_UNKNOWN only after init where allowed).","Verify targetBeanName matches a registered bean and that the bean is type-determinable at factory-post-processing time.","Ensure the scoped bean is registered before the scoped-proxy factory bean initialises."],"exampleFix":"// before\npublic class MyFactoryBean implements FactoryBean<Object> {\n  public Class<?> getObjectType() { return null; } // -> error 110\n}\n\n// after\npublic class MyFactoryBean implements FactoryBean<Service> {\n  public Class<?> getObjectType() { return Service.class; }\n}","handlingStrategy":"validation","validationCode":"Class<?> type = beanFactory.getType(targetBeanName);\nif (type == null) {\n    // defer proxy creation or fail with a clearer message\n    throw new IllegalStateException(\"Cannot resolve type for \" + targetBeanName);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Implement deterministic getObjectType() on FactoryBeans.","Register the scoped target bean before its scoped-proxy factory bean.","Double-check targetBeanName spelling and bean existence."],"tags":["spring-aop","scope","proxy","factorybean","type-resolution"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}