The spring boot exception org.springframework.beans.BeanInstantiationException: Failed to instantiate: No default constructor found occurs when no default constructor is found in a bean when the bean is created. The bean is automatically wired using the @Autowired annotation. If an exception occurs in the bean, the bean will not be auto-wired. If the bean does not have the default constructor method, the exception BeanInstantiationException will be thrown.
The spring boot bean will not be instantiated if the default constructor is missing or not available. If the spring boot application begins, the bean classes will be instantiated and loaded in the application context. If the bean does not have a default bean constructor when the bean is instantiated, the org.springframework.beans.BeanInstantiationException exception would be thrown.
The bean object is created using the default bean constructor. If the java class does not have a default constructor, the bean cannot be created. The spring boot framework creates an instance of the class using the constructor and assigns values using the setter method.
As the bean is created at the beginning of the spring boot application, this exception will be thrown at the start of the application. The java class will have a default constructor. The default constructormust be available if a parametrized constructor is created. If the default constructor is not available or accessible, the bean will not be initiated. Consequently, the exception will be thrown.
Exception
The exception org.springframework.beans.BeanInstantiationException: Failed to instantiate: No default constructor found stack trace would be similar to the one seen below.
2020-12-09 19:25:52.390 WARN 66152 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 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.<init>()
2020-12-09 19:25:52.397 INFO 66152 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-09 19:25:52.425 ERROR 66152 --- [ main] o.s.boot.SpringApplication : Application run failed
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.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1318) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1216) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:571) ~[spring-beans-5.3.1.jar:5.3.1]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.yawintutor.Department]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.yawintutor.Department.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1310) ~[spring-beans-5.3.1.jar:5.3.1]
... 17 common frames omitted
Caused by: java.lang.NoSuchMethodException: com.yawintutor.Department.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_101]
at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_101]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) ~[spring-beans-5.3.1.jar:5.3.1]
... 18 common frames omitted
Root Cause
When the spring boot application is starting, the Spring boot application loads the beans into the ApplicationContext. Subsequently, the dependent beans are added to produce other types of beans.A bean is created using the default constructor in the class. If the default constructor is unavailable orinaccessible, the bean will not be created and loaded in the spring boot application context.
How to reproduce this exception
The spring boot application throws this exception if the default constructor method is unavailable or inaccessible in the class. Create a bean class without a default constructor. If the spring boot application starts loading all the bean classes, the exception will be “org.springframework.beans.BeanInstantiationException: Failed to instantiate: No default constructor found”
package com.yawintutor;
import org.springframework.stereotype.Component;
@Component
public class Department {
public Department(Lion lion) {
}
public Department(Tiger tiger) {
}
}
package com.yawintutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Employee {
@Autowired
Department department;
}
Output
2020-12-09 19:25:52.390 WARN 66152 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: 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.<init>()
2020-12-09 19:25:52.397 INFO 66152 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-09 19:25:52.425 ERROR 66152 --- [ main] o.s.boot.SpringApplication : Application run failed
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.<init>()
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1318) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1216) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:571) ~[spring-beans-5.3.1.jar:5.3.1]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.yawintutor.Department]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.yawintutor.Department.<init>()
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:83) ~[spring-beans-5.3.1.jar:5.3.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1310) ~[spring-beans-5.3.1.jar:5.3.1]
... 17 common frames omitted
Caused by: java.lang.NoSuchMethodException: com.yawintutor.Department.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_101]
at java.lang.Class.getDeclaredConstructor(Class.java:2178) ~[na:1.8.0_101]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:78) ~[spring-beans-5.3.1.jar:5.3.1]
... 18 common frames omitted
Solution 1
The exception would display the name of the class where the default constructor is unavailable or inaccessible. Identify the class and go through the constructor methods. If the default constructor method is not available, add a new default constructor method. This is going to solve the exception. The example below shows the default constructorthat is newly added.
package com.yawintutor;
import org.springframework.stereotype.Component;
@Component
public class Department {
public Department() {
}
public Department(Lion lion) {
}
public Department(Tiger tiger) {
}
}
Solution 2
If the bean contains more than one default constructor, go through the default constructor and remove multiple default constructor. If a default constructor is available in the class, the exception will not be thrown. In the example below, the department class contains one default constructor and the additional default constructors are removed from this class.
package com.yawintutor;
import org.springframework.stereotype.Component;
@Component
public class Department {
public Department(Lion lion) {
}
}