spring-projects/spring-framework · error · AotBeanProcessingException

instance supplier is not supported

Error message

instance supplier is not supported

What it means

DefaultBeanRegistrationCodeFragments.getTarget refuses to compute the target class for a bean whose definition carries an instance supplier (setInstanceSupplier). AOT cannot serialize an arbitrary programmatic Supplier to source code, so any bean with an instance supplier is unsupported for native/AOT. Thrown as AotBeanProcessingException.

Source

Thrown at spring-beans/src/main/java/org/springframework/beans/factory/aot/DefaultBeanRegistrationCodeFragments.java:82

	private final Supplier<InstantiationDescriptor> instantiationDescriptor;


	DefaultBeanRegistrationCodeFragments(
			BeanRegistrationsCode beanRegistrationsCode, RegisteredBean registeredBean,
			BeanDefinitionMethodGeneratorFactory beanDefinitionMethodGeneratorFactory) {

		this.beanRegistrationsCode = beanRegistrationsCode;
		this.registeredBean = registeredBean;
		this.beanDefinitionMethodGeneratorFactory = beanDefinitionMethodGeneratorFactory;
		this.instantiationDescriptor = SingletonSupplier.of(registeredBean::resolveInstantiationDescriptor);
	}


	@Override
	public ClassName getTarget(RegisteredBean registeredBean) {
		if (hasInstanceSupplier()) {
			throw new AotBeanProcessingException(registeredBean, "instance supplier is not supported");
		}
		Class<?> target = extractDeclaringClass(registeredBean, this.instantiationDescriptor.get());
		while (target.getName().startsWith("java.") && registeredBean.isInnerBean()) {
			RegisteredBean parent = registeredBean.getParent();
			Assert.state(parent != null, "No parent available for inner bean");
			target = parent.getBeanClass();
		}
		return (target.isArray() ? ClassName.get(target.getComponentType()) : ClassName.get(target));
	}

	private Class<?> extractDeclaringClass(RegisteredBean registeredBean, InstantiationDescriptor instantiationDescriptor) {
		Class<?> declaringClass = ClassUtils.getUserClass(instantiationDescriptor.targetClass());
		if (instantiationDescriptor.executable() instanceof Constructor<?> ctor &&
				AccessControl.forMember(ctor).isPublic() && FactoryBean.class.isAssignableFrom(declaringClass)) {
			return extractTargetClassFromFactoryBean(declaringClass, registeredBean.getBeanType());
		}
		return declaringClass;
	}

View on GitHub (pinned to e8729d0438)

Solutions

  1. Replace the instance supplier with a constructor-based or static factory-method bean definition that AOT can code-generate.
  2. If you must keep the supplier, exclude the bean from native/AOT processing or run in JIT mode.
  3. Refactor to register the bean via @Configuration/@Bean so AOT has a statically analyzable creation path.

Example fix

// before
context.registerBean(MyBean.class, () -> new MyBean(config));
// after
@Configuration
class Cfg {
  @Bean MyBean myBean() { return new MyBean(cfg); }
}
Defensive patterns

Strategy: validation

Validate before calling

for (String name : context.getBeanDefinitionNames()) {
  BeanDefinition bd = context.getBeanFactory().getBeanDefinition(name);
  if (bd.getInstanceSupplier() != null) {
    log.warn("Bean {} uses an instance supplier -> unsupported by AOT", name);
  }
}

Type guard

boolean isAotCompatible(BeanDefinition bd) {
  return bd.getInstanceSupplier() == null;
}

Prevention

When it happens

Trigger: A bean registered programmatically via BeanDefinition.setInstanceSupplier(...) (or GenericApplicationContext.registerBean with a Supplier) is processed by the Spring AOT engine during getTarget.

Common situations: Spring Boot native-image build failing on beans registered with registerBean(Supplier) or setInstanceSupplier; functional bean registration (context.registerBean(MyType.class, () -> ...)) in a native-targeted app.

Related errors


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