Starter for using Tomcat as the embedded servlet container, providing default servlet container functionality for Spring Boot web applications
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Spring Boot Starter Tomcat provides Tomcat embedded servlet container functionality for Spring Boot applications. It serves as the default servlet container starter used by spring-boot-starter-web, enabling developers to create production-ready web applications with embedded Tomcat servers through dependency injection and auto-configuration.
pom.xml or build.gradleMaven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>2.7.18</version>
</dependency>Gradle:
implementation 'org.springframework.boot:spring-boot-starter-tomcat:2.7.18'import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Configuration
public class TomcatConfiguration {
@Bean
public TomcatConnectorCustomizer connectorCustomizer() {
return connector -> {
connector.setPort(8443);
connector.setSecure(true);
};
}
}Configuration via properties:
# Server configuration
server.port=8080
server.tomcat.basedir=/tmp/tomcat
server.tomcat.threads.max=300
server.tomcat.threads.min-spare=20
server.tomcat.max-connections=10000
server.tomcat.connection-timeout=30s
# Access logging
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=combinedSpring Boot Starter Tomcat is built around several key components:
TomcatServletWebServerFactory and TomcatReactiveWebServerFactory create and configure Tomcat server instancesTomcatConnectorCustomizer, TomcatContextCustomizer) for fine-grained server customizationServerProperties.Tomcattomcat-embed-core, tomcat-embed-el, and tomcat-embed-websocketCore factory classes for creating and configuring Tomcat-based web servers. Provides both servlet and reactive web server support with extensive customization options.
public class TomcatServletWebServerFactory extends AbstractServletWebServerFactory
implements ConfigurableTomcatWebServerFactory {
public TomcatServletWebServerFactory();
public TomcatServletWebServerFactory(int port);
public WebServer getWebServer(ServletContextInitializer... initializers);
}
public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFactory
implements ConfigurableTomcatWebServerFactory {
public TomcatReactiveWebServerFactory();
public WebServer getWebServer(HttpHandler httpHandler);
}Functional interfaces for customizing various aspects of the Tomcat server including connectors, contexts, and protocol handlers.
@FunctionalInterface
public interface TomcatConnectorCustomizer {
void customize(Connector connector);
}
@FunctionalInterface
public interface TomcatContextCustomizer {
void customize(Context context);
}
@FunctionalInterface
public interface TomcatProtocolHandlerCustomizer<T> {
void customize(T protocolHandler);
}Comprehensive configuration options through Spring Boot's properties system, covering connection settings, threading, access logging, static resources, and SSL.
@ConfigurationProperties(prefix = "server.tomcat")
public static class Tomcat {
private File basedir;
private Duration backgroundProcessorDelay = Duration.ofSeconds(10);
private int maxConnections = 8192;
private int acceptCount = 100;
private Duration connectionTimeout;
private Threads threads = new Threads();
private Accesslog accesslog = new Accesslog();
// ... additional properties
}Automatic configuration classes that set up Tomcat components based on classpath detection and application properties.
@AutoConfiguration
@ConditionalOnClass(ServletRequest.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(ServerProperties.class)
public class ServletWebServerFactoryAutoConfiguration {
// Auto-configuration beans and customizers
}The starter automatically includes these embedded Tomcat dependencies:
jakarta.annotation:jakarta.annotation-api - Jakarta annotations supportorg.apache.tomcat.embed:tomcat-embed-core - Core Tomcat embedded functionalityorg.apache.tomcat.embed:tomcat-embed-el - Expression Language supportorg.apache.tomcat.embed:tomcat-embed-websocket - WebSocket protocol support