Solving org.springframework.beans.factory.BeanCreationException: Error creating bean with name X [Java Spring]

If you are using Spring framework in your Java application and getting this error during startup it means Spring is not able to initialize the bean X and add into its application context, Why? There could be multiple reasons e.g. a typo on spring bean name. Let's take a closer look at the stack trace to find out the real reason:

BeanInstantiationException: Could not instantiate bean class [X]: No default constructor found; nested exception is java.lang.NoSuchMethodException: X.()

Here X is the class, which is declared as Spring bean. The error clearly says that default constructor is not present in class X. By default, Spring will try to instantiate beans by calling the default or no-argument constructor and if it doesn't find the default, no-argument constructor then it is not able to initialize the bean and hence it throws the BeanInstantiationException during startup, the real reason is also appended into stack trace as NoSuchMethodException.

If you remember, Java by default adds a default constructor in every class but many programmers don't know that it only does that if you don't have any constructor in the class if by any chance you have defined a constructor which takes a parameter, then Java will not add it.

This is also one of the reasons I suggest to always add default constructor in Java class, no matter whether you have more constructor or not. Java allows constructor overloading and you should take advantage of that.

Now, it depends on how you are instructing Spring to instantiate your bean? If you are using auto-wiring e.g. @Autowired annotation, and you also have a suitable constructor define then just annotated that with @Autwired annotation, as shown below:

public class X{
private D d;

public X(Y dependency){
this.d = depenency;


Your problem will be solved.

Btw, if you are using XML based configuration then make sure you are using right constructor on your config.

Also, as per Spring Java documentation, A BeanCrationException is thrown when a BeanFactory encounters an error while attempting to create a bean from bean definition. It doesn't say anything about the error, that's why you need to look at the nested exception to find the actual cause. For example, Like in this case, the nested error was BeanInstantiationException, which indicate instantiation of a bean failed.

You should always pay attention to detailed stack trace as It also carries the offending bean class and reason e.g., in this case, it was trying to instantiate bean by invoking default constructor and couldn't find it. By looking at the error, you can conclude that whether you need to add default constructor or Spring is invoking a wrong constructor, which means a missing config somehow.

Sometimes BeanInstantiationException is also throwing in Spring is not able to find the bean class in the classpath. That time you will see either NoClassDefFoundError or ClassNotFoundException as nested exception in the stack trace.

Solving org.springframework.beans.factory.BeanCreationException: Error creating bean with name X [Java Spring]

Other Related Error and Exception Tutorials for Java Programmers
  • java.lang.ClassNotFoundException : org.Springframework.Web.Context.ContextLoaderListener (solution)
  • java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet in Java Spring MVC Application [solution]
  • How to solve java.sql.BatchUpdateException: String or binary data would be truncated (guide)
  • How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver error? (hint)
  • 5 Reasons of NoClassDefFoundError in Java? (tutorial)
  • How to fix Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger (solution)
  • Cause of "java.lang.SecurityException: Missing required Permissions manifest attribute in main jar" [solution]
  • How to fix "illegal start of expression" compile time error in Java? (tutorial)
  • How to connect to MySQL database in Java? (tutorial)
  • How to fix "Error: Could not find or load main class" in Eclipse? (guide)
  • java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory error (solution)

  • java.sql.SQLException: No suitable driver found for 'jdbc:mysql://localhost:3306/mysql [Solution]

  • How to deal with "No JVM installation found, please install 64-bit JDK" error? (solution)
  • How to fix unable to find certification path error in Java SSL? (guide)
  • How to avoid ConcurrentModificationException in Java? (tutorial)
  • How to solve "could not create the Java virtual machine" error in Java? (solution)
  • Common reasons of java.lang.ArrayIndexOutOfBoundsException in Java? (solution)
  • 10 common reasons of java.lang.NumberFormatException in Java? (tutorial)
  • java.sql.SQLServerException: The index 58 is out of range - JDBC (solution)
  • Fixing java.lang.unsupportedclassversionerror unsupported major.minor version 50.0 (solution)
  • How to fix 'javac' is not recognized as an internal or external command (solution)
  • Cause and solution of "class, interface, or enum expected" compiler error in Java? (fix)
  • How to solve "variable might not have initialized" compile time error in Java? (answer)
  • How to solve java.lang.classnotfoundexception oracle.jdbc.driver.oracledriver? (solution)

Spring BeanCreationException
Spring in Action

Thanks for reading this article so far. If you like this troubleshooting guide and my explanation of Spring's BeanCreationException then please share with your friends and colleagues. If you have any question or feedback, please drop a comment.

No comments:

Post a Comment