spring-projects/spring-framework · error · IllegalStateException
Not running in a ConfigurableBeanFactory: {beanFactory}
Error message
Not running in a ConfigurableBeanFactory: {beanFactory} What it means
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.
Source
Thrown at spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java:88
* Create a new ScopedProxyFactoryBean instance.
*/
public ScopedProxyFactoryBean() {
setProxyTargetClass(true);
}
/**
* Set the name of the bean that is to be scoped.
*/
public void setTargetBeanName(String targetBeanName) {
this.targetBeanName = targetBeanName;
this.scopedTargetSource.setTargetBeanName(targetBeanName);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) {
if (!(beanFactory instanceof ConfigurableBeanFactory cbf)) {
throw new IllegalStateException("Not running in a ConfigurableBeanFactory: " + beanFactory);
}
this.scopedTargetSource.setBeanFactory(beanFactory);
ProxyFactory pf = new ProxyFactory();
pf.copyFrom(this);
pf.setTargetSource(this.scopedTargetSource);
Assert.notNull(this.targetBeanName, "Property 'targetBeanName' is required");
Class<?> beanType = beanFactory.getType(this.targetBeanName);
if (beanType == null) {
throw new IllegalStateException("Cannot create scoped proxy for bean '" + this.targetBeanName +
"': Target type could not be determined at the time of proxy creation.");
}
if (!isProxyTargetClass() || beanType.isInterface() || Modifier.isPrivate(beanType.getModifiers())) {
pf.setInterfaces(ClassUtils.getAllInterfacesForClass(beanType, cbf.getBeanClassLoader()));
}
// Add an introduction that implements only the methods on ScopedObject.View on GitHub (pinned to e8729d0438)
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).
Example fix
// before BeanFactory bf = new MyMinimalBeanFactory(); scopedProxyFactoryBean.setBeanFactory(bf); // not ConfigurableBeanFactory // after ConfigurableBeanFactory cbf = applicationContext.getBeanFactory(); scopedProxyFactoryBean.setBeanFactory(cbf);
Defensive patterns
Strategy: validation
Validate before calling
if (!(beanFactory instanceof ConfigurableBeanFactory)) {
throw new IllegalStateException("Use a ConfigurableBeanFactory-backed context for scoped proxies");
} Type guard
boolean isConfigurable(org.springframework.beans.factory.BeanFactory bf) {
return bf instanceof org.springframework.beans.factory.config.ConfigurableBeanFactory;
} Prevention
- Use ApplicationContext implementations (which expose a ConfigurableBeanFactory).
- Prefer @Scope(proxyMode=...) or <aop:scoped-proxy/> over manual ScopedProxyFactoryBean.
- Validate the factory type before calling setBeanFactory.
When it happens
Trigger: Registering a ScopedProxyFactoryBean against a BeanFactory that is only org.springframework.beans.factory.BeanFactory (e.g. a custom minimal implementation) rather than DefaultListableBeanFactory / GenericApplicationContext.
Common situations: Using a stripped-down or custom BeanFactory in tests; bridging Spring AOP into a non-standard container; manual wiring that passes the wrong factory.
Related errors
- MethodInvocation is not a Spring ProxyMethodInvocation: {}
- Property 'target' is required
- [{}] is not an interface
- Could not generate CGLIB subclass of {advised.getTargetClass
- BeanFactory must be set on {getClass().getSimpleName()} to a
AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04).
Data as JSON: /data/errors/28f668e92421befc.json.
Report an issue: GitHub.