The spring boot exception org.springframework.beans.factory.BeanCreationException: Error creating bean with name happens when a problem occurs when the BeanFactory creates a bean. If the BeanFactory encounters an error when creating a bean from either bean definition or auto-configuration, the BeanCreationException will be thrown. The exception Error creating bean with name defined in file happens most of the time in the @Autowired annotation.
If BeanCreationException is found in the spring boot application, the nested exception would reveal the root cause of the exception. There are multiple nested exceptions that trigger BeanCreationException: Error creating bean with name in the spring boot application.
The nested exception will help you to fix BeanCreationException. The following list describes the common root causes that are seen in the nested exception.
NoSuchBeanDefinitionException: No qualifying bean of type
This exception occurs when the bean is not available or defined while auto-wired in another class.
s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization – cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘zoo’: Unsatisfied dependency expressed through field ‘lion’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.yawintutor.Lion’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
If the root cause exception is displayed as No qualifying bean of type then follow the link below to resolve this exception.
NoSuchBeanDefinitionException: No qualifying bean of type
NoSuchBeanDefinitionException: No bean named available
This exception happens when you try to access a bean that is not available or is not defined in the spring boot context.
Exception in thread “main” org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named ‘lion1’ available
If the root cause exception is displayed as No qualifying bean of type then follow the link below to resolve this exception.
NoSuchBeanDefinitionException: No bean named available
BeanCurrentlyInCreationException: Error creating bean with name: Requested bean is currently in creation
This exception happens when two beans are in circular dependences with each other.
s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization – cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘department’ defined in file [/SpringBootBean/target/classes/com/yawintutor/Department.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘lion’ defined in file [/SpringBootBean/target/classes/com/yawintutor/Lion.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘tiger’ defined in file [/SpringBootBean/target/classes/com/yawintutor/Tiger.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name ‘lion’: Requested bean is currently in creation: Is there an unresolvable circular reference?
If the root cause exception is displayed as Requested bean is currently in creation then follow the link below to resolve this exception.
BeanCurrentlyInCreationException: Error creating bean with name: Requested bean is currently in creation
NoUniqueBeanDefinitionException: No qualifying bean of type available: expected single matching bean but found
This exception occurs when the bean is auto-wired that matches two or more loaded beans in the spring boot application context.
s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization – cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘zoo’: Unsatisfied dependency expressed through field ‘animal’; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type ‘com.yawintutor.Animal’ available: expected single matching bean but found 2: lion,tiger
If the root cause exception is displayed as expected single matching bean but found then follow the link below to resolve this exception.
NoUniqueBeanDefinitionException: No qualifying bean of type available: expected single matching bean but found
BeanInstantiationException: Failed to instantiate: 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
BeanInstantiationException: Failed to instantiate: 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
BeanInstantiationException: Failed to instantiate: 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 BeanCreationException, please add it to the comments section, we will provide you with the solution.