spring-projects/spring-framework · error · BeanInstantiationException

Constructor threw exception

Error message

Constructor threw exception

What it means

Thrown as BeanInstantiationException by instantiateClass(Constructor, args) at BeanUtils.java:220 when Constructor.newInstance raises InvocationTargetException — i.e. the constructor ran and the constructed object's constructor body threw. The framework unwraps it via ex.getTargetException() and stores that as the cause, so the real application exception is preserved.

Solutions

  1. Inspect getCause()/getRootCause() of the BeanInstantiationException — it is the original exception from the constructor.
  2. Reproduce by calling 'new Bean(args...)' directly to confirm the constructor throws.
  3. Fix the invariants/inputs so the constructor succeeds, or move side-effecting logic out of the constructor.
  4. Ensure required dependencies/fields are supplied (non-null) to the constructor.

Example fix

// before
public UserService(Repo repo) {
    this.repo = Objects.requireNonNull(repo);
    this.cache = repo.loadAll(); // throws if DB down
}

// after
public UserService(Repo repo) {
    this.repo = Objects.requireNonNull(repo);
    this.cache = new HashMap<>(); // lazy-load later
}
Defensive patterns

Strategy: try-catch

Validate before calling

// Smoke-test the constructor with sample/validated args
try {
    Constructor<T> c = clazz.getDeclaredConstructor(...);
    // validate inputs against @NotNull/Bean Validation before invoking
} catch (Exception ignore) {}

Type guard

// No static guard possible for arbitrary constructor side effects;
// wrap calls and inspect the cause.
static <T> Optional<T> safeNew(Constructor<T> c, Object[] args) {
    try { return Optional.of(BeanUtils.instantiateClass(c, args)); }
    catch (BeanInstantiationException e) { return Optional.empty(); }
}

Try / catch

try {
    BeanUtils.instantiateClass(ctor, args);
} catch (BeanInstantiationException ex) {
    Throwable real = ex.getCause(); // original exception from ctor body
    // classify: missing dependency, bad input, or environment failure
    throw new IllegalStateException("constructor failed", real);
}

Prevention

When it happens

Trigger: The bean's constructor itself throws any Throwable (NullPointerException, IllegalStateException, IllegalArgumentException, an application domain exception, a failed assertion, an assertion in a required field check). The constructor was found and invoked but the object refused to initialize.

Common situations: Constructor that validates arguments and throws on bad input; constructor performing I/O/DB calls that fail; @PostConstruct-style logic accidentally placed in the constructor; null-checks with Objects.requireNonNull on missing dependencies; failing static initialization triggered during construction.

Related errors


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

Appendix: source

Thrown at spring-beans/src/main/java/org/springframework/beans/BeanUtils.java:220

					}
					else {
						argsWithDefaultValues[i] = args[i];
					}
				}
				return ctor.newInstance(argsWithDefaultValues);
			}
		}
		catch (InstantiationException ex) {
			throw new BeanInstantiationException(ctor, "Is it an abstract class?", ex);
		}
		catch (IllegalAccessException ex) {
			throw new BeanInstantiationException(ctor, "Is the constructor accessible?", ex);
		}
		catch (IllegalArgumentException ex) {
			throw new BeanInstantiationException(ctor, "Illegal arguments for constructor", ex);
		}
		catch (InvocationTargetException ex) {
			throw new BeanInstantiationException(ctor, "Constructor threw exception", ex.getTargetException());
		}
	}

	/**
	 * Return a resolvable constructor for the provided class, either a primary or single
	 * public constructor with arguments, a single non-public constructor with arguments
	 * or simply a default constructor.
	 * <p>Callers have to be prepared to resolve arguments for the returned constructor's
	 * parameters, if any.
	 * @param clazz the class to check
	 * @throws IllegalStateException in case of no unique constructor found at all
	 * @since 5.3
	 * @see #findPrimaryConstructor
	 */
	@SuppressWarnings("unchecked")
	public static <T> Constructor<T> getResolvableConstructor(Class<T> clazz) {
		Constructor<T> ctor = findPrimaryConstructor(clazz);
		if (ctor != null) {

View on GitHub (pinned to 69bf83ad71)