{"id":"d4687a7a6af8bdce","repo":"spring-projects/spring-framework","slug":"specified-class-is-an-interface","errorCode":null,"errorMessage":"Specified class is an interface","messagePattern":"Specified class is an interface","errorType":"exception","errorClass":"BeanInstantiationException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/BeanUtils.java","lineNumber":102,"sourceCode":"\t\t\tdouble.class, 0D,\n\t\t\tchar.class, '\\0');\n\n\tprivate static final boolean KOTLIN_REFLECT_PRESENT = KotlinDetector.isKotlinReflectPresent();\n\n\n\t/**\n\t * Convenience method to instantiate a class using its no-arg constructor.\n\t * @param clazz class to instantiate\n\t * @return the new instance\n\t * @throws BeanInstantiationException if the bean cannot be instantiated\n\t * @see Class#newInstance()\n\t * @deprecated following the deprecation of {@link Class#newInstance()} in JDK 9\n\t */\n\t@Deprecated(since = \"5.0\")\n\tpublic static <T> T instantiate(Class<T> clazz) throws BeanInstantiationException {\n\t\tAssert.notNull(clazz, \"Class must not be null\");\n\t\tif (clazz.isInterface()) {\n\t\t\tthrow new BeanInstantiationException(clazz, \"Specified class is an interface\");\n\t\t}\n\t\ttry {\n\t\t\treturn clazz.newInstance();\n\t\t}\n\t\tcatch (InstantiationException ex) {\n\t\t\tthrow new BeanInstantiationException(clazz, \"Is it an abstract class?\", ex);\n\t\t}\n\t\tcatch (IllegalAccessException ex) {\n\t\t\tthrow new BeanInstantiationException(clazz, \"Is the constructor accessible?\", ex);\n\t\t}\n\t}\n\n\t/**\n\t * Instantiate a class using its 'primary' constructor (for Kotlin classes,\n\t * potentially having default arguments declared) or its default constructor\n\t * (for regular Java classes, expecting a standard no-arg setup).\n\t * <p>Note that this method tries to set the constructor accessible\n\t * if given a non-accessible (that is, non-public) constructor.","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/e8729d043887bf0d0baf91e062e909b56eb2b708/spring-beans/src/main/java/org/springframework/beans/BeanUtils.java#L84-L120","documentation":"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.","triggerScenarios":"Calling BeanUtils.instantiate(SomeInterface.class) where SomeInterface is an interface. This path is only reachable via the deprecated method at BeanUtils.java:99-103.","commonSituations":"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.","solutions":["Switch to BeanUtils.instantiateClass(clazz) (non-deprecated) and pass a concrete implementation class, not the interface.","Pick the concrete implementation: instantiateClass(HashMap.class) instead of Map.class.","If loading by name, validate clazz.isInterface() before calling and select a default impl.","Review bean configuration to ensure the declared class is an instantiable concrete type."],"exampleFix":"// before (deprecated + interface)\nBeanUtils.instantiate(Map.class); // -> BeanInstantiationException\n\n// after\nBeanUtils.instantiateClass(HashMap.class);","handlingStrategy":"type-guard","validationCode":"if (clazz.isInterface()) {\n  throw new IllegalArgumentException(\"Provide a concrete class, not interface \" + clazz);\n}","typeGuard":"public static boolean isInstantiableClass(Class<?> c) {\n  return c != null && !c.isInterface() && !Modifier.isAbstract(c.getModifiers());\n}","tryCatchPattern":"try { BeanUtils.instantiate(clazz); }\ncatch (BeanInstantiationException e) { /* pick a concrete impl instead */ }","preventionTips":["Migrate off the deprecated instantiate(Class) to instantiateClass(Class).","Validate !isInterface() before reflective instantiation.","Keep a registry mapping interfaces to default implementations."],"tags":["spring-beans","beanutils","instantiation","interface","deprecated"],"analyzedSha":"e8729d043887bf0d0baf91e062e909b56eb2b708","analyzedAt":"2026-08-04T19:07:39.725Z","schemaVersion":2}