main method

@SpringBootApplication
public class DemoApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
  • Entry Point:
    Execution begins at public 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์ปจํ…์ŠคํŠธ ์ดˆ๊ธฐํ™” ์งํ›„
ApplicationPreparedEventBean ๋“ฑ๋ก ์ „
ApplicationReadyEvent๋ชจ๋“  ์ดˆ๊ธฐํ™” ์™„๋ฃŒ ํ›„
  1. Embedded Server Startup (for web apps)
    • Starts embedded servlet container (Tomcat, Jetty, Undertow) via WebServerFactory.
    • Registers DispatcherServlet to handle web requests.

@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

  1. main() method (Application Entry)
    • Starts the entire process.
  2. SpringApplication.run() (The Grand Orchestrator)
    • Does NOT happen in the EXACT order linearly, in reality they are interleaved
    • @SpringBootApplication (meta annotation)
      1. Creation and Initialization of SpringApplication Object
        • `SpringApplication.run(MyApp.class, args);`
          
      2. Creation and Configuration of ApplicationContext (the IoC Container)
        • Chooses ApplicationContext implementation based on WebApplicationType:
          • Servlet apps โ†’ AnnotationConfigServletWebServerApplicationContext
          • WebFlux apps โ†’ AnnotationConfigReactiveWebServerApplicationContext
          • Non-web apps โ†’ AnnotationConfigApplicationContext
        • Runs registered ApplicationContextInitializers for customization.
        • Configures Environment, ApplicationContext, and ResourceLoader.
      3. 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
      4. Component Scanning (@ComponentScan)
      5. Processing of @SpringBootConfiguration
      6. ApplicationContext Refresh (Initialization)
        • ALL BEANS are registered, initialized, and DI is performed
        • Executes CommandLineRunner and ApplicationRunner beans.
        • Publishes lifecycle events received by registered ApplicationListeners.
  3. 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()

์œ„์— ๊ฑฐ ์˜†์— ๋‘๊ณ  ๋น„๊ตํ•ด๋ณด๋ฉด ์ข‹์Œ

  1. Preparation: SpringApplication object created, Environment set up, basic ApplicationContext type determined. (Your 2.1 and part of 2.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.
  3. 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/ApplicationRunners are run.
    • Events are published. (This covers your 2.6)
  4. 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.

EventDescription
ApplicationStartingEventFired immediately after the SpringApplication.run() method is called.
ApplicationEnvironmentPreparedEventOccurs after the Environment (e.g., properties, profiles) has been prepared.
ApplicationContextInitializedEventFired right after the [[IoC (Inversion of Control)#ApplicationContext|ApplicationContext]] has been initialized but before any beans are loaded.
ApplicationPreparedEventTriggered immediately after all preparations are complete, but before the context is refreshed.
ApplicationStartedEventFired right after the ApplicationContext has been refreshed and all beans are loaded.
ApplicationReadyEventOccurs when the application is fully ready to serve requests, after all CommandLineRunner and ApplicationRunner beans have been called.
ApplicationFailedEventFired 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() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์•„๋ž˜์™€ ๊ฐ™์€ ์ฃผ์š” ๋‹จ๊ณ„๊ฐ€ ์ˆœ์ฐจ์ ์œผ๋กœ ์ˆ˜ํ–‰๋œ๋‹ค:
    1. ApplicationContext ์ค€๋น„ (prepareContext)
    2. ์ž๋™ ์„ค์ • ๋กœ๋”ฉ ๋ฐ Bean ๋“ฑ๋ก
    3. BeanFactoryPostProcessor, BeanPostProcessor ์ž‘๋™
    4. ApplicationContext ์ดˆ๊ธฐํ™” (refresh)
    5. Runner ์‹คํ–‰ (callRunners)
  • ์ด ํ๋ฆ„์€ ๋ชจ๋‘ ๋””๋ฒ„๊น…์„ ํ†ตํ•ด ์ง์ ‘ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋ฉฐ, refreshContext() ๋‚ด๋ถ€์—์„œ ์ตœ์ข…์ ์œผ๋กœ context.refresh()๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ Spring ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ์™„์ „ํ•œ ์‹คํ–‰ ์ƒํƒœ๋กœ ์ „ํ™˜๋œ๋‹ค.
  • ์‹ค์Šต์—์„œ๋Š” ๋ธŒ๋ ˆ์ดํฌํฌ์ธํŠธ๋ฅผ ํ™œ์šฉํ•ด Spring์˜ ์‹คํ–‰ ํ๋ฆ„์„ ์ถ”์ ํ•˜๊ณ , ๊ฐ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ์ ์˜ ๊ฐ์ฒด ์ƒํƒœ๋ฅผ ํ™•์ธํ•จ์œผ๋กœ์จ ํ”„๋ ˆ์ž„์›Œํฌ ๋‚ด๋ถ€ ๋™์ž‘์— ๋Œ€ํ•œ ๊นŠ์€ ์ดํ•ด๋ฅผ ํ˜•์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋Ÿฌํ•œ ์‹ค์Šต ๊ธฐ๋ฐ˜ ํ•™์Šต์€ ์ดํ›„ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์‹คํ–‰ ์˜ค๋ฅ˜๋‚˜ ์„ค์ • ์ถฉ๋Œ ์ด์Šˆ์— ๋Œ€ํ•œ ์ง„๋‹จ๊ณผ ๋ฌธ์ œ ํ•ด๊ฒฐ ์—ญ๋Ÿ‰์„ ๊ฐ•ํ™”ํ•˜๋Š” ๋ฐ ๊ธฐ์ดˆ๊ฐ€ ๋œ๋‹ค.