Let's get started with a Microservice Architecture with Spring Cloud:
Spring BeanCreationException
Last updated: May 11, 2024
1. Overview
In this tutorial, we’ll discuss the Spring org.springframework.beans.factory.BeanCreationException. It’s a very common exception thrown when the BeanFactory creates beans of the bean definitions, and encounteres a problem. This article will explore the most common causes of this exception, along with the solutions.
Further reading:
Intro to Inversion of Control and Dependency Injection with Spring
A quick introduction to the concepts of Inversion of Control and Dependency Injection, followed by a simple demonstration using the Spring Framework
BeanNameAware and BeanFactoryAware Interfaces in Spring
Have a look at working with the BeanNameAware and BeanFactoryAware interfaces in Spring.
Spring Functional Bean Registration
See how to register beans using the functional approach in Spring 5.
2. Cause: org.springframework.beans.factory.NoSuchBeanDefinitionException
By far, the most common cause of the BeanCreationException is Spring trying to inject a bean that doesn’t exist in the context.
For example, BeanA is trying to inject BeanB:
@Component
public class BeanA {
@Autowired
private BeanB dependency;
...
}
If a BeanB isn’t found in the context, then the following exception will be thrown (Error Creating Bean):
Error creating bean with name 'beanA': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private com.baeldung.web.BeanB cpm.baeldung.web.BeanA.dependency;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException:
No qualifying bean of type [com.baeldung.web.BeanB] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
To diagnose this type of issue, we’ll first make sure the bean is declared:
- either in an XML configuration file using the <bean /> element
- or in a Java @Configuration class via the @Bean annotation
- or is annotated with @Component, @Repository, @Service, @Controller, and classpath scanning is active for that package
We’ll also check that Spring actually picks up the configuration files or classes, and loads them into the main context.
Further reading:
Intro to Inversion of Control and Dependency Injection with Spring
A quick introduction to the concepts of Inversion of Control and Dependency Injection, followed by a simple demonstration using the Spring Framework
BeanNameAware and BeanFactoryAware Interfaces in Spring
Have a look at working with the BeanNameAware and BeanFactoryAware interfaces in Spring.
Spring Functional Bean Registration
See how to register beans using the functional approach in Spring 5.
3. Cause: org.springframework.beans.factory.NoUniqueBeanDefinitionException
Another similar cause for the bean creation exception is Spring trying to inject a bean by type, namely by its interface, and finding two or more beans implementing that interface in the context.
For example, BeanB1 and BeanB2 both implement the same interface:
@Component
public class BeanB1 implements IBeanB { ... }
@Component
public class BeanB2 implements IBeanB { ... }
@Component
public class BeanA {
@Autowired
private IBeanB dependency;
...
}
This will lead to the following exception being thrown by the Spring bean factory:
Error creating bean with name 'beanA': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private com.baeldung.web.IBeanB com.baeldung.web.BeanA.b;
nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException:
No qualifying bean of type [com.baeldung.web.IBeanB] is defined:
expected single matching bean but found 2: beanB1,beanB2
4. Cause: org.springframework.beans.BeanInstantiationException
4.1. Custom Exception
Next in line is a bean that throws an exception during its creation process. A simplified example to easily understand the problem is throwing an exception in the constructor of the bean:
@Component
public class BeanA {
public BeanA() {
super();
throw new NullPointerException();
}
...
}
As expected, this will lead to Spring failing fast with the following exception:
Error creating bean with name 'beanA' defined in file [...BeanA.class]:
Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class [com.baeldung.web.BeanA]:
Constructor threw exception;
nested exception is java.lang.NullPointerException
4.2. java.lang.InstantiationException
Another possible occurence of the BeanInstantiationException is defining an abstract class as a bean in XML; this has to be in XML because there’s no way to do it in a Java @Configuration file, and classpath scanning will ignore the abstract class:
@Component
public abstract class BeanA implements IBeanA { ... }
Here’s the XML definition of the bean:
<bean id="beanA" class="com.baeldung.web.BeanA" />
This setup will result in a similar exception:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'beanA' defined in class path resource [beansInXml.xml]:
Instantiation of bean failed;
nested exception is org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class [com.baeldung.web.BeanA]:
Is it an abstract class?;
nested exception is java.lang.InstantiationException
4.3. java.lang.NoSuchMethodException
If a bean has no default constructor, and Spring tries to instantiate it by looking for that constructor, this will result in a runtime exception:
@Component
public class BeanA implements IBeanA {
public BeanA(final String name) {
super();
System.out.println(name);
}
}
When the classpath scanning mechanism picks up this bean, the failure will be:
Error creating bean with name 'beanA' defined in file [...BeanA.class]: Instantiation of bean failed;
nested exception is org.springframework.beans.BeanInstantiationException:
Could not instantiate bean class [com.baeldung.web.BeanA]:
No default constructor found;
nested exception is java.lang.NoSuchMethodException: com.baeldung.web.BeanA.<init>()
A similar exception, but harder to diagnose, may occur when the Spring dependencies on the classpath don’t have the same version. This kind of version incompatibility may result in a NoSuchMethodException because of API changes. The solution to such a problem is to make sure all Spring libraries have the exact same version in the project.
5. Cause: org.springframework.beans.NotWritablePropertyException
Yet another possiblity is defining a bean, BeanA, with a reference to another bean, BeanB, without having the corresponding setter method in BeanA:
@Component
public class BeanA {
private IBeanB dependency;
...
}
@Component
public class BeanB implements IBeanB { ... }
Here’s the Spring XML Configuration:
<bean id="beanA" class="com.baeldung.web.BeanA">
<property name="beanB" ref="beanB" />
</bean>
Again, this can only occur in XML Configuration because when using Java @Configuration, the compiler will make this issue impossible to reproduce.
Of course, in order to solve this issue, we need to add the setter for IBeanB:
@Component
public class BeanA {
private IBeanB dependency;
public void setDependency(final IBeanB dependency) {
this.dependency = dependency;
}
}
6. Cause: org.springframework.beans.factory.CannotLoadBeanClassException
Spring throws this exception when it can’t load the class of the defined bean. This may occur if the Spring XML Configuration contains a bean that simply doesn’t have a corresponding class. For example, if class BeanZ doesn’t exist, the following definition will result in an exception:
<bean id="beanZ" class="com.baeldung.web.BeanZ" />
The root cause of the ClassNotFoundException and the full exception in this case is:
nested exception is org.springframework.beans.factory.BeanCreationException:
...
nested exception is org.springframework.beans.factory.CannotLoadBeanClassException:
Cannot find class [com.baeldung.web.BeanZ] for bean with name 'beanZ'
defined in class path resource [beansInXml.xml];
nested exception is java.lang.ClassNotFoundException: com.baeldung.web.BeanZ
7. Children of BeanCreationException
7.1. The org.springframework.beans.factory.BeanCurrentlyInCreationException
One of the subclasses of BeanCreationException is the BeanCurrentlyInCreationException. This usually occurs when using constructor injection, for example, in a case of circular dependencies:
@Component
public class BeanA implements IBeanA {
private IBeanB beanB;
@Autowired
public BeanA(final IBeanB beanB) {
super();
this.beanB = beanB;
}
}
@Component
public class BeanB implements IBeanB {
final IBeanA beanA;
@Autowired
public BeanB(final IBeanA beanA) {
super();
this.beanA = beanA;
}
}
Spring won’t be able to resolve this kind of wiring scenario and the end result will be:
org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'beanA':
Requested bean is currently in creation: Is there an unresolvable circular reference?
The full exception is very verbose:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'beanA' defined in file [...BeanA.class]:
Unsatisfied dependency expressed through constructor argument with index 0
of type [com.baeldung.web.IBeanB]: :
Error creating bean with name 'beanB' defined in file [...BeanB.class]:
Unsatisfied dependency expressed through constructor argument with index 0
of type [com.baeldung.web.IBeanA]: :
Error creating bean with name 'beanA': Requested bean is currently in creation:
Is there an unresolvable circular reference?;
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'beanA':
Requested bean is currently in creation:
Is there an unresolvable circular reference?;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'beanB' defined in file [...BeanB.class]:
Unsatisfied dependency expressed through constructor argument with index 0
of type [com.baeldung.web.IBeanA]: :
Error creating bean with name 'beanA':
Requested bean is currently in creation:
Is there an unresolvable circular reference?;
nested exception is org.springframework.beans.factory.BeanCurrentlyInCreationException:
Error creating bean with name 'beanA':
Requested bean is currently in creation: Is there an unresolvable circular reference?
7.2. The org.springframework.beans.factory.BeanIsAbstractException
This instantiation exception may occur when the Bean Factory attempts to retrieve and instantiate a bean that was declared as abstract:
public abstract class BeanA implements IBeanA {
...
}
We declare it in the XML Configuration as:
<bean id="beanA" abstract="true" class="com.baeldung.web.BeanA" />
If we try to retrieve BeanA from the Spring Context by name, like when instantiating another bean:
@Configuration
public class Config {
@Autowired
BeanFactory beanFactory;
@Bean
public BeanB beanB() {
beanFactory.getBean("beanA");
return new BeanB();
}
}
This will result in the following exception:
org.springframework.beans.factory.BeanIsAbstractException:
Error creating bean with name 'beanA': Bean definition is abstract
And the full exception stacktrace:
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'beanB' defined in class path resource
[org/baeldung/spring/config/WebConfig.class]: Instantiation of bean failed;
nested exception is org.springframework.beans.factory.BeanDefinitionStoreException:
Factory method
[public com.baeldung.web.BeanB com.baeldung.spring.config.WebConfig.beanB()] threw exception;
nested exception is org.springframework.beans.factory.BeanIsAbstractException:
Error creating bean with name 'beanA': Bean definition is abstract
8. Conclusion
In this article, we learned how to navigate the variety of causes and problems that may lead to a BeanCreationException in Spring, as well as developed a good grasp on how to fix all of these problems.
The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
















