0
# Spring Boot Starter Tomcat
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: spring-boot-starter-tomcat
7
- **Package Type**: Maven
8
- **Language**: Java
9
- **Group ID**: org.springframework.boot
10
- **Artifact ID**: spring-boot-starter-tomcat
11
- **Installation**: Add dependency to `pom.xml` or `build.gradle`
12
13
Maven:
14
```xml
15
<dependency>
16
<groupId>org.springframework.boot</groupId>
17
<artifactId>spring-boot-starter-tomcat</artifactId>
18
<version>2.7.18</version>
19
</dependency>
20
```
21
22
Gradle:
23
```groovy
24
implementation 'org.springframework.boot:spring-boot-starter-tomcat:2.7.18'
25
```
26
27
## Core Imports
28
29
```java
30
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
31
import org.springframework.boot.web.embedded.tomcat.TomcatReactiveWebServerFactory;
32
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
33
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
34
import org.springframework.boot.web.embedded.tomcat.TomcatProtocolHandlerCustomizer;
35
```
36
37
## Basic Usage
38
39
```java
40
import org.springframework.boot.SpringApplication;
41
import org.springframework.boot.autoconfigure.SpringBootApplication;
42
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
43
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
44
import org.springframework.context.annotation.Bean;
45
import org.springframework.context.annotation.Configuration;
46
47
@SpringBootApplication
48
public class Application {
49
public static void main(String[] args) {
50
SpringApplication.run(Application.class, args);
51
}
52
}
53
54
@Configuration
55
public class TomcatConfiguration {
56
57
@Bean
58
public TomcatConnectorCustomizer connectorCustomizer() {
59
return connector -> {
60
connector.setPort(8443);
61
connector.setSecure(true);
62
};
63
}
64
}
65
```
66
67
Configuration via properties:
68
```properties
69
# Server configuration
70
server.port=8080
71
server.tomcat.basedir=/tmp/tomcat
72
server.tomcat.threads.max=300
73
server.tomcat.threads.min-spare=20
74
server.tomcat.max-connections=10000
75
server.tomcat.connection-timeout=30s
76
77
# Access logging
78
server.tomcat.accesslog.enabled=true
79
server.tomcat.accesslog.pattern=combined
80
```
81
82
## Architecture
83
84
Spring Boot Starter Tomcat is built around several key components:
85
86
- **Web Server Factories**: `TomcatServletWebServerFactory` and `TomcatReactiveWebServerFactory` create and configure Tomcat server instances
87
- **Auto-Configuration**: Automatic setup of Tomcat components based on classpath detection and properties
88
- **Customizers**: Functional interfaces (`TomcatConnectorCustomizer`, `TomcatContextCustomizer`) for fine-grained server customization
89
- **Configuration Properties**: Extensive property-based configuration through `ServerProperties.Tomcat`
90
- **Embedded Dependencies**: Includes `tomcat-embed-core`, `tomcat-embed-el`, and `tomcat-embed-websocket`
91
92
## Capabilities
93
94
### Web Server Factories
95
96
Core factory classes for creating and configuring Tomcat-based web servers. Provides both servlet and reactive web server support with extensive customization options.
97
98
```java { .api }
99
public class TomcatServletWebServerFactory extends AbstractServletWebServerFactory
100
implements ConfigurableTomcatWebServerFactory {
101
102
public TomcatServletWebServerFactory();
103
public TomcatServletWebServerFactory(int port);
104
public WebServer getWebServer(ServletContextInitializer... initializers);
105
}
106
107
public class TomcatReactiveWebServerFactory extends AbstractReactiveWebServerFactory
108
implements ConfigurableTomcatWebServerFactory {
109
110
public TomcatReactiveWebServerFactory();
111
public WebServer getWebServer(HttpHandler httpHandler);
112
}
113
```
114
115
[Web Server Factories](./web-server-factories.md)
116
117
### Server Customization
118
119
Functional interfaces for customizing various aspects of the Tomcat server including connectors, contexts, and protocol handlers.
120
121
```java { .api }
122
@FunctionalInterface
123
public interface TomcatConnectorCustomizer {
124
void customize(Connector connector);
125
}
126
127
@FunctionalInterface
128
public interface TomcatContextCustomizer {
129
void customize(Context context);
130
}
131
132
@FunctionalInterface
133
public interface TomcatProtocolHandlerCustomizer<T> {
134
void customize(T protocolHandler);
135
}
136
```
137
138
[Server Customization](./server-customization.md)
139
140
### Configuration Properties
141
142
Comprehensive configuration options through Spring Boot's properties system, covering connection settings, threading, access logging, static resources, and SSL.
143
144
```java { .api }
145
@ConfigurationProperties(prefix = "server.tomcat")
146
public static class Tomcat {
147
private File basedir;
148
private Duration backgroundProcessorDelay = Duration.ofSeconds(10);
149
private int maxConnections = 8192;
150
private int acceptCount = 100;
151
private Duration connectionTimeout;
152
private Threads threads = new Threads();
153
private Accesslog accesslog = new Accesslog();
154
// ... additional properties
155
}
156
```
157
158
[Configuration Properties](./configuration-properties.md)
159
160
### Auto-Configuration
161
162
Automatic configuration classes that set up Tomcat components based on classpath detection and application properties.
163
164
```java { .api }
165
@AutoConfiguration
166
@ConditionalOnClass(ServletRequest.class)
167
@ConditionalOnWebApplication(type = Type.SERVLET)
168
@EnableConfigurationProperties(ServerProperties.class)
169
public class ServletWebServerFactoryAutoConfiguration {
170
// Auto-configuration beans and customizers
171
}
172
```
173
174
[Auto-Configuration](./auto-configuration.md)
175
176
## Dependencies Provided
177
178
The starter automatically includes these embedded Tomcat dependencies:
179
- `jakarta.annotation:jakarta.annotation-api` - Jakarta annotations support
180
- `org.apache.tomcat.embed:tomcat-embed-core` - Core Tomcat embedded functionality
181
- `org.apache.tomcat.embed:tomcat-embed-el` - Expression Language support
182
- `org.apache.tomcat.embed:tomcat-embed-websocket` - WebSocket protocol support