The 30 second answer
Tomcat is selected through normal auto-configuration, not a separate discovery trick. In a servlet application, the Tomcat auto-configuration checks for the servlet and Tomcat classes with @ConditionalOnClass and verifies the application type with @ConditionalOnWebApplication. If I haven't supplied a ServletWebServerFactory, it creates a TomcatServletWebServerFactory. During refresh, the web application context finds that factory and uses it to create the server, which starts as the context finishes refreshing.
1. Application type. WebApplicationType.deduce() checks for servlet classes:
private static final String[] SERVLET_INDICATOR_CLASSES = { "jakarta.servlet.Servlet",
"org.springframework.web.context.ConfigurableWebApplicationContext" };WebApplicationType.java L43-L67. In Spring Boot 4, modules can also contribute a Deducer through spring.factories, for example for WebFlux. A SERVLET app gets a ServletWebServerApplicationContext.
2. Auto-configuration matches. The Tomcat module lists TomcatServletWebServerAutoConfiguration in its AutoConfiguration.imports file (imports file). The class is conditional:
@AutoConfiguration
@ConditionalOnClass({ ServletRequest.class, Tomcat.class, UpgradeProtocol.class, TomcatServletWebServerFactory.class })
@ConditionalOnWebApplication(type = Type.SERVLET)
public final class TomcatServletWebServerAutoConfiguration {
@Bean
@ConditionalOnMissingBean(value = ServletWebServerFactory.class, search = SearchStrategy.CURRENT)
TomcatServletWebServerFactory tomcatServletWebServerFactory(...) {
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.getConnectorCustomizers().addAll(connectorCustomizers.orderedStream().toList());
...
return factory;
}
}TomcatServletWebServerAutoConfiguration.java L41-L65
3. The server is created during refresh.
protected void onRefresh() {
super.onRefresh();
try {
createWebServer();
}
catch (Throwable ex) {
throw new ApplicationContextException("Unable to start web server", ex);
}
}ServletWebServerApplicationContext.java L149-L157. getWebServerFactory() requires exactly one ServletWebServerFactory bean; zero or more than one fails startup (L201-L213).
4. Servlets and filters. The factory receives a ServletContextInitializer. It registers the DispatcherServlet and every Servlet, Filter and listener bean in the embedded Tomcat.
Switching servers: exclude spring-boot-starter-tomcat and add spring-boot-starter-jetty. The Tomcat class condition now fails and the Jetty one matches. Both on the classpath with no exclusion is a common mistake.
Customizing: use server.* and server.tomcat.* properties, or a WebServerFactoryCustomizer<TomcatServletWebServerFactory> bean.
Spring Boot 3.x note: the same class was called ServletWebServerFactoryAutoConfiguration with nested Tomcat/Jetty/Undertow configurations; Spring Boot 4 split each server into its own module.
Sources (2)
- Spring Boot reference: Embedded servlet container supportdocs.spring.io
- Spring Boot how-to: Use another web serverdocs.spring.io