Spring Boot
Tomcat
Embedded Servlet Container
Spring Boot 2
Migration

TomcatEmbeddedServletContainerFactory is missing in Spring Boot 2

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Spring Boot has been a game-changer for Java developers, offering a streamlined approach to application creation with its opinionated setup. Older versions of Spring Boot provided developers with various tools and classes to set up and customize embedded servlet containers such as Tomcat. One such class was the TomcatEmbeddedServletContainerFactory, popular for its simplicity and ease of use. However, with the release of Spring Boot 2, this class and others like it were deprecated. Let's explore what this change entails and how to handle embedded server configuration in the latest versions.

Transition from Spring Boot 1.x to 2.x

In Spring Boot 1.x, TomcatEmbeddedServletContainerFactory provided extensive support for customizing the embedded Tomcat server. This factory class was part of the traditional way of bootstrapping and customizing an embedded web server in a Spring Boot application.

Migration of Concepts

With Spring Boot 2.x, there was a major overhaul of server configuration capabilities. The primary class for this purpose, TomcatEmbeddedServletContainerFactory, was removed in favor of a more generic and streamlined hierarchy:

  • Spring Boot 1.x: TomcatEmbeddedServletContainerFactory
  • Spring Boot 2.x: TomcatServletWebServerFactory

Example of the Change

Here's an example that demonstrates the difference between configuring embedded Tomcat in Spring Boot 1.x and 2.x.

Spring Boot 1.x:

java
1import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
2import org.springframework.context.annotation.Bean;
3import org.springframework.context.annotation.Configuration;
4
5@Configuration
6public class MyTomcatConfig {
7
8    @Bean
9    public TomcatEmbeddedServletContainerFactory tomcatFactory() {
10        TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
11        factory.setPort(8080);
12        return factory;
13    }
14}

Spring Boot 2.x:

java
1import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
2import org.springframework.context.annotation.Bean;
3import org.springframework.context.annotation.Configuration;
4
5@Configuration
6public class MyTomcatConfig {
7
8    @Bean
9    public TomcatServletWebServerFactory tomcatFactory() {
10        TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
11        factory.setPort(8080);
12        return factory;
13    }
14}

Key Changes and Improvements

The new class and package structure in Spring Boot 2.x brought several improvements, notably:

  1. Unified Server Configuration: The creation and customization of servlet web servers are now more straightforward and unified across different types of containers using the ServletWebServerFactory interface. This promotes better abstraction and consistency.
  2. Enhanced Flexibility: Developers have greater control and flexibility by defining beans of type TomcatServletWebServerFactory, which can be injected and customized as needed using factory methods.
  3. Centralized Properties: Server settings are increasingly controlled via properties in application.properties or application.yml, reducing the need for extensive Java configuration.

Configuration Using Properties

A significant shift in Spring Boot 2.x is the reliance on configuration files for server settings. This removes much of the boilerplate Java code and simplifies server customization.

Example application.properties:

properties
1server.port=8080
2server.tomcat.max-threads=200
3server.tomcat.uri-encoding=UTF-8
4server.tomcat.accesslog.enabled=true

Summary of Key Changes

To quickly recap the evolution from Spring Boot 1.x to 2.x, refer to the table below:

Feature/PackageSpring Boot 1.xSpring Boot 2.x
Tomcat Factory ClassTomcatEmbeddedServletContainerFactoryTomcatServletWebServerFactory
Packageorg.springframework.boot.context.embedded.tomcatorg.springframework.boot.web.embedded.tomcat
HTTP/2 SupportLimitedEnhanced
Server Configuration MethodPredominantly Java CodeCombination of Java & Configuration files
Configuration FlexibilityCode-based extensionsProperty & Bean-based customization

Best Practices for Transition

If you're planning to upgrade your Spring Boot application or start a new project, keep the following practices in mind:

  • Leverage Configuration Files: Maximize the use of application.properties or application.yml for overriding default settings.
  • Simplify: Utilize the updated Spring Boot 2.x capabilities for server configuration to reduce the complexity of configuration classes.
  • Stay Informed: Keep abreast of the latest Spring Boot releases and their respective documentation to anticipate and make the most out of new features.

Conclusion

The removal of TomcatEmbeddedServletContainerFactory from Spring Boot 2.x marks a move towards a more robust, scalable, and developer-friendly approach to embedding servers. By embracing these changes, developers can enjoy an architecture that is both cleaner and more powerful, with a reduced emphasis on boilerplate code, making way for greater focus on application logic.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.