{"id":"849fd243a5704898","repo":"spring-projects/spring-framework","slug":"factorybean-is-not-fully-initialized-yet","errorCode":null,"errorMessage":"FactoryBean is not fully initialized yet","messagePattern":"FactoryBean is not fully initialized yet","errorType":"exception","errorClass":"FactoryBeanNotInitializedException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java","lineNumber":121,"sourceCode":"\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\n\t@Override\n\tpublic @Nullable Object getObject() {\n\t\tif (this.proxy == null) {\n\t\t\tthrow new FactoryBeanNotInitializedException();\n\t\t}\n\t\treturn this.proxy;\n\t}\n\n\t@Override\n\tpublic @Nullable Class<?> getObjectType() {\n\t\tif (this.proxy != null) {\n\t\t\treturn this.proxy.getClass();\n\t\t}\n\t\treturn this.scopedTargetSource.getTargetClass();\n\t}\n\n\t@Override\n\tpublic boolean isSingleton() {\n\t\treturn true;\n\t}\n\n}","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java#L103-L139","documentation":"ScopedProxyFactoryBean.getObject throws FactoryBeanNotInitializedException when the cached proxy field is still null, meaning setBeanFactory has not yet executed. The FactoryBean contract requires initialisation before object retrieval; without the proxy there is nothing to return.","triggerScenarios":"Calling scopedProxyFactoryBean.getObject() before the Spring container has called setBeanFactory (e.g. during a premature programmatic lookup).","commonSituations":"Programmatically instantiating ScopedProxyFactoryBean and calling getObject() directly; bean definition ordering that triggers early getObject before initialisation.","solutions":["Let the container fully initialise the FactoryBean (setTargetBeanName + setBeanFactory) before retrieving the object.","Obtain the scoped proxy through the ApplicationContext (context.getBean(name)) rather than calling getObject() manually.","If registering programmatically, call setBeanFactory last and only then access getObject()."],"exampleFix":"// before\nScopedProxyFactoryBean fb = new ScopedProxyFactoryBean();\nObject o = fb.getObject(); // proxy still null -> error 111\n\n// after\nScopedProxyFactoryBean fb = new ScopedProxyFactoryBean();\nfb.setTargetBeanName(\"scopedTarget\");\nfb.setBeanFactory(applicationContext.getBeanFactory());\nObject o = fb.getObject();","handlingStrategy":"validation","validationCode":"if (scopedProxyFactoryBean.getObjectType() == null && /* not initialised */ true) {\n    throw new IllegalStateException(\"FactoryBean not initialised; call setBeanFactory first\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Retrieve scoped proxies via context.getBean(name), not manual getObject().","Ensure setTargetBeanName + setBeanFactory run before getObject in programmatic setups.","Respect the FactoryBean lifecycle; do not short-circuit initialisation."],"tags":["spring-aop","scope","factorybean","lifecycle"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}