{"record":{"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":"critical","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/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java#L70-L106","documentation":"Thrown by ScopedProxyFactoryBean.setBeanFactory (line 87-89) when the supplied BeanFactory is not a ConfigurableBeanFactory. Scoped proxies resolve the target bean from a scope on each access and need to register/destroy scoped instances, which requires ConfigurableBeanFactory machinery; any narrower factory type is rejected with an IllegalStateException.","triggerScenarios":"Manually instantiating ScopedProxyFactoryBean and calling setBeanFactory with a non-ConfigurableBeanFactory (e.g. a custom or stripped-down BeanFactory). Wiring a scoped proxy mechanism outside a Spring ApplicationContext, where the backing factory lacks the ConfigurableBeanFactory interface.","commonSituations":"Using <aop:scoped-proxy/> or @Scope(proxyMode=...) inside an environment whose bean factory is not configurable. Tests with a hand-built minimal BeanFactory. Library code that wraps a BeanFactory and breaks the ConfigurableBeanFactory contract.","solutions":["Run within a standard Spring ApplicationContext whose bean factory is a DefaultListableBeanFactory (which implements ConfigurableBeanFactory).","If constructing the ScopedProxyFactoryBean manually, pass a ConfigurableBeanFactory / DefaultListableBeanFactory instance.","Avoid scoped proxies in environments that cannot supply a ConfigurableBeanFactory; resolve scoped beans manually instead."],"exampleFix":"// before\nScopedProxyFactoryBean fb = new ScopedProxyFactoryBean();\nfb.setTargetBeanName(\"scoped\");\nfb.setBeanFactory(plainBeanFactory); // not ConfigurableBeanFactory\n\n// after\nDefaultListableBeanFactory bf = new DefaultListableBeanFactory();\n// register the scoped target bean into bf ...\nScopedProxyFactoryBean fb = new ScopedProxyFactoryBean();\nfb.setTargetBeanName(\"scoped\");\nfb.setBeanFactory(bf);","handlingStrategy":"validation","validationCode":"if (!(beanFactory instanceof ConfigurableBeanFactory)) {\n    throw new IllegalStateException(\"ScopedProxyFactoryBean needs a ConfigurableBeanFactory\");\n}","typeGuard":"import org.springframework.beans.factory.config.ConfigurableBeanFactory;\nstatic boolean supportsScopedProxy(BeanFactory bf) {\n    return bf instanceof ConfigurableBeanFactory;\n}","tryCatchPattern":null,"preventionTips":["Run scoped proxies inside a Spring ApplicationContext.","When constructing ScopedProxyFactoryBean manually, supply a ConfigurableBeanFactory.","Avoid scoped proxies in lightweight/standalone containers."],"tags":["spring-aop","scope","bean-factory","configuration"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}