spring-projects/spring-framework · error · FactoryBeanNotInitializedException

Cannot determine target class for proxy

Error message

Cannot determine target class for proxy

What it means

Thrown as FactoryBeanNotInitializedException by ProxyFactoryBean.getSingletonInstance when autodetectInterfaces is true (the default), no interfaces are explicitly set, proxyTargetClass is false, and getTargetClass() returns null. The factory cannot infer which interfaces to proxy because no target is set yet, so initialization aborts.

Solutions

  1. Set the target: <property name="target" ref="myServiceBean"/> or setTargetName("myServiceBean").
  2. Or set explicit interfaces: <property name="proxyInterfaces" value="com.example.MyService"/>.
  3. Or set proxyTargetClass=true so the factory builds a class proxy without needing interfaces.
  4. Check for typos in targetName and circular-dependency ordering issues that leave the target unresolved.

Example fix

<!-- before -->
<bean id="service" class="...ProxyFactoryBean">
  <property name="interceptorNames" value="loggingAdvice"/>
</bean>

<!-- after -->
<bean id="service" class="...ProxyFactoryBean">
  <property name="target" ref="serviceImpl"/>
  <property name="interceptorNames" value="loggingAdvice"/>
</bean>
Defensive patterns

Strategy: validation

Validate before calling

// Sanity-check before relying on getObjectType()/getObject()
if (pfb.isAutodetectInterfaces() && pfb.getProxiedInterfaces().length == 0
    && !pfb.isProxyTargetClass() && pfb.getTargetClass() == null) {
  throw new IllegalStateException("ProxyFactoryBean missing target/interfaces/proxyTargetClass");
}

Type guard

static boolean proxyFactoryBeanReady(ProxyFactoryBean fb) {
  return fb.getTarget() != null || fb.getTargetSource() != null
      || fb.getProxiedInterfaces().length > 0 || fb.isProxyTargetClass();
}

Try / catch

try { Object p = pfb.getObject(); }
catch (FactoryBeanNotInitializedException e) {
  if (e.getMessage().contains("Cannot determine target class")) { /* set target or interfaces */ }
  throw e;
}

Prevention

When it happens

Trigger: A ProxyFactoryBean bean configured with interceptorNames but no target/targetName/targetSource and no proxyInterfaces, fetched from the BeanFactory before a target is available. Lines 303-308: autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass() && getTargetClass() == null.

Common situations: XML/@Configuration ProxyFactoryBean where the target property was forgotten or the target bean name was misspelled; circular-dependency scenarios where getObject() is called before the target bean exists; using interceptorNames with the target as last entry but the bean type cannot be determined.

Related errors


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

Appendix: source

Thrown at spring-aop/src/main/java/org/springframework/aop/framework/ProxyFactoryBean.java:307

	@Override
	public boolean isSingleton() {
		return this.singleton;
	}


	/**
	 * Return the singleton instance of this class's proxy object,
	 * lazily creating it if it hasn't been created already.
	 * @return the shared singleton proxy
	 */
	private synchronized Object getSingletonInstance() {
		if (this.singletonInstance == null) {
			this.targetSource = freshTargetSource();
			if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) {
				// Rely on AOP infrastructure to tell us what interfaces to proxy.
				Class<?> targetClass = getTargetClass();
				if (targetClass == null) {
					throw new FactoryBeanNotInitializedException("Cannot determine target class for proxy");
				}
				setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader));
			}
			// Initialize the shared singleton instance.
			super.setFrozen(this.freezeProxy);
			this.singletonInstance = getProxy(createAopProxy());
		}
		return this.singletonInstance;
	}

	/**
	 * Create a new prototype instance of this class's created proxy object,
	 * backed by an independent AdvisedSupport configuration.
	 * @return a totally independent proxy, whose advice we may manipulate in isolation
	 */
	private synchronized Object newPrototypeInstance() {
		// In the case of a prototype, we need to give the proxy
		// an independent instance of the configuration.

View on GitHub (pinned to 69bf83ad71)