Spring Boot
ApplicationContextException
ServletWebServerFactory
Web Server
Java

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:

  1. 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 a ServletWebServerFactory.
  2. 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.
  3. 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:

xml
1<dependency>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-tomcat</artifactId>
4</dependency>
xml
1<dependency>
2    <groupId>org.springframework.boot</groupId>
3    <artifactId>spring-boot-starter-jetty</artifactId>
4</dependency>

For Gradle, include:

gradle
implementation 'org.springframework.boot:spring-boot-starter-tomcat'
gradle
implementation 'org.springframework.boot:spring-boot-starter-jetty'

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

properties
spring.main.web-application-type=servlet

3. Custom Configuration Review

If custom server configuration is used, ensure that it correctly configures a ServletWebServerFactory bean.

java
1import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
2import org.springframework.boot.web.server.ServletWebServerFactory;
3import org.springframework.context.annotation.Bean;
4import org.springframework.context.annotation.Configuration;
5
6@Configuration
7public class WebServerConfig {
8
9    @Bean
10    public ServletWebServerFactory servletWebServerFactory() {
11        return new TomcatServletWebServerFactory();
12    }
13}

Key Points Summary

Key AreaDescription
ServletWebServerFactoryInterface to abstract embedded server creation.
Common CausesMissing dependencies, non-web configuration, custom misconfigurations.
Dependency VerificationEnsure correct server starter dependencies exist.
Application TypeVerify if the application is set as a web context.
Custom ConfigurationsConfirm 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-&#123;profile&#125;.properties or application-&#123;profile&#125;.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.


Course illustration
Course illustration

All Rights Reserved.