ApplicationContextException Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the world of Spring Boot, an ApplicationContextException can often raise eyebrows, particularly when the error message reads, "Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean." Understanding this exception is crucial for developers working with Spring Boot applications that make use of embedded servers like Tomcat, Jetty, or Undertow.
Understanding the Exception
Spring Boot simplifies the process of bootstrapping and running a web application by allowing developers to embed a web server directly within the application. The ServletWebServerApplicationContext is responsible for launching and managing the lifecycle of an embedded web server. When an application fails to start due to the absence of a ServletWebServerFactory bean, it means Spring Boot could not find a suitable bean to configure the embedded server.
What is ServletWebServerFactory?
The ServletWebServerFactory is a core interface used by Spring Boot to abstract the implementation specifics of creating and configuring a web server. Implementations of this interface are responsible for creating an instance of a particular web server (Tomcat, Jetty, Undertow).
Why the Exception Occurs
This exception predominantly occurs in the following scenarios:
- Missing Dependencies: If the application does not include the required dependencies for any embedded server (e.g.,
spring-boot-starter-tomcat,spring-boot-starter-jetty), Spring Boot will not be able to locate aServletWebServerFactory. - Non-Web Environment: When a Spring Boot application is configured as a non-web application (e.g., command-line application), it does not include the necessary configuration for a web server.
- Configuration Issues: Custom or incorrect configurations might exclude the necessary configurations for the embedded server.
Steps to Resolve the Exception
1. Verify Dependencies
Ensure that the applicable server starter dependency is included in the pom.xml or build.gradle file.
For Maven, check that one of these dependencies is present:
For Gradle, include:
2. Check Application Type
Ensure your application is not inadvertently set up as a non-web environment. Make sure the spring.main.web-application-type is not set to NONE in your application.properties or application.yml.
Example
3. Custom Configuration Review
If custom server configuration is used, ensure that it correctly configures a ServletWebServerFactory bean.
Key Points Summary
| Key Area | Description |
ServletWebServerFactory | Interface to abstract embedded server creation. |
| Common Causes | Missing dependencies, non-web configuration, custom misconfigurations. |
| Dependency Verification | Ensure correct server starter dependencies exist. |
| Application Type | Verify if the application is set as a web context. |
| Custom Configurations | Confirm any custom configurations include a ServletWebServerFactory. |
Additional Considerations
- Spring Boot Version: Ensure you are using a compatible version of Spring Boot that supports your configuration.
- Profile-Specific Configuration: Check you aren't missing Servlet configurations specific to an
application-{profile}.propertiesorapplication-{profile}.yml.
Resolving the ApplicationContextException requires a careful review of your configuration and dependencies. By ensuring the presence of necessary web server factory beans and confirming configurations, you can swiftly deal with this common hurdle in Spring Boot applications.

