spring-projects/spring-framework · error · BeanInstantiationException

Specified class is an interface

Error message

Specified class is an interface

What it means

Thrown as BeanInstantiationException by the deprecated BeanUtils.instantiate(Class) when the supplied class is a Java interface. Spring cannot instantiate an interface, so it short-circuits before attempting newInstance(). Prefer instantiateClass(Class) which is the non-deprecated replacement and throws the same message.

Source

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

			double.class, 0D,
			char.class, '\0');

	private static final boolean KOTLIN_REFLECT_PRESENT = KotlinDetector.isKotlinReflectPresent();


	/**
	 * Convenience method to instantiate a class using its no-arg constructor.
	 * @param clazz class to instantiate
	 * @return the new instance
	 * @throws BeanInstantiationException if the bean cannot be instantiated
	 * @see Class#newInstance()
	 * @deprecated following the deprecation of {@link Class#newInstance()} in JDK 9
	 */
	@Deprecated(since = "5.0")
	public static <T> T instantiate(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");
		}
		try {
			return clazz.newInstance();
		}
		catch (InstantiationException ex) {
			throw new BeanInstantiationException(clazz, "Is it an abstract class?", ex);
		}
		catch (IllegalAccessException ex) {
			throw new BeanInstantiationException(clazz, "Is the constructor accessible?", ex);
		}
	}

	/**
	 * Instantiate a class using its 'primary' constructor (for Kotlin classes,
	 * potentially having default arguments declared) or its default constructor
	 * (for regular Java classes, expecting a standard no-arg setup).
	 * <p>Note that this method tries to set the constructor accessible
	 * if given a non-accessible (that is, non-public) constructor.

View on GitHub (pinned to e8729d0438)

Solutions

  1. Switch to BeanUtils.instantiateClass(clazz) (non-deprecated) and pass a concrete implementation class, not the interface.
  2. Pick the concrete implementation: instantiateClass(HashMap.class) instead of Map.class.
  3. If loading by name, validate clazz.isInterface() before calling and select a default impl.
  4. Review bean configuration to ensure the declared class is an instantiable concrete type.

Example fix

// before (deprecated + interface)
BeanUtils.instantiate(Map.class); // -> BeanInstantiationException

// after
BeanUtils.instantiateClass(HashMap.class);
Defensive patterns

Strategy: type-guard

Validate before calling

if (clazz.isInterface()) {
  throw new IllegalArgumentException("Provide a concrete class, not interface " + clazz);
}

Type guard

public static boolean isInstantiableClass(Class<?> c) {
  return c != null && !c.isInterface() && !Modifier.isAbstract(c.getModifiers());
}

Try / catch

try { BeanUtils.instantiate(clazz); }
catch (BeanInstantiationException e) { /* pick a concrete impl instead */ }

Prevention

When it happens

Trigger: Calling BeanUtils.instantiate(SomeInterface.class) where SomeInterface is an interface. This path is only reachable via the deprecated method at BeanUtils.java:99-103.

Common situations: Passing a dynamically-loaded Class that is expected to be concrete but resolves to an interface; misconfigured bean class in XML/annotations pointing at an interface; reflective code that forgot to choose an implementation.

Related errors


AI-assisted analysis of spring-projects/spring-framework@e8729d0438 (2026-08-04). Data as JSON: /data/errors/d4687a7a6af8bdce.json. Report an issue: GitHub.