spring-projects/spring-framework · error · FactoryBeanNotInitializedException

FactoryBean is not fully initialized yet

Error message

FactoryBean is not fully initialized yet

What it means

ScopedProxyFactoryBean.getObject throws FactoryBeanNotInitializedException when the cached proxy field is still null, meaning setBeanFactory has not yet executed. The FactoryBean contract requires initialisation before object retrieval; without the proxy there is nothing to return.

Source

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

			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());
	}


	@Override
	public @Nullable Object getObject() {
		if (this.proxy == null) {
			throw new FactoryBeanNotInitializedException();
		}
		return this.proxy;
	}

	@Override
	public @Nullable Class<?> getObjectType() {
		if (this.proxy != null) {
			return this.proxy.getClass();
		}
		return this.scopedTargetSource.getTargetClass();
	}

	@Override
	public boolean isSingleton() {
		return true;
	}

}

View on GitHub (pinned to e8729d0438)

Solutions

  1. Let the container fully initialise the FactoryBean (setTargetBeanName + setBeanFactory) before retrieving the object.
  2. Obtain the scoped proxy through the ApplicationContext (context.getBean(name)) rather than calling getObject() manually.
  3. If registering programmatically, call setBeanFactory last and only then access getObject().

Example fix

// before
ScopedProxyFactoryBean fb = new ScopedProxyFactoryBean();
Object o = fb.getObject(); // proxy still null -> error 111

// after
ScopedProxyFactoryBean fb = new ScopedProxyFactoryBean();
fb.setTargetBeanName("scopedTarget");
fb.setBeanFactory(applicationContext.getBeanFactory());
Object o = fb.getObject();
Defensive patterns

Strategy: validation

Validate before calling

if (scopedProxyFactoryBean.getObjectType() == null && /* not initialised */ true) {
    throw new IllegalStateException("FactoryBean not initialised; call setBeanFactory first");
}

Prevention

When it happens

Trigger: Calling scopedProxyFactoryBean.getObject() before the Spring container has called setBeanFactory (e.g. during a premature programmatic lookup).

Common situations: Programmatically instantiating ScopedProxyFactoryBean and calling getObject() directly; bean definition ordering that triggers early getObject before initialisation.

Related errors


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