spring-projects/spring-framework · critical · IllegalStateException

Not running in a ConfigurableBeanFactory

Error message

Not running in a ConfigurableBeanFactory: {beanFactory}

What it means

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.

Solutions

  1. Run within a standard Spring ApplicationContext whose bean factory is a DefaultListableBeanFactory (which implements ConfigurableBeanFactory).
  2. If constructing the ScopedProxyFactoryBean manually, pass a ConfigurableBeanFactory / DefaultListableBeanFactory instance.
  3. Avoid scoped proxies in environments that cannot supply a ConfigurableBeanFactory; resolve scoped beans manually instead.

Example fix

// before
ScopedProxyFactoryBean fb = new ScopedProxyFactoryBean();
fb.setTargetBeanName("scoped");
fb.setBeanFactory(plainBeanFactory); // not ConfigurableBeanFactory

// after
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
// register the scoped target bean into bf ...
ScopedProxyFactoryBean fb = new ScopedProxyFactoryBean();
fb.setTargetBeanName("scoped");
fb.setBeanFactory(bf);
Defensive patterns

Strategy: validation

Validate before calling

if (!(beanFactory instanceof ConfigurableBeanFactory)) {
    throw new IllegalStateException("ScopedProxyFactoryBean needs a ConfigurableBeanFactory");
}

Type guard

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
static boolean supportsScopedProxy(BeanFactory bf) {
    return bf instanceof ConfigurableBeanFactory;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09). Data as JSON: /api/errors/28f668e92421befc. Report an issue: GitHub.

Appendix: 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 69bf83ad71)