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) at BeanUtils.java:143 when getDeclaredConstructor() (no-arg) throws NoSuchMethodException AND findPrimaryConstructor(clazz) also returns null. So the class has neither a default no-arg constructor nor a discoverable primary/canonical constructor (Kotlin primary or record canonical). The NoSuchMethodException is attached as the cause.

Solutions

  1. Add a no-arg constructor to the class (optionally alongside others).
  2. Use instantiateClass(Constructor, args) with the specific constructor and supply arguments.
  3. For Java records use the canonical constructor (findPrimaryConstructor handles it) — make sure Kotlin reflect is present if mixing types.
  4. Use getResolvableConstructor to pick the right constructor explicitly.

Example fix

// before
public class User {
    public User(String name, int age) { ... } // only ctor
}
BeanUtils.instantiateClass(User.class);

// after
public class User {
    public User() {}
    public User(String name, int age) { ... }
}
Defensive patterns

Strategy: validation

Validate before calling

// Check for a usable no-arg or primary constructor first
Constructor<?> c;
try { c = clazz.getDeclaredConstructor(); }
catch (NoSuchMethodException e) {
    c = BeanUtils.findPrimaryConstructor(clazz);
}
if (c == null) {
    // supply explicit constructor + args instead
}

Type guard

static boolean hasDefaultOrPrimary(Class<?> c) {
    try { c.getDeclaredConstructor(); return true; }
    catch (NoSuchMethodException e) { return BeanUtils.findPrimaryConstructor(c) != null; }
}

Try / catch

try {
    BeanUtils.instantiateClass(clazz);
} catch (BeanInstantiationException ex) {
    if (ex.getCause() instanceof NoSuchMethodException) {
        Constructor<?> c = clazz.getDeclaredConstructors()[0];
        Object bean = BeanUtils.instantiateClass(c, suppliedArgs);
    } else throw ex;
}

Prevention

When it happens

Trigger: Instantiating a Java class that declares only parameterized constructors (no no-arg ctor) and is neither a Kotlin class with a primary constructor nor a Java record. getDeclaredConstructor() fails, findPrimaryConstructor returns null, and the exception is raised.

Common situations: @ConfigurationProperties/value objects that only have an all-args constructor; domain classes with required fields and no default ctor; instantiating a class that only has constructors with @Autowired; legacy POJOs missing the implicit default constructor after adding a custom constructor.

Related errors


AI-assisted analysis of spring-projects/spring-framework@69bf83ad71 (2026-08-09). Data as JSON: /api/errors/bcac2ac8d3664bab. Report an issue: GitHub.

Appendix: 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 instance

View on GitHub (pinned to 69bf83ad71)