spring-projects/spring-framework · error · BeanInstantiationException
Failed to instantiate method
Error message
Failed to instantiate method
What it means
Thrown as a BeanInstantiationException when invoking the constructor or factory method of a bean via reflection throws any Throwable. BeanInstanceSupplier.instantiate wraps the underlying cause (IllegalAccessException, InvocationTargetException, error inside the constructor, etc.) into a BeanInstantiationException so a single, consistent exception type surfaces from bean creation.
Source
Thrown at spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanInstanceSupplier.java:368
registeredBean.getBeanName(), registeredBean.getBeanFactory());
}
return BeanUtils.instantiateClass(constructor, args);
}
if (executable instanceof Method method) {
Object target = null;
String factoryBeanName = registeredBean.getMergedBeanDefinition().getFactoryBeanName();
if (factoryBeanName != null) {
target = registeredBean.getBeanFactory().getBean(factoryBeanName, method.getDeclaringClass());
}
else if (!Modifier.isStatic(method.getModifiers())) {
throw new IllegalStateException("Cannot invoke instance method without factoryBeanName: " + method);
}
try {
ReflectionUtils.makeAccessible(method);
return method.invoke(target, args);
}
catch (Throwable ex) {
throw new BeanInstantiationException(method, ex.getMessage(), ex);
}
}
throw new IllegalStateException("Unsupported executable " + executable.getClass().getName());
}
private static String toCommaSeparatedNames(Class<?>... parameterTypes) {
return Arrays.stream(parameterTypes).map(Class::getName).collect(Collectors.joining(", "));
}
/**
* Performs lookup of the {@link Executable}.
*/
abstract static class ExecutableLookup {
abstract Executable get(RegisteredBean registeredBean);
}View on GitHub (pinned to 69bf83ad71)
Solutions
- Read the 'caused by' of the BeanInstantiationException to see the real exception thrown by the constructor and fix it at the source.
- If access fails, ensure the constructor/factory method is registered for reflection (ExecutableMode.INVOKE) in native image, typically via the AOT runtime hints.
- Confirm the constructor argument types/count match what the supplier is passing (signature drift after refactor).
- Initialize required state before bean creation (provide constructor args, properties, or environment values) so the constructor does not throw.
- Unit-test the constructor/factory method in isolation to reproduce the failure without Spring.
Example fix
// before: constructor throws because a required arg is null
public MyService(Dependency dep) {
this.dep = Objects.requireNonNull(dep); // NPE here
}
// after: ensure the dependency is provided/registered before instantiation
@Configuration
class Cfg {
@Bean Dependency dep() { return new DefaultDependency(); }
@Bean MyService myService(Dependency dep) { return new MyService(dep); }
} Defensive patterns
Strategy: try-catch
Validate before calling
// Smoke-test the constructor in isolation before Spring invokes it MyBean b = new MyBean(requiredArg1, requiredArg2); Assert.notNull(b, "bean must construct");
Try / catch
try {
return beanFactory.getBean(MyBean.class);
} catch (BeanInstantiationException ex) {
Throwable cause = ex.getCause();
throw new IllegalStateException("Constructor of " + ex.getBeanClass() + " failed", cause);
} Prevention
- Unit-test bean constructors with realistic inputs before wiring them in Spring.
- Register constructors for reflection (ExecutableMode.INVOKE) in native image.
- Validate required properties before construction (Objects.requireNonNull in the constructor).
- Keep constructor argument types stable across builds to avoid AOT drift.
When it happens
Trigger: method.invoke(target, args) (or BeanUtils.instantiateClass) throws because the constructor/factory method itself raised an exception, the JVM denied access, the bean threw in its constructor, an argument type mismatch caused a runtime exception, or a static initializer failed.
Common situations: Bean constructor throws NullPointerException/IllegalArgumentException because of bad state; required property missing because bean is being instantiated before its @PostConstruct-style init; native-image missing reflection registration causing IllegalAccess; constructor argument types changed between compile and AOT metadata; third-party library throwing in its constructor (driver init, connection pool).
Related errors
- Failed to load Class [
- Unsatisfied dependency expressed through injection point
- %s cannot be found on %s
- failed to generate code for bean definition
- instance supplier is not supported
AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09).
Data as JSON: /api/errors/9198e4f61d29a128.
Report an issue: GitHub.