Spring Boot is a popular framework for building web applications, but sometimes developers may encounter errors when starting the application. One such error is “org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean”. In this post, we will go over the cause of this error and how to resolve it.



Root Cause

The error message “org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean” occurs when the ServletWebServerFactory bean is missing from the application context. The ServletWebServerFactory is responsible for creating the embedded servlet container, which is used to host the application. Without this bean, Spring Boot will not be able to start the web server and run the application.



Solution

To resolve this error, you need to add the missing ServletWebServerFactory bean to your application. You can do this by adding the following code to your main class:

@Bean
public ServletWebServerFactory servletContainer() {
    TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
    tomcat.addAdditionalTomcatConnectors(createStandardConnector());
    return tomcat;
}

private Connector createStandardConnector() {
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setPort(8080);
    return connector;
}

In the example above, we are using Tomcat as the embedded servlet container. You can also use other containers such as Jetty or Undertow by simply changing the ServletWebServerFactory bean.



What is ServletWebServerFactory

The ServletWebServerFactory is a factory interface used to create the embedded servlet container in a Spring Boot application. This interface has several implementations, including TomcatServletWebServerFactory, JettyServletWebServerFactory, and UndertowServletWebServerFactory, which can be used to create the servlet container based on the specific needs of the application.



How it Works

When a Spring Boot application starts, it scans the application context for a ServletWebServerFactory bean. If one is found, it uses it to create the embedded servlet container. If a ServletWebServerFactory bean is not found, Spring Boot will use the default implementation, which is TomcatServletWebServerFactory.

The servlet container created by the ServletWebServerFactory is responsible for hosting the application and serving incoming HTTP requests. The container also manages the lifecycle of the application, including starting, stopping, and restarting the application.



Customizing the Servlet Container

In addition to resolving the missing ServletWebServerFactory bean error, you may also want to customize the behavior of the servlet container. For example, you may want to change the port that the container listens on or configure additional connectors. You can do this by modifying the createStandardConnector() method.

private Connector createStandardConnector() {
    Connector connector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
    connector.setPort(9090);
    return connector;
}

In the example above, we are changing the port that the container listens on from 8080 to 9090.



Conclusion

The “org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean” error in Spring Boot can be easily resolved by adding the missing ServletWebServerFactory bean to the application context. With this bean in place, Spring Boot will be able to start the web server and run the application. Additionally, you can customize the behavior of the servlet container to meet the specific needs of your application.



Leave a Reply