Clean • Professional
One of Spring Boot’s biggest innovations is the embedded web server (Tomcat, Jetty, or Undertow). Instead of deploying WAR files to an external server, Spring Boot starts the web server inside the JVM during application startup.
Understanding this lifecycle is essential for:
An embedded server is a web server packaged inside the Spring Boot application, removing the need for external deployment (WAR, standalone Tomcat).
Common embedded servers:
Embedded server startup happens after the ApplicationContext is refreshed, but before the application is fully ready.
High-level flow:
SpringApplication.run()
├──prepareEnvironment()
├──createApplicationContext()
├──refreshContext()
│ ├── BeanDefinition loading
│ ├── Bean instantiation
│ ├── Embedded server creation & start ← HERE
└── ApplicationReadyEvent
| Component | Responsibility |
|---|---|
ServletWebServerApplicationContext | Web-aware ApplicationContext |
ServletWebServerFactory | Creates web server |
TomcatServletWebServerFactory | Tomcat-specific factory |
WebServer | Unified server abstraction |
DispatcherServlet | Central request dispatcher |
During SpringApplication.run():
WebApplicationType.deduceFromClasspath()
Possible values:
SERVLET → Tomcat / JettyREACTIVE → NettyNONE → No web serverIf SERVLET is detected:
➡️ Spring creates ServletWebServerApplicationContext.
Spring Boot auto-configures a ServletWebServerFactory bean:
Examples:
TomcatServletWebServerFactory
JettyServletWebServerFactory
UndertowServletWebServerFactory
Auto-configured by:
ServletWebServerFactoryAutoConfiguration
Conditional checks:
server.port, server.ssl.*)During:
AbstractApplicationContext.refresh()
Spring performs:
Embedded server is not started yet.
Inside:
ServletWebServerApplicationContext.onRefresh()
Spring calls:
createWebServer()
What happens:
ServletWebServerFactorygetWebServer(...)➡️ Server is created but not yet serving requests
Before server starts:
DispatcherServlet bean is createdFilterRegistrationBean)Default mapping:
DispatcherServlet →"/"
Spring calls:
webServer.start()For Tomcat:
server.port)At this moment:
After successful startup:
WebServerInitializedEvent
Contains:
Useful for:
Final lifecycle event:
ApplicationReadyEvent
Meaning:
SpringApplication.run()
↓
Detect WebApplicationType
↓
Create ServletWebServerApplicationContext
↓
Auto-configure ServletWebServerFactory
↓
refreshContext()
↓
onRefresh()
↓
createWebServer()
↓
Register DispatcherServlet & filters
↓
webServer.start()
↓
WebServerInitializedEvent
↓
ApplicationReadyEvent
TomcatServletWebServerFactory
└──createTomcat()
├── Create Tomcat instance
├── Configure connectors
├── Setup thread pool
├── Register servlets
└──start()
Change Port
server.port=9090
Customize Tomcat Programmatically
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory>customizer() {
return factory -> factory.setPort(9090);
}
| Issue | Cause |
|---|---|
| Port already in use | Another process running |
| Server not starting | WebApplicationType = NONE |
| DispatcherServlet not registered | Context load failure |
| Filters not applied | Order or registration issue |
Enable debug:
debug=true
onRefresh()ApplicationReadyEventThe Embedded Server Startup Lifecycle is a core pillar of Spring Boot architecture.
It: