{"id":"28f668e92421befc","repo":"spring-projects/spring-framework","slug":"not-running-in-a-configurablebeanfactory-beanfac","errorCode":null,"errorMessage":"Not running in a ConfigurableBeanFactory: {beanFactory}","messagePattern":"Not running in a ConfigurableBeanFactory: (.+?)","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java","lineNumber":88,"sourceCode":"\t * Create a new ScopedProxyFactoryBean instance.\n\t */\n\tpublic ScopedProxyFactoryBean() {\n\t\tsetProxyTargetClass(true);\n\t}\n\n\n\t/**\n\t * Set the name of the bean that is to be scoped.\n\t */\n\tpublic void setTargetBeanName(String targetBeanName) {\n\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.","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java#L70-L106","documentation":"ScopedProxyFactoryBean.setBeanFactory throws IllegalStateException when the supplied BeanFactory is not a ConfigurableBeanFactory. Scoped proxies resolve the target bean per-scope via SimpleBeanTargetSource, which needs ConfigurableBeanFactory (for getBean(name, scopedTargetSource) and scoped lifecycle hooks). A plain BeanFactory lacks those operations.","triggerScenarios":"Registering a ScopedProxyFactoryBean against a BeanFactory that is only org.springframework.beans.factory.BeanFactory (e.g. a custom minimal implementation) rather than DefaultListableBeanFactory / GenericApplicationContext.","commonSituations":"Using a stripped-down or custom BeanFactory in tests; bridging Spring AOP into a non-standard container; manual wiring that passes the wrong factory.","solutions":["Use a ConfigurableBeanFactory-backed context (AnnotationConfigApplicationContext, GenericApplicationContext, ClassPathXmlApplicationContext).","Prefer @Scope(proxyMode = ScopedProxyMode.TARGET_CLASS / INTERFACES) or <aop:scoped-proxy/> which configure the factory bean correctly.","If wiring manually, pass the ApplicationContext's internal bean factory (which is ConfigurableBeanFactory)."],"exampleFix":"// before\nBeanFactory bf = new MyMinimalBeanFactory();\nscopedProxyFactoryBean.setBeanFactory(bf); // not ConfigurableBeanFactory\n\n// after\nConfigurableBeanFactory cbf = applicationContext.getBeanFactory();\nscopedProxyFactoryBean.setBeanFactory(cbf);","handlingStrategy":"validation","validationCode":"if (!(beanFactory instanceof ConfigurableBeanFactory)) {\n    throw new IllegalStateException(\"Use a ConfigurableBeanFactory-backed context for scoped proxies\");\n}","typeGuard":"boolean isConfigurable(org.springframework.beans.factory.BeanFactory bf) {\n    return bf instanceof org.springframework.beans.factory.config.ConfigurableBeanFactory;\n}","tryCatchPattern":null,"preventionTips":["Use ApplicationContext implementations (which expose a ConfigurableBeanFactory).","Prefer @Scope(proxyMode=...) or <aop:scoped-proxy/> over manual ScopedProxyFactoryBean.","Validate the factory type before calling setBeanFactory."],"tags":["spring-aop","scope","proxy","beanfactory","configuration"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}