Spring Boot Error ApplicationContextException

The reasons for Spring Boot Error ApplicationContextException are missing Spring Auto-Configuration, missing @SpringBootApplication annotation in the main class of the Spring Stater project, and missing the ServletWebServerFactory bean. When we face this error, we can not run the Spring Boot project as expected. This error is mostly not raised because of the Spring Auto Configuration feature, which means this feature can handle the basic required dependency for creating a Spring Boot project.

In this article, we will discuss how to solve Spring Boot Error ApplicationContextException with relative examples.

Reasons for Spring Boot Error ApplicationContextException

  • Missing ServletWebServerFactory bean
  • Problem in Spring Auto Configuration
  • Missing the @SpringBootApplication annotation in the Main class
  • Uncompleted Project creation

Troubleshooting Ways for Error ApplicationContextException

  • Check the Main class Annotation
  • Create the Spring Boot project properly
  • Use latest version of Spring version for Spring Auto Configuration
  • Check For ServletWebServerFactory bean is available in the project or Not

Gettting Spring Boot Error ApplicationContextException

Mostly we face this error due to missing ServletWebServerFactory bean. Below we have provided an example for Spring Boot Error ApplicationContextException for understanding this concept in better way.

Here, we removed @SpringBootApplication annotation from the Main class, then we got this Spring Boot Error ApplicationContextException.

Java
package com.app;

import org.springframework.boot.SpringApplication;

public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

Output:

Below we can refer the image to know how ApplicationContextException occurs.


Steps to Troubleshoot Spring Boot Error ApplicationContextException

Here, we created one simple Spring Boot project by using Spring Stater Initializr with basic Spring Stater Dependency.

Step 1:

Use spring initializer to create the spring boot project, when the project is created, add the below dependencies to the project.

  • Spring Web
  • Spring Dev Tools
  • Spring Test

Once project is completed the project folder look like below image.


Step 2:

  • Now check the Spring Project Main class is Annotated with @SpringBootApplication annotation or not.
  • Once checking completed, then check the Spring Auto configuration.
  • After that check the missing ServletWebServerFactory bean is visible in Spring Project logs.
  • Once all checking are completed, now follow the next Step.

Step 3:

  • Now run this project as Spring Boot App.
  • Then we can observe the Logs of the project and this project is running with Apache tomcat server on port number 8080 by default.

Step 4:

After successfully running the project, we will get the below output image.