spring-projects/spring-framework · error · IllegalStateException

No BeanFactory available anymore (probably due to serializat

Error message

No BeanFactory available anymore (probably due to serialization) - cannot resolve target with name '{}'

What it means

Thrown as IllegalStateException by ProxyFactoryBean.freshTargetSource when targetName is set but this.beanFactory is null. The transient BeanFactory is required to resolve the named target bean. As with the other deserialization errors, the field is lost when the proxy crosses a serialization boundary.

Source

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

		// We need to convert to an Advisor if necessary so that our source reference
		// matches what we find from superclass interceptors.
		addAdvisor(namedBeanToAdvisor(next));
	}

	/**
	 * Return a TargetSource to use when creating a proxy. If the target was not
	 * specified at the end of the interceptorNames list, the TargetSource will be
	 * this class's TargetSource member. Otherwise, we get the target bean and wrap
	 * it in a TargetSource if necessary.
	 */
	private TargetSource freshTargetSource() {
		if (this.targetName == null) {
			// Not refreshing target: bean name not specified in 'interceptorNames'
			return this.targetSource;
		}
		else {
			if (this.beanFactory == null) {
				throw new IllegalStateException("No BeanFactory available anymore (probably due to serialization) " +
						"- cannot resolve target with name '" + this.targetName + "'");
			}
			if (logger.isDebugEnabled()) {
				logger.debug("Refreshing target with name '" + this.targetName + "'");
			}
			Object target = this.beanFactory.getBean(this.targetName);
			return (target instanceof TargetSource targetSource ? targetSource : new SingletonTargetSource(target));
		}
	}

	/**
	 * Convert the following object sourced from calling getBean() on a name in the
	 * interceptorNames array to an Advisor or TargetSource.
	 */
	private Advisor namedBeanToAdvisor(Object next) {
		try {
			return this.advisorAdapterRegistry.wrap(next);
		}

View on GitHub (pinned to 69bf83ad71)

Solutions

  1. Do not serialize ProxyFactoryBean-derived proxies across JVMs; look the bean up by name after deserialization.
  2. Inline the target object via setTarget instead of targetName if you must serialize (then no beanFactory lookup is needed).
  3. Mark such beans transient/non-serializable and rebuild them from the ApplicationContext on the other side.

Example fix

// before
// (xml) <property name="targetName" value="svc"/>
Object p = ctx.getBean("svcProxy");
session.setAttribute("p", p);

// after
session.setAttribute("pName", "svcProxy");
Object p = ctx.getBean((String) session.getAttribute("pName"));
Defensive patterns

Strategy: fallback

Validate before calling

// If you must serialize, prefer setTarget over targetName so no factory lookup is needed
if (pfb.getTargetName() != null && willBeSerialized) {
  pfb.setTarget(beanFactory.getBean(pfb.getTargetName()));
  pfb.setTargetName(null);
}

Type guard

static boolean dependsOnBeanFactoryForTarget(ProxyFactoryBean fb) { return fb.getTargetName() != null; }

Try / catch

try { Object p = pfb.getObject(); }
catch (IllegalStateException e) {
  if (e.getMessage().contains("cannot resolve target with name")) { /* re-resolve by name */ }
  throw e;
}

Prevention

When it happens

Trigger: A ProxyFactoryBean with targetName set is deserialized, then a new instance (prototype mode or singleton re-cache via adviceChanged) is requested; freshTargetSource at lines 534-537 tries beanFactory.getBean(targetName) but beanFactory is null.

Common situations: Distributed session/cache serialization of AOP proxies whose target is a named bean; restart-from-snapshot; RMI/JMS transport of proxies.

Related errors


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