spring-projects/spring-framework · error · BeanInstantiationException
No default constructor found
Error message
No default constructor found
What it means
Thrown as BeanInstantiationException by instantiateClass(Class) when clazz.getDeclaredConstructor() (no-arg) raises NoSuchMethodException and findPrimaryConstructor(clazz) also returns null. This means the class has neither a no-arg constructor nor a resolvable primary/canonical constructor (Kotlin primary or Java record canonical), so Spring cannot pick one (BeanUtils.java:136-145).
Source
Thrown at spring-beans/src/main/java/org/springframework/beans/BeanUtils.java:143
* primary/default constructor was found, a {@link NoClassDefFoundError}
* or other {@link LinkageError} in case of an unresolvable class definition
* (for example, due to a missing dependency at runtime), or an exception thrown
* from the constructor invocation itself.
* @see Constructor#newInstance
*/
public static <T> T instantiateClass(Class<T> clazz) throws BeanInstantiationException {
Assert.notNull(clazz, "Class must not be null");
if (clazz.isInterface()) {
throw new BeanInstantiationException(clazz, "Specified class is an interface");
}
Constructor<T> ctor;
try {
ctor = clazz.getDeclaredConstructor();
}
catch (NoSuchMethodException ex) {
ctor = findPrimaryConstructor(clazz);
if (ctor == null) {
throw new BeanInstantiationException(clazz, "No default constructor found", ex);
}
}
catch (LinkageError err) {
throw new BeanInstantiationException(clazz, "Unresolvable class definition", err);
}
return instantiateClass(ctor);
}
/**
* Instantiate a class using its no-arg constructor and return the new instance
* as the specified assignable type.
* <p>Useful in cases where the type of the class to instantiate (clazz) is not
* available, but the type desired (assignableTo) is known.
* <p>Note that this method tries to set the constructor accessible if given a
* non-accessible (that is, non-public) constructor.
* @param clazz class to instantiate
* @param assignableTo type that clazz must be assignableTo
* @return the new instanceView on GitHub (pinned to e8729d0438)
Solutions
- Add a public no-arg constructor to the class.
- Resolve the constructor yourself with getDeclaredConstructor(argTypes...) and call instantiateClass(ctor, args).
- Use getResolvableConstructor to obtain a single unique constructor and supply matching arguments.
- For Kotlin classes ensure the kotlin-reflect dependency is present so the primary constructor can be found.
Example fix
// before
public class User { public User(String name) {} }
BeanUtils.instantiateClass(User.class); // no default ctor
// after
Constructor<User> c = User.class.getDeclaredConstructor(String.class);
BeanUtils.instantiateClass(c, "Alice"); Defensive patterns
Strategy: validation
Validate before calling
Constructor<?> def;
try { def = clazz.getDeclaredConstructor(); }
catch (NoSuchMethodException e) { def = null; }
if (def == null && BeanUtils.findPrimaryConstructor(clazz) == null) {
// resolve an explicit constructor and pass args instead
} Type guard
public static boolean hasDefaultOrPrimaryCtor(Class<?> c) {
try { c.getDeclaredConstructor(); return true; }
catch (NoSuchMethodException e) { return BeanUtils.findPrimaryConstructor(c) != null; }
} Try / catch
try { BeanUtils.instantiateClass(clazz); }
catch (BeanInstantiationException e) {
Constructor<?> c = clazz.getDeclaredConstructor(String.class);
BeanUtils.instantiateClass(c, arg);
} Prevention
- Add a no-arg constructor when you add required-args constructors.
- For Kotlin, keep kotlin-reflect on the classpath for primary-constructor detection.
- Resolve constructors explicitly with getDeclaredConstructor(types) when args are known.
When it happens
Trigger: instantiateClass(Foo.class) on a Java class whose only constructors take arguments and that is neither a Kotlin class with a primary constructor nor a record. The NoSuchMethodException is retained as the cause.
Common situations: Refactoring a class to add a required-args constructor and removing the no-arg one; instantiating an immutable/value type by class only; integration code assuming a default constructor exists.
Related errors
- Specified class is an interface
- Is it an abstract class?
- Is the constructor accessible?
- Unresolvable class definition
- Illegal arguments for constructor
AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04).
Data as JSON: /data/errors/bcac2ac8d3664bab.json.
Report an issue: GitHub.