Starter for using Tomcat as the embedded servlet container, providing default servlet container functionality for Spring Boot web applications
—
Functional interfaces for customizing various aspects of the Tomcat server including connectors, contexts, and protocol handlers. These customizers enable fine-grained control over Tomcat configuration while integrating seamlessly with Spring Boot's auto-configuration system.
Functional interface for customizing Tomcat connectors, which handle incoming HTTP connections and protocol processing.
/**
* Functional interface for customizing Tomcat connectors
*/
@FunctionalInterface
public interface TomcatConnectorCustomizer {
/**
* Customize the given Tomcat connector
* @param connector the Tomcat connector to customize
*/
void customize(Connector connector);
}Usage Examples:
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ConnectorConfig {
@Bean
public TomcatConnectorCustomizer httpsConnectorCustomizer() {
return connector -> {
connector.setScheme("https");
connector.setSecure(true);
connector.setPort(8443);
connector.setAttribute("keyAlias", "tomcat");
connector.setAttribute("keystoreFile", "/path/to/keystore.p12");
connector.setAttribute("keystoreType", "PKCS12");
connector.setAttribute("keystorePass", "password");
};
}
@Bean
public TomcatConnectorCustomizer performanceCustomizer() {
return connector -> {
// Connection pool settings
connector.setAttribute("maxThreads", "300");
connector.setAttribute("minSpareThreads", "20");
connector.setAttribute("maxConnections", "10000");
connector.setAttribute("acceptCount", "200");
// Timeout settings
connector.setAttribute("connectionTimeout", "30000");
connector.setAttribute("keepAliveTimeout", "15000");
connector.setAttribute("maxKeepAliveRequests", "100");
// HTTP settings
connector.setAttribute("compression", "on");
connector.setAttribute("compressionMinSize", "2048");
connector.setAttribute("compressableMimeType",
"text/html,text/xml,text/plain,text/css,application/json,application/javascript");
};
}
}Functional interface for customizing Tomcat contexts, which represent individual web applications.
/**
* Functional interface for customizing Tomcat contexts
*/
@FunctionalInterface
public interface TomcatContextCustomizer {
/**
* Customize the given Tomcat context
* @param context the Tomcat context to customize
*/
void customize(Context context);
}Usage Examples:
@Configuration
public class ContextConfig {
@Bean
public TomcatContextCustomizer sessionCustomizer() {
return context -> {
// Session configuration
context.setSessionTimeout(30); // 30 minutes
context.setCookies(true);
context.setUseHttpOnly(true);
// Security settings
context.getManager().setSecureRandomClass("java.security.SecureRandom");
context.getManager().setSecureRandomAlgorithm("SHA1PRNG");
};
}
@Bean
public TomcatContextCustomizer staticResourceCustomizer() {
return context -> {
// Static resource caching
context.setCachingAllowed(true);
context.setCacheMaxSize(10240); // 10MB
context.setCacheTTL(86400000); // 24 hours
// MIME type mappings
context.addMimeMapping("woff", "application/font-woff");
context.addMimeMapping("woff2", "application/font-woff2");
};
}
@Bean
public TomcatContextCustomizer errorPageCustomizer() {
return context -> {
// Custom error pages
ErrorPage error404 = new ErrorPage(HttpStatus.NOT_FOUND, "/error/404.html");
ErrorPage error500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/error/500.html");
context.addErrorPage(error404);
context.addErrorPage(error500);
};
}
}Generic functional interface for customizing Tomcat protocol handlers, which manage the low-level protocol processing.
/**
* Generic functional interface for customizing Tomcat protocol handlers
* @param <T> the type of protocol handler to customize
*/
@FunctionalInterface
public interface TomcatProtocolHandlerCustomizer<T> {
/**
* Customize the given protocol handler
* @param protocolHandler the protocol handler to customize
*/
void customize(T protocolHandler);
}Usage Examples:
@Configuration
public class ProtocolConfig {
@Bean
public TomcatProtocolHandlerCustomizer<Http11NioProtocol> nioProtocolCustomizer() {
return protocolHandler -> {
// NIO connector settings
protocolHandler.setAcceptorThreadCount(2);
protocolHandler.setMaxConnections(10000);
protocolHandler.setConnectionTimeout(30000);
// Buffer settings
protocolHandler.setSocketBuffer(65536);
protocolHandler.setAppReadBufSize(8192);
protocolHandler.setAppWriteBufSize(8192);
// Keep-alive settings
protocolHandler.setKeepAliveTimeout(15000);
protocolHandler.setMaxKeepAliveRequests(100);
};
}
@Bean
public TomcatProtocolHandlerCustomizer<AbstractProtocol<?>> genericProtocolCustomizer() {
return protocolHandler -> {
// Generic protocol handler settings
protocolHandler.setTcpNoDelay(true);
protocolHandler.setSoLinger(-1);
protocolHandler.setSoTimeout(30000);
// Thread pool configuration
if (protocolHandler.getExecutor() instanceof ThreadPoolExecutor) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) protocolHandler.getExecutor();
executor.setKeepAliveTime(60, TimeUnit.SECONDS);
executor.allowCoreThreadTimeOut(true);
}
};
}
}Spring Boot provides several built-in customizers that are automatically applied:
Configures SSL/TLS settings on Tomcat connectors based on ServerProperties.Ssl configuration.
/**
* Customizes Tomcat connectors for SSL configuration
*/
public class SslConnectorCustomizer implements TomcatConnectorCustomizer {
/** Constructor with SSL configuration */
public SslConnectorCustomizer(Ssl ssl, SslStoreProvider sslStoreProvider);
/** Applies SSL configuration to the connector */
public void customize(Connector connector);
}Configures HTTP compression settings on Tomcat connectors based on ServerProperties.Compression configuration.
/**
* Customizes Tomcat connectors for HTTP compression
*/
public class CompressionConnectorCustomizer implements TomcatConnectorCustomizer {
/** Constructor with compression configuration */
public CompressionConnectorCustomizer(Compression compression);
/** Applies compression settings to the connector */
public void customize(Connector connector);
}Customizers are automatically discovered and applied by Spring Boot's auto-configuration:
All customizer beans in the application context are automatically applied:
@Bean
public TomcatConnectorCustomizer myCustomizer() {
return connector -> {
// Custom configuration
};
}Customizers can be added directly to web server factories:
@Bean
public TomcatServletWebServerFactory tomcatFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.addConnectorCustomizers(connector -> {
// Connector customization
});
factory.addContextCustomizers(context -> {
// Context customization
});
factory.addProtocolHandlerCustomizers((Http11NioProtocol protocol) -> {
// Protocol handler customization
});
return factory;
}Customizers are applied in the following order:
server.tomcat.* propertiesWithin each category, customizers are ordered using Spring's @Order annotation or by implementing Ordered interface.
@Configuration
public class ConditionalTomcatConfig {
@Bean
@ConditionalOnProperty("app.ssl.enabled")
public TomcatConnectorCustomizer sslCustomizer() {
return connector -> {
// SSL configuration
};
}
@Bean
@Profile("production")
public TomcatProtocolHandlerCustomizer<Http11NioProtocol> productionCustomizer() {
return protocol -> {
// Production-specific settings
};
}
}@Bean
public TomcatServletWebServerFactory multiConnectorFactory() {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
// Additional HTTP connector
Connector httpConnector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
httpConnector.setPort(8080);
httpConnector.setScheme("http");
// Additional HTTPS connector
Connector httpsConnector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
httpsConnector.setPort(8443);
httpsConnector.setScheme("https");
httpsConnector.setSecure(true);
factory.addAdditionalTomcatConnectors(httpConnector, httpsConnector);
return factory;
}Install with Tessl CLI
npx tessl i tessl/maven-org-springframework-boot--spring-boot-starter-tomcat