How SpringBoot Boots Tomcat: Deep Dive into Startup Process and Tomcat Internals
This article explains how SpringBoot launches an embedded Tomcat server by walking through the main method, the SpringApplication.run workflow, context creation and refresh, and the internal Tomcat components such as Connector, Engine, Host, Context, and Wrapper, illustrated with code snippets and diagrams.
Preface
We know SpringBoot provides a new development experience by packaging web programs into a jar that can be started directly, thanks to its embedded container. This article uses Tomcat as an example to explore how SpringBoot starts Tomcat and to study Tomcat source code.
Starting from the Main Method
SpringBoot applications begin with a main method that invokes SpringApplication.run(...). The run method performs a series of steps: configure properties, obtain listeners, publish start events, initialize arguments, prepare environment, print banner, create application context, load environment, refresh context, publish running events, and finally return the context.
@SpringBootApplication
public class TomcatdebugApplication {
public static void main(String[] args) {
SpringApplication.run(TomcatdebugApplication.class, args);
}
}The core of the startup is creating and refreshing the application context, which is handled by createApplicationContext() and refreshContext(context).
protected ConfigurableApplicationContext createApplicationContext() {
Class contextClass = this.applicationContextClass;
if (contextClass == null) {
switch (this.webApplicationType) {
case SERVLET: contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS); break;
case REACTIVE: contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS); break;
default: contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
}
}
return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}For a typical web application SpringBoot selects the servlet web context class, which ultimately extends AnnotationConfigServletWebServerApplicationContext.
Inside Tomcat
Tomcat’s factory creates a Tomcat instance, adds a Connector, configures the Engine, and prepares the context.
public WebServer getWebServer(ServletContextInitializer... initializers) {
Tomcat tomcat = new Tomcat();
File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");
tomcat.setBaseDir(baseDir.getAbsolutePath());
Connector connector = new Connector(this.protocol);
tomcat.getService().addConnector(connector);
customizeConnector(connector);
tomcat.setConnector(connector);
tomcat.getHost().setAutoDeploy(false);
configureEngine(tomcat.getEngine());
// add additional connectors and prepare context
return getTomcatWebServer(tomcat);
}The Engine is the top‑level container; its children are Host, Context, and Wrapper, forming the hierarchy Engine → Host → Context → Wrapper.
Tomcat’s Server contains Services; each Service holds a Connector and a single Engine. An Engine contains Hosts, each Host contains Contexts, and each Context contains Wrappers (servlets).
Summary
SpringBoot starts via SpringApplication.run, which configures properties, publishes events, prepares the environment, creates a servlet web application context, and refreshes it. The refresh step triggers Tomcat’s creation, where the Connector and the hierarchical Container (Engine, Host, Context, Wrapper) are initialized, forming the core of Tomcat’s embedded server.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Interview Crash Guide
Dedicated to sharing Java interview Q&A; follow and reply "java" to receive a free premium Java interview guide.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
