main
method
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
- Entry Point:
Execution begins atpublic static void main(String[] args)
โ the standard Java application entry point SpringApplication.run(...)
- Initializes the Spring ApplicationContext (the core container).
- Starts the embedded servlet container (e.g., Tomcat) if itโs a web app.
- Loads configuration files and applies auto-configuration.
- Performs component scanning to discover beans and dependencies
- More of this down below
Event class | ๋ฐ์ ์์ |
---|---|
ApplicationStartingEvent | ์ ํ๋ฆฌ์ผ์ด์ ์์ ์งํ |
ApplicationEnvironmentPreparedEvent | ํ๊ฒฝ ์ค๋น ์งํ |
ApplicationContextInitializedEvent | ์ปจํ ์คํธ ์ด๊ธฐํ ์งํ |
ApplicationPreparedEvent | Bean ๋ฑ๋ก ์ |
ApplicationReadyEvent | ๋ชจ๋ ์ด๊ธฐํ ์๋ฃ ํ |
- Embedded Server Startup (for web apps)
- Starts embedded servlet container (Tomcat, Jetty, Undertow) via
WebServerFactory
. - Registers
DispatcherServlet
to handle web requests.
- Starts embedded servlet container (Tomcat, Jetty, Undertow) via
@SpringBootApplication
- Main notes: @SpringBootApplication
Visualization
flowchart TD A["public static void main"] --> B["SpringApplication.run(...)"] B --> C["@SpringBootApplication"] C --> D_sub D_sub --> E["ApplicationContext ์ด๊ธฐํ (All beans are registered, initialized, and DI is performed)"] E --> F["๋ด์ฅ Tomcat ๋ฑ ์๋ฒ ๊ตฌ๋"] F --> G["ํด๋ผ์ด์ธํธ ์์ฒญ ๋๊ธฐ ์ํ ์ง์ "] subgraph D_sub [๊ตฌ์ฑ ์ด๋ ธํ ์ด์ ] DA["@EnableAutoConfiguration"] DB["@ComponentScan"] DC["@SpringBootConfiguration"] end
The overall, higher-level flow of the entire application startup
main()
method (Application Entry)- Starts the entire process.
SpringApplication.run()
(The Grand Orchestrator)- Does NOT happen in the EXACT order linearly, in reality they are interleaved
- @SpringBootApplication (meta annotation)
- Creation and Initialization of
SpringApplication
Object-
`SpringApplication.run(MyApp.class, args);`
-
- Creation and Configuration of ApplicationContext (the IoC Container)
- Chooses ApplicationContext implementation based on
WebApplicationType
:- Servlet apps โ
AnnotationConfigServletWebServerApplicationContext
- WebFlux apps โ
AnnotationConfigReactiveWebServerApplicationContext
- Non-web apps โ
AnnotationConfigApplicationContext
- Servlet apps โ
- Runs registered
ApplicationContextInitializers
for customization. - Configures Environment,
ApplicationContext
, andResourceLoader
.
- Chooses ApplicationContext implementation based on
- Auto-Configuration Application (
@EnableAutoConfiguration
)- Applies configurations conditionally (e.g.,
@ConditionalOnClass
,@ConditionalOnMissingBean
) to register as bean. - Automatically loads and applies configuration candidates based on the classpath.
- These are not found by your applicationโs
@ComponentScan
(they are in separate libraries) - @SpringBootApplication - @EnableAutoConfiguration
- Applies configurations conditionally (e.g.,
- Component Scanning (
@ComponentScan
)- Automatically discovers components (like
@Component
,@Service
, etc.) within specified packages. - @SpringBootApplication - @ComponentScan
- Automatically discovers components (like
- Processing of
@SpringBootConfiguration
- Registers Java-based configuration classes, used for explicit Bean definitions.
- your own explicitly defined
@Bean
methods - @SpringBootApplication - @SpringBootConfiguration
- ApplicationContext Refresh (Initialization)
- ALL BEANS are registered, initialized, and DI is performed
- Executes
CommandLineRunner
andApplicationRunner
beans. - Publishes lifecycle events received by registered
ApplicationListener
s.
- Creation and Initialization of
- Embedded Server Startup
- Transitions the application to a state where it can receive HTTP requests, typically on the default port (8080).
- The application becomes ready to serve requests.
Summary of SpringApplication.run()
์์ ๊ฑฐ ์์ ๋๊ณ ๋น๊ตํด๋ณด๋ฉด ์ข์
- Preparation:
SpringApplication
object created,Environment
set up, basicApplicationContext
type determined. (Your 2.1 and part of 2.2) - Bean Definition Gathering: This is where the
ApplicationContext
starts collecting definitions of all the beans it needs to create. This involves:- Processing
@SpringBootConfiguration
(your 2.5) - Performing
@ComponentScan
(your 2.4) - Applying
@EnableAutoConfiguration
(your 2.3) โ all contributing bean definitions.
- Processing
- Context Refresh (The Core Construction Phase): Once all bean definitions are gathered, the
ApplicationContext
โrefreshes.โ This is a major phase where:- The
BeanFactory
actually creates the bean instances. - Dependency Injection is performed.
- Lifecycle callbacks are executed.
- Post-processors are applied.
CommandLineRunner
/ApplicationRunner
s are run.- Events are published. (This covers your 2.6)
- The
- Finalization: Embedded server startup. (Your main point 3)
Events
Spring Boot triggers various lifecycle events during its execution. These events allow developers to insert specific actions at crucial moments, such as right after the application starts or just before it shuts down.
Event | Description |
---|---|
ApplicationStartingEvent | Fired immediately after the SpringApplication.run() method is called. |
ApplicationEnvironmentPreparedEvent | Occurs after the Environment (e.g., properties, profiles) has been prepared. |
ApplicationContextInitializedEvent | Fired right after the [[IoC (Inversion of Control)#ApplicationContext |ApplicationContext]] has been initialized but before any beans are loaded. |
ApplicationPreparedEvent | Triggered immediately after all preparations are complete, but before the context is refreshed. |
ApplicationStartedEvent | Fired right after the ApplicationContext has been refreshed and all beans are loaded. |
ApplicationReadyEvent | Occurs when the application is fully ready to serve requests, after all CommandLineRunner and ApplicationRunner beans have been called. |
ApplicationFailedEvent | Fired if the application fails to start due to an exception. |
์์: ์๋ฒ๊ฐ ์ผ์ง ๋ค ์๋ฆผ ๋ณด๋ด๊ธฐ |
@Component
public class ServerReadyNotifier implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
// ์: ์ฌ๋ ๋ฉ์์ง ์ ์ก
slackService.send("์๋ฒ๊ฐ ์ค๋น๋์์ต๋๋ค.");
}
}
Summary
main()
๋ฉ์๋์SpringApplication.run()
ํธ์ถ์ ๋จ์ํ ์ง์ ์ ์ด ์๋๋ผ Spring Boot ์ ์ฒด ์คํ์ ๊ตฌ์ฑํ๋ ์ค์ ๊ด์ ํ ์ญํ ์ ํ๋ค.- ์ด ํธ์ถ ์ดํ ๋ด๋ถ์ ์ผ๋ก๋
SpringApplication
์ธ์คํด์ค๊ฐ ์์ฑ๋๊ณ ,run()
๋ฉ์๋๋ฅผ ํตํด ์๋์ ๊ฐ์ ์ฃผ์ ๋จ๊ณ๊ฐ ์์ฐจ์ ์ผ๋ก ์ํ๋๋ค:- ApplicationContext ์ค๋น (
prepareContext
) - ์๋ ์ค์ ๋ก๋ฉ ๋ฐ Bean ๋ฑ๋ก
- BeanFactoryPostProcessor, BeanPostProcessor ์๋
- ApplicationContext ์ด๊ธฐํ (
refresh
) - Runner ์คํ (
callRunners
)
- ApplicationContext ์ค๋น (
- ์ด ํ๋ฆ์ ๋ชจ๋ ๋๋ฒ๊น
์ ํตํด ์ง์ ํ์ธ ๊ฐ๋ฅํ๋ฉฐ,
refreshContext()
๋ด๋ถ์์ ์ต์ข ์ ์ผ๋กcontext.refresh()
๋ฅผ ํธ์ถํ์ฌ Spring ์ปจํ ์ด๋๊ฐ ์์ ํ ์คํ ์ํ๋ก ์ ํ๋๋ค. - ์ค์ต์์๋ ๋ธ๋ ์ดํฌํฌ์ธํธ๋ฅผ ํ์ฉํด Spring์ ์คํ ํ๋ฆ์ ์ถ์ ํ๊ณ , ๊ฐ ๋ฉ์๋ ํธ์ถ ์์ ์ ๊ฐ์ฒด ์ํ๋ฅผ ํ์ธํจ์ผ๋ก์จ ํ๋ ์์ํฌ ๋ด๋ถ ๋์์ ๋ํ ๊น์ ์ดํด๋ฅผ ํ์ฑํ ์ ์๋ค.
- ์ด๋ฌํ ์ค์ต ๊ธฐ๋ฐ ํ์ต์ ์ดํ ๋ฐ์ํ ์ ์๋ ์คํ ์ค๋ฅ๋ ์ค์ ์ถฉ๋ ์ด์์ ๋ํ ์ง๋จ๊ณผ ๋ฌธ์ ํด๊ฒฐ ์ญ๋์ ๊ฐํํ๋ ๋ฐ ๊ธฐ์ด๊ฐ ๋๋ค.