spring-projects/spring-framework · error · FactoryBeanNotInitializedException
Cannot determine target class for proxy
Error message
Cannot determine target class for proxy
What it means
In ProxyFactoryBean.getSingletonInstance(), when interfaces must be auto-detected (autodetectInterfaces=true, no explicit interfaces, not proxyTargetClass) and getTargetClass() returns null, Spring cannot decide which interfaces the JDK proxy should expose. It throws FactoryBeanNotInitializedException because the target class is unknown at the moment of proxy creation.
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 e8729d0438)
Solutions
- Set an explicit target (target or targetName property) or explicit 'proxyInterfaces' on the ProxyFactoryBean.
- Set proxyTargetClass=true if you want a CGLIB proxy of a concrete class.
- Ensure the referenced target bean is resolvable at ProxyFactoryBean initialization time (avoid circular dependencies).
Example fix
// before <bean class="...ProxyFactoryBean"> <property name="interceptorNames" value="myAdvice"/> </bean> // after <bean class="...ProxyFactoryBean"> <property name="target" ref="myService"/> <property name="interceptorNames" value="myAdvice"/> </bean>
Defensive patterns
Strategy: validation
Validate before calling
// Before relying on the FactoryBean, ensure target/interface is resolvable
if (proxyFactoryBean.getTargetSource() == null
&& proxyFactoryBean.getTarget() == null
&& proxyFactoryBean.getProxiedInterfaces().length == 0
&& !proxyFactoryBean.isProxyTargetClass()) {
throw new IllegalStateException("ProxyFactoryBean needs a target, targetName, proxyInterfaces, or proxyTargetClass=true");
} Prevention
- Always set target/targetName or proxyInterfaces on ProxyFactoryBean.
- Set proxyTargetClass=true to skip interface auto-detection.
- Avoid pointing targetName at another FactoryBean that resolves lazily.
When it happens
Trigger: ProxyFactoryBean configured with interceptorNames but neither a target nor a targetSource set, AND not in proxyTargetClass mode, AND autodetectInterfaces is left on (default). Also happens when the target bean is itself a FactoryBean that has not been initialized yet.
Common situations: XML/config lists only interceptorNames and omits 'target'/'targetName'; pointing targetName at a prototype FactoryBean whose type cannot be resolved eagerly; misordering bean definitions so the target is not yet available; circular bean references during initialization.
Related errors
- No BeanFactory available anymore (probably due to serializat
- No BeanFactory available anymore (probably due to serializat
- Target required after globals
- Can only use global advisors or interceptors with a Listable
- No BeanFactory available anymore (probably due to serializat
AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04).
Data as JSON: /data/errors/a039aca4ac1f35fe.json.
Report an issue: GitHub.