A nanny-level guide to introduce you to the startup principle of SringBoot, which will impress the interviewer.

A nanny-level guide to introduce you to the startup principle of SringBoot, which will impress the interviewer.

1. Introduction

graph TD;
A[Startup class] --> B[SpringApplication.run()]
B --> C[ Create SpringApplication instance]
C --> D[Initialize application context]
D --> E[Load application configuration]
E --> F[Execute custom initializer]
F --> G[Execute initialize method of ApplicationContextInitializer]
G -- > H[Prepare context]
H --> I[Load configuration source (default is application.properties)]
I --> J [Parse the configuration source]
J --> K[Save the parsing results in the Environment object]
K --> L[Execute custom attributes Source loader]
L --> M[Execute SpringApplicationListeners]
M --> N[Execute ApplicationStartingEvent event listener]< a i=14> N --> O[Create Application Context] O --> P[Register ShutdownHook] P --> Q[ Prepare context] Q --> R[Execute the initialize method of ApplicationContextInitializer] R --> S[Execute SpringApplicationListeners]< a i=19> S --> T[Execute ApplicationEnvironmentPreparedEvent event listener] T --> U[Load BeanDefinition] U --> V[Execute SpringApplicationListeners] V --> W[Execute ApplicationContextInitializedEvent event listener] W --> X[Refresh application context] > A1[Execute the run method of CommandLineRunner and ApplicationRunner interface]












2. Detailed introduction

When we start a Spring Boot application, Spring Boot will perform a series of operations in a certain order to complete the initialization and startup of the application. The following are the detailed steps of Spring Boot startup principle:

  1. SpringApplication.run()Call the method in the startup class to start the Spring Boot application.

  2. Create SpringApplication instance and do some initialization work on the application. This includes setting up the application's class loader, initializing the default exception handler, etc.

  3. Initialize the application context, that is, create the ApplicationContext object. Application context is the core container of the Spring framework and is responsible for managing and organizing Bean objects. By calling the method of SpringApplication, the corresponding application context is created based on the environment configuration and application type. createApplicationContext()

  4. Load application configuration, the default configuration file is application.properties. SpringApplication will load the configuration file from the default location through the static method of SpringApplication, such as classpath:application.properties or classpath:application.yml .

  5. executes the custom initializer, that is, executes the method in the class that implements the ApplicationContextInitializer interface. These initializers can customize the application context before it is created, such as modifying the configuration, adding property sources, etc. initialize()

  6. prepares the context environment, that is, initializes the attribute source for the Environment object, parses the configuration source, and stores the parsing result into the Environment object. Environment is an abstract interface, Spring Boot uses StandardEnvironment to implement it. When preparing the context, the application loads and parses the configuration file and stores the parsed results in Environment.

  7. Execute the custom attribute source loader, that is, execute the method in the class that implements the PropertySourceLoader interface. Custom property source loaders can be used to load additional property sources, such as databases, remote configuration centers, etc. load()

  8. executes SpringApplicationListeners, that is, executes the method in the class that implements the SpringApplicationListener interface. These listeners can listen and process at different stages of the application, such as performing some operations before and after the application starts. onApplicationEvent()

  9. Execute ApplicationStartingEvent event listener. This is a special event used to notify listeners that the application is about to start.

  10. Create an application context, that is, create the corresponding ApplicationContext object according to the configuration. Spring Boot supports multiple types of application contexts, such as AnnotationConfigApplicationContext, ClassPathXmlApplicationContext, etc. The specific application context type is determined by the environment configuration.

  11. Register ShutdownHook to call back the shutdown method when the JVM is shut down. This ensures that the application completes necessary cleanup before shutting down gracefully.

  12. prepares the context environment, that is, initializes the attribute source for the Environment object, parses the configuration source, and stores the parsing result into the Environment object.

  13. Execute the method of ApplicationContextInitializer. Here is an opportunity to implement a custom initializer again, allowing for more customization of the application context. initialize()

  14. executes SpringApplicationListeners, that is, executes the method in the class that implements the SpringApplicationListener interface. These listeners allow additional monitoring and processing of the application context. onApplicationEvent()

  15. Execute ApplicationEnvironmentPreparedEvent event listener. This event notifies the listener that the application environment is ready for further configuration.

  16. loads BeanDefinition, that is, loads the beans in the configuration file into ApplicationContext. In this step, Spring Boot scans the application's class path, creates the corresponding BeanDefinition based on the information in the configuration file, and registers it in the application context.

  17. executes SpringApplicationListeners, that is, executes the method in the class that implements the SpringApplicationListener interface. These listeners can listen and process during the loading process of the BeanDefinition. onApplicationEvent()

  18. Execute ApplicationContextInitializedEvent event listener. This event notifies the listener that the application context has been initialized.

  19. Refreshes the application context, which completes the initialization work of ApplicationContext. During the refresh process, Spring Boot will execute a series of life cycle callback methods, including initializing beans, dependency injection, post-processing, etc.

  20. executes SpringApplicationListeners, that is, executes the method in the class that implements the SpringApplicationListener interface. These listeners can listen and process during application context refresh. onApplicationEvent()

  21. Execute ApplicationStartedEvent event listener. This event notifies the listener that the application has been started.

  22. Execute the methods of the CommandLineRunner and ApplicationRunner interfaces. These interfaces can perform some specific tasks after the application is started, such as initializing data, starting scheduled tasks, etc. run()

Through these steps, Spring Boot initializes and starts the application. Developers can customize and extend it as needed to achieve more flexible and efficient applications.

Insert image description here

Guess you like

Origin blog.csdn.net/qq_49841284/article/details/134818096