Java, Spring, Spring Boot

List of Spring Bean (@Bean) related Exceptions or Errors with cause and commonly implemented solutions.

  1. Using Third Party Library but not adding it in class-path.

Defined Flow:

As we have all heard of IOC (Inversion of Control) in spring that is a framework for implementing automatic dependency injection. This manages object creation and its life-time, and also injects dependencies to the class path.

Note: This is only one of many implementations that IOC has to offer

Sample Stack Trace:

java.rmi.ServerError: Error occurred in server thread; nested exception is:
java.lang.NoClassDefFoundError: Could not initialize class com.hp.apps.nuncas.server.facade.RMINUNCASFacade_Stub
at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:333)
at sun.rmi.transport.Transport$1.run(Transport.java:159)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Transport.java:155)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:535)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:790)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:649)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
at sun.rmi.transport.StreamRemoteCall.exceptionReceivedFromServer(StreamRemoteCall.java:255)
at sun.rmi.transport.StreamRemoteCall.executeCall(StreamRemoteCall.java:233)
at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:142)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class

Key Words to look for in your stack trace:

  • java.lang.NoClassDefFoundError: Could not initialize class

Usual Solution:

  • Using native library? Make sure that is available first as NoClassDefFoundError is a subclass of java.lang.LinkageError which can come if one of it dependency is a native library.
  • There might be some start-up configuration that might be overriding your application class-path environment variables.
  • If you are running your program using jar dependencies and class was not defined in manifest file’s class-path then check, there

2. No default constructor on Spring Bean defined.

Defined Flow:

Spring manages Java class instantiation and if nothing is defined by a developer then a default no-argument constructor is added. But we usually have a constructor that takes at least one argument and when that is not the case we see this exception.

Sample Stack Trace:

nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘bJavaSample’ defined in class path resource [com/javapan/demo/ExampleConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.javapan.demo.BJavaSample]: Factory method ‘bJavaSample’ threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘aJavaSample’: No default constructor found;

nested exception is java.lang.NoSuchMethodException

Key Words to look for in your stack trace:

  • org.springframework.beans.factory.BeanCreationException:
  • “No default constructor found;
  • nested exception is java.lang.NoSuchMethodException:

Common Solution:

  • Initialize or add a default constructor and its arguments if not added into your configuration class as well as check your original class being initialized.

3. Forgot to define the bean in Spring Context.

Defined Flow:

When spring application starts and when that context is being loaded the BeanFactory is invoked which in turn calls the bean and tries to resolve that bean by its name.

Sample Stack Trace:

org.springframework.beans.factory.NoSuchBeanDefinitionException

Key Words to look for in your stack trace:

  • BeanCreationException
  • No Qualifying Bean of Type XXX Found for Dependency
  • NoSuchBeanDefinitionException
  • expected at least 1 bean which qualifies as autowire candidate for this dependency.

Common Solution:

Consider defining a bean of type XXX in your configuration.

Leave a Reply

Your email address will not be published. Required fields are marked *