Spring Boot Startup Process: A Zero‑to‑One Source Code Walkthrough
This article dissects the Spring Boot startup sequence step by step, showing how a minimal @SpringBootApplication triggers listeners, type deduction, auto‑configuration loading, environment preparation, IOC container creation, bean refresh, and embedded Tomcat launch, with interview‑ready explanations.
1. A Minimal Spring Boot Application
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}The above few lines start Tomcat (or Jetty/Undertow), the Spring IoC container, auto‑configuration (datasource, MVC, Redis, …), and instantiate all beans.
Problem: What actually happens behind SpringApplication.run()?
2. Full Startup Flowchart (must‑know for interviews)
┌─────────────────────────────────────────────────┐
│ SpringApplication.run() │
└─────────────────────┬───────────────────────────┘
│
▼
┌─────────────────────────────────────────────────┐
│ 1. Start listeners: SpringApplicationRunListeners│
└─────────────────────┬───────────────────────────┘
│
▼
┌─────────────────────────────────────────────────┐
│ 2. Deduce application type: Servlet or Reactive │
└─────────────────────┬───────────────────────────┘
│
▼
┌─────────────────────────────────────────────────┐
│ 3. Load Spring factories from META-INF/spring.factories│
└─────────────────────┬───────────────────────────┘
│
▼
┌─────────────────────────────────────────────────┐
│ 4. Find and instantiate ApplicationContextInitializer│
└─────────────────────┬───────────────────────────┘
│
▼
┌─────────────────────────────────────────────────┐
│ 5. Create and prepare Environment (load config files)│
└─────────────────────┬───────────────────────────┘
│
▼
┌─────────────────────────────────────────────────┐
│ 6. Print Banner │
└─────────────────────┬───────────────────────────┘
│
▼
┌─────────────────────────────────────────────────┐
│ 7. Create IoC container (AnnotationConfig…) │
└─────────────────────┬───────────────────────────┘
│
▼
┌─────────────────────────────────────────────────┐
│ 8. Prepare container (set Environment, etc.) │
└─────────────────────┬───────────────────────────┘
│
▼
┌─────────────────────────────────────────────────┐
│ 9. Refresh container – core <code>refresh()</code>│
└─────────────────────┬───────────────────────────┘
│
▼
┌─────────────────────────────────────────────────┐
│10. Run CommandLineRunner / ApplicationRunner │
└─────────────────────────────────────────────────┘3. Layer‑by‑Layer Breakdown
Step 1 – Deduce Application Type
// Inside SpringApplication constructor
this.webApplicationType = WebApplicationType.deduceFromClasspath();Deduction logic:
Presence of org.springframework.web.reactive.DispatcherHandler → REACTIVE
Presence of javax.servlet.Servlet or
org.springframework.web.context.ConfigurableWebApplicationContext→ SERVLET
None of the above → NONE
Interview answer: Spring Boot automatically decides between a servlet container (Tomcat), a reactive server (Netty), or a plain application based on classpath contents.
Step 2 – Load Spring Factories
Configurations are read from META-INF/spring.factories:
# spring-boot-autoconfigure/META-INF/spring.factories (excerpt)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,\
org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration,\
...This is the source of Spring Boot’s auto‑configuration.
Step 3 – Create Environment
The environment aggregates configuration sources in the following priority (high to low): command‑line arguments, system environment variables, application.properties / application.yml files.
Step 4 – Create IoC Container
// Create container based on application type
if (webApplicationType == SERVLET) {
context = new AnnotationConfigServletWebServerApplicationContext();
} else if (webApplicationType == REACTIVE) {
context = new AnnotationConfigReactiveWebServerApplicationContext();
} else {
context = new AnnotationConfigApplicationContext();
}Step 5 – Refresh Container (the core)
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// 1. Prepare refresh
prepareRefresh();
// 2. Obtain BeanFactory
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// 3. Prepare BeanFactory (class loader, post‑processors, …)
prepareBeanFactory(beanFactory);
// 4. Post‑process BeanFactory (Web containers start Tomcat here)
postProcessBeanFactory(beanFactory);
// 5. Invoke BeanFactoryPostProcessors
invokeBeanFactoryPostProcessors(beanFactory);
// 6. Register BeanPostProcessors
registerBeanPostProcessors(beanFactory);
// 7. Initialize MessageSource
initMessageSource();
// 8. Initialize ApplicationEventMulticaster
initApplicationEventMulticaster();
// 9. Subclass-specific onRefresh (starts embedded Tomcat)
onRefresh();
//10. Register listeners
registerListeners();
//11. Instantiate all non‑lazy singleton beans
finishBeanFactoryInitialization(beanFactory);
//12. Finish refresh and publish ContextRefreshedEvent
finishRefresh();
}
}4. How Tomcat Starts
The web server is created inside onRefresh() of ServletWebServerApplicationContext:
@Override
protected void onRefresh() {
super.onRefresh();
try {
createWebServer(); // creates Tomcat
} catch (Throwable ex) {
throw new ApplicationContextException("Unable to start web server", ex);
}
}
private void createWebServer() {
// Obtain WebServerFactory (TomcatServletWebServerFactory)
WebServerFactory factory = getWebServerFactory();
// Create Tomcat WebServer
this.webServer = factory.getWebServer(getSelfInitializer());
}5. Interview Answer Template
Spring Boot startup can be divided into three major stages: Stage 1 – Preparation Deduce application type (Servlet/Reactive/None) Load configurations from META-INF/spring.factories Create and initialize the Environment (read config files) Stage 2 – Create Container Create the IoC container according to the application type Set the Environment and other properties into the container Stage 3 – Refresh Container The core refresh() method parses configuration classes, registers bean definitions, and instantiates singleton beans For web projects, the embedded Tomcat or Jetty is started Finally, a ContextRefreshedEvent is published
Bonus points: The author highlights that refresh() contains 12 core steps, such as invokeBeanFactoryPostProcessors (executes BeanFactory post‑processors) and finishBeanFactoryInitialization (instantiates all non‑lazy singleton beans). Tomcat startup occurs in onRefresh().
6. One‑Page Diagram of the Startup Process
SpringApplication.run()
├── Deduce application type (SERVLET/REACTIVE/NONE)
├── Load spring.factories → auto‑configuration
├── Create Environment → read config files
├── Create IoC container
│ └── AnnotationConfig…ApplicationContext
└── refresh(container)
├── prepareRefresh (prepare)
├── obtainFreshBeanFactory (get BeanFactory)
├── prepareBeanFactory (prepare BeanFactory)
├── invokeBeanFactoryPostProcessors
├── registerBeanPostProcessors
├── onRefresh (start Tomcat)
├── finishBeanFactoryInitialization (instantiate beans)
└── finishRefresh (complete)7. Preview of the Next Episode
Spring Boot Source Analysis (Part 2): What does @SpringBootApplication actually do? – the three meta‑annotations, auto‑configuration mechanism, component‑scan range, and interview talking points.
8. Interaction
What Spring Boot source‑code interview questions have you been asked? Examples include “Spring Boot startup process”, “auto‑configuration principle”, and “custom starter”. Share your experiences in the comments; the next episode will prioritize the topics you care about.
— 老 J, see you next time.
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.
Coder Trainee
Experienced in Java and Python, we share and learn together. For submissions or collaborations, DM us.
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.
