When you start a Spring Boot application, a series of steps happen behind the scenes to bootstrap and initialize the application. Here's a breakdown of the logical execution order with a step-by-step explanation.
1. Steps in Spring Boot Application Startup
1.1 Bootstrap the JVM and Load Main Class
- The Java Virtual Machine (JVM) starts and loads the main class containing the
public static void main(String[] args)
method.
1.2 Run the SpringApplication.run()
method
- The
SpringApplication.run()
method is executed, which starts the Spring Boot application. - This is the entry point for all Spring Boot applications.
1.3 Initialize SpringApplication
-
SpringApplication
is initialized by performing several setup steps, including:- Determining whether the application is a web or non-web application.
- Loading application configuration and environment properties.
- Preparing the application context.
1.4 Prepare the ApplicationContext
- The Spring Application Context is created, which acts as a container to manage beans, configurations, and dependencies.
- If it's a web application, a Servlet-based context (like
AnnotationConfigServletWebServerApplicationContext
) is used.
1.5 Load ApplicationContextInitializers
- Any registered ApplicationContextInitializer beans (which modify the context before it starts) are loaded.
1.6 Detect and Apply SpringApplicationRunListeners
- Spring scans and loads any
SpringApplicationRunListener
implementations that allow external listeners to be notified about application startup events.
1.7 Prepare the Environment
(Properties, Profiles, and Configurations)
- The environment variables,
application.properties
orapplication.yml
, and command-line arguments are loaded. - Active and default profiles are determined.
1.8 Refresh the Application Context
- The Application Context is fully refreshed, which includes:
- Scanning for @Component, @Service, @Repository, and @Controller annotated classes.
- Instantiating beans and managing their lifecycle.
- Applying dependency injection and resolving dependencies.
1.9 Start Embedded Web Server (if applicable)
- If the application is a web application, Spring Boot automatically starts an embedded web server (Tomcat, Jetty, or Undertow).
- The web server listens for incoming requests on a specified port.
1.10 Execute CommandLineRunner
and ApplicationRunner
Beans
- If any beans implementing
CommandLineRunner
orApplicationRunner
are present, they are executed. - These are useful for running initialization logic after the application has started.
1.11 Trigger Application Ready Events
- The
ApplicationReadyEvent
is triggered, signaling that the application is fully started and ready to handle requests.
1.12 Keep Application Running (for Web Apps)
- For web applications, the application keeps running, waiting for incoming HTTP requests.
- For non-web applications, it will exit after running initialization logic.
2. Logical Execution Order: Simple Overview
To remember the startup flow, think of it as three major phases:
1️⃣ Bootstrapping Phase
- JVM starts the application.
-
SpringApplication.run()
is executed. - Environment and configuration are prepared.
2️⃣ Context Initialization Phase
- Application Context is created and refreshed.
- Beans are scanned, created, and injected.
- If a web app, the embedded server starts.
3️⃣ Post-Startup Phase
-
CommandLineRunner
andApplicationRunner
execute. - Application is now fully running and waiting for requests. 🚀
Top comments (0)