Sunday, April 21, 2024

2 Reasons of org.springframework.beans.factory.BeanCreationException: Error creating bean with name [Solution]

The Spring framework is one of the most popular frameworks for developing Java applications. Apart from many goodies, it also provides a DI and IOC container that initializes objects and their dependencies and assembles them together. The Java classes created and maintained by Spring are called Spring bean. At the startup, when the Spring framework initializes the system by creating objects and their dependencies depending upon @Autowired annotation or spring configuration XML file, it throws "org.springframework.beans.factory.BeanCreationException: Error creating a bean with name X" error if it is not able to instantiate a particular Spring bean.


There could be numerous reasons why Spring could not able to create a bean with name X, but clue always lies on the detailed stack trace. This error always has some underlying cause e.g. a ClassNotFoundException or a NoClassDefFoundError, which potentially signal a missing JAR file in the classpath.

In short, you should always give a detailed look at the stack trace of your error message and find out the exact cause of "org.springframework.beans.factory.BeanCreationException: Error creating a bean with name" error. 

The solution would vary accordingly. 

Btw,  If you are curious about how dependency injection works in Spring and how Spring initializes and wires dependencies together, you should read the first few recipes of Spring Recipes book, where you will find a good explanation of IOC and DI containers.

In this article, I'll share two of the most common reasons for "org.springframework.beans.factory.BeanCreationException: Error creating a bean with name" error in Spring-based Java application and their solutions.

These are just based on my limited experience with using Spring framework in core Java application and Java web application if you have come across any other reasons for BeanCreationException in Spring, don't forget to share with us in comments.

By the way, if you are new to the Spring framework then I also suggest you join a comprehensive and up-to-date course to learn Spring in depth. If you need recommendations, I highly suggest you take a look at these best Spring Framework courses, these are online, comprehensive and hands-on resource to learn modern Spring.





1. No default constructor on Spring Bean

One of the common mistakes Java programmers make is they forget to define a no-argument constructor in their Spring Bean. If you remember, a spring bean is nothing but a Java class instantiated and managed by Spring. 

If you also remember, Java compiler adds a default no-argument constructor if you don't define any, but if you do then it will not insert. It becomes the developer's responsibility.

Many Java programmer defines a constructor which accepts one or two-argument and forget about the default no-argument constructor, which result in org.springframework.beans.factory.BeanCreationException: Error creating bean with the name at runtime as shown below:


ERROR: org.springframework.web.servlet.DispatcherServlet - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'InterestRateController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.abc.project.model.service.InterestRateServiceImpl com.abc.project.controller.InterestRateController.InterestRateServ; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'InterestRateServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void com.abc.project.model.service.InterestRateServiceImpl.setInterestRateDAO(com.abc.project.model.dao.InterestRateDAO); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'InterestRateDAO' defined in file [C:\Users\zouhair\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\TESTER\WEB-INF\classes\com\abc\project\model\dao\InterestRateDAO.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.abc.project.model.dao.InterestRateDAO]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.abc.project.model.dao.InterestRateDAO.()
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)

The most important line in this stack trace is

"No default constructor found; nested exception is java.lang.NoSuchMethodException: com.abc.project.model.dao.InterestRateDAO.()"

which is often overlooked by Java programmers. So, either don't provide any constructor so that compiler can add the default constructor for you, and if you do provide parameterized constructor then don't forget to add a no-argument, default constructor in your bean to solve this error. 


2) Spring Bean dependent on third party library

If your Spring bean is using a third party library and that library is not available in the classpath at runtime, Spring will again throw
"org.springframework.beans.factory.BeanCreationException: Error creating a bean with name" error. 

When you look at the stack trace just look for the "Caused By" keyword, this often gives clues about the real error which is causing the problem. Sometimes this can be a ClassNotFoundException while other times a NoClassDefFoundError.

Here is a code snippet which defines beans in Spring configuration and how one single bean is used as a dependency for several other beans. The bean is created and maintained by the Spring IOC container.




That's all about how to resolve org.springframework.beans.factory.BeanCreationException: Error creating a bean with the name in Java. As I said, most of the time the clue of the root cause lies in the nested exception. 

You should always pay attention to the "Caused By" keyword. 

As I said before, this list is by no means complete and these are just a couple of reasons from numerous others which cause this problem. If you come across any other reasons in your project then please share with us.


Other Java Spring articles you may like to explore
  • Spring HelloWorld example using Dependency Injection (tutorial)
  • 21 Spring Boot Testing interview questions (spring boot questions)
  • Difference between Setter and Constructor Injection in Spring? (answer)
  • 25 Spring Security Interview Questions and Answers (spring security questions)
  • Difference between BeanFactory and ApplicationContext in Spring? (answer)
  • How to call stored procedures using the Spring framework in Java? (example)
  • What is the bean scope in the Spring framework? (answer)
  • How to implement LDAP authentication using Spring Security? (example)
  • Difference between @RequestParam and @PathVaraible in Spring? (answer)
  • How to implement RBAC (Role-based access control) using Spring? (tutorial)
  • How to limit the number of user sessions using Spring Security? (solution)
  • 5 Books to Learn Spring Framework (books)
  • How to use JDBC database connection pooling with Spring? (tutorial)

P.S. - If you want to learn how to develop RESTful Web Service using Spring MVC in-depth, I suggest you join these free Spring Framework courses. One of the best courses to learn REST with Spring MVC. 

4 comments:

  1. Nice Article.

    Most of the times we use place holders in our application, missing of place holders while bean creation also cause this issue. In other words unable to find the dependencies during object creation will lead to this error.

    ReplyDelete
  2. How to resolve bean factory errors

    ReplyDelete
  3. some time java version is not proper

    ReplyDelete
  4. How to solve this problem please tell me

    ReplyDelete