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 application. Apart from many goodies, it also provides a DI and IOC container which initialize objects and their dependencies and assemble them together. The Java classes created and maintained by Spring are called Spring bean. At the startup, when Spring framework initializes 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 wire dependencies together, you should read first few recipes of Spring Recipes book, where you will find a good explanation of IOC and DI container.

In this article, I'll share two of the most common reasons of "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.




1) No default constructor on Spring Bean
One of the common mistakes Java programmer 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 developer's responsibility.

Many Java programmer define 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 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.


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 "Caused By" keyword, this often gives clue about the real error which is causing the problem. Sometimes this can be a ClassNotFoundException while other time a NoClassDefFoundError.

Here is a code snippet which defines beans in Spring configuration and how one single bean is used as dependency for several other beans. The bean is created and maintained by 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 "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)
  • Difference between Setter and Constructor injection in Spring? (answer)
  • Difference between BeanFactory and ApplicationContext in Spring? (answer)
  • How to call stored procedure using Spring framework in Java? (example)
  • What is bean scope in Spring framework? (answer)
  • How to implement LDAP authentication using Spring Security? (example)
  • How to implement RBAC (Role based access control) using Spring? (tutorial)
  • How to limit the number of user session using Spring Security? (solution)
  • 5 Books to Learn Spring Framework (books)
  • How to use JDBC database connection pooling with Spring? (tutorial)


References
Spring BeanCreationException
Spring in Action


1 comment :

kotaadithya kumar said...

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.

Post a Comment