The spring boot exception BeanInstantiationException: Failed to instantiate happens when the bean can’t instantiate when auto-wiring in another bean. If the bean is instantiated in another bean, the bean either throws an exception or fails to create an object. BeanInstantiationException will be fired on runtime when the bean is created and dynamically loaded in the application.

The exception BeanInstantiationException: Failed to instantiate will be threw mostly for three reasons. If the bean implementation class is not available or has not been added to the java class path, the bean could not be loaded into the spring boot context. If the bean is instantiated using the abstract class of the bean, the abstract class does not locate the implemented class in the spring boot context.

If the bean class is created and available in the java class path, then the bean will throw exception either the default constructor is not available or can not be invoked. Otherwise, the default constructor will throw the exception while running the code. The BeanInstantiationException will be thrown if an error occurs in default constructor method.



Solution 1 – No default constructor found

If the default constructor is not found while auto-wiring the bean, the exception below will be thrown.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'department' defined in file [SpringBootBean/target/classes/com/yawintutor/Department.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.yawintutor.Department]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.yawintutor.Department.

If the root cause exception is displayed as No default constructor found then follow the link below to resolve this exception.

BeanInstantiationException: Failed to instantiate: No default constructor found



Solution 2 – Constructor threw exception

If the default constructor throws an exception while auto-wiring the bean, the exception below will be thrown.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'department' defined in file [/SpringBootBean/target/classes/com/yawintutor/Department.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.yawintutor.Department]: Constructor threw exception; nested exception is java.lang.NullPointerException

If the root cause exception is displayed as Constructor threw exception then follow the link below to resolve this exception.

BeanInstantiationException: Failed to instantiate: Constructor threw exception



Solution 3 – Factory method threw exception

If the bean is not available, try to create and load using the abstract class name and bean factory. The beans are not going to instantiate. In this case, the exception below will be thrown.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'animal' defined in class path resource [com/yawintutor/SpringConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.yawintutor.AbstractAnimal]: Factory method 'animal' threw exception; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'abstractAnimal' available

If the root cause exception is displayed as Factory method threw exception then follow the link below to resolve this exception.

BeanInstantiationException: Failed to instantiate: Factory method threw exception



If you find some other type of BeanInstantiationException, please add it to the comments section, we will provide you with the solution.



Leave a Reply