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
- Replace the instance supplier with a constructor-based or static factory-method bean definition that AOT can code-generate.
- If you must keep the supplier, exclude the bean from native/AOT processing or run in JIT mode.
- 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
- Avoid registerBean(Supplier)/setInstanceSupplier for native targets.
- Express beans via @Bean/@Configuration for AOT analyzability.
- Add a CI check that flags instance-supplier beans when native is enabled.
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
- Argument type mismatch: expected '{}' for value [{}]
- Failed to load Class [{}] from ClassLoader [{}]
- Unexpected MethodOverride subclass: {}
- Unsupported executable: {}
- {} cannot be found on {}
AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04).
Data as JSON: /data/errors/e0a49ff020f2e255.json.
Report an issue: GitHub.