spring-projects/spring-framework · error · IllegalStateException

Cannot create scoped proxy for bean '{targetBeanName}': Targ

Error message

Cannot create scoped proxy for bean '{targetBeanName}': Target type could not be determined at the time of proxy creation.

What it means

ScopedProxyFactoryBean.setBeanFactory throws IllegalStateException when beanFactory.getType(targetBeanName) returns null, i.e. the target bean's type cannot be determined at the time the proxy is built. The proxy needs a concrete type to generate the CGLIB subclass or interface set; an unresolved type (often a FactoryBean whose getObjectType() returns null before initialisation) blocks creation.

Source

Thrown at spring-aop/src/main/java/org/springframework/aop/scope/ScopedProxyFactoryBean.java:99

		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.
		ScopedObject scopedObject = new DefaultScopedObject(cbf, this.scopedTargetSource.getTargetBeanName());
		pf.addAdvice(new DelegatingIntroductionInterceptor(scopedObject));

		// Add the AopInfrastructureBean marker to indicate that the scoped proxy
		// itself is not subject to auto-proxying! Only its target bean is.
		pf.addInterface(AopInfrastructureBean.class);

		this.proxy = pf.getProxy(cbf.getBeanClassLoader());
	}

View on GitHub (pinned to e8729d0438)

Solutions

  1. Make the target FactoryBean's getObjectType() return a deterministic type (or FactoryBean.OBJECT_TYPE_UNKNOWN only after init where allowed).
  2. Verify targetBeanName matches a registered bean and that the bean is type-determinable at factory-post-processing time.
  3. Ensure the scoped bean is registered before the scoped-proxy factory bean initialises.

Example fix

// before
public class MyFactoryBean implements FactoryBean<Object> {
  public Class<?> getObjectType() { return null; } // -> error 110
}

// after
public class MyFactoryBean implements FactoryBean<Service> {
  public Class<?> getObjectType() { return Service.class; }
}
Defensive patterns

Strategy: validation

Validate before calling

Class<?> type = beanFactory.getType(targetBeanName);
if (type == null) {
    // defer proxy creation or fail with a clearer message
    throw new IllegalStateException("Cannot resolve type for " + targetBeanName);
}

Prevention

When it happens

Trigger: The scoped target is a FactoryBean whose getObjectType() returns null early in startup, or targetBeanName does not resolve to any known type when setBeanFactory runs.

Common situations: Scoping a FactoryBean that needs full initialisation before its type is known; circular references forcing early proxy creation; typos in targetBeanName; programmatic registration ordering issues.

Related errors


AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04). Data as JSON: /data/errors/9d81b7fb437bc7ed.json. Report an issue: GitHub.