SpringBoot automatic assembly
SpringBoot startup process
注意:此博文只为记录自己DEBUG 源码核心过程,大家可能看不太明白,还请谅解!
Start preparation
- Start class as entry
- Enter the run method
- Created a SpringBoot program
- SpringApplication Constructor
- Enter the getSpringFactoriesInstances method
- Enter the loadFactoryNames method
- Enter loadSpringFactories
The resource path loaded here is exactly the path of spring.factories!
Back to the getSpringFactoriesInstances method
Instantiate through reflection
- After loading the classes under ApplicationContextInitializer in spring.factories, a total of seven classes are loaded
The seven categories are:
- Set the obtained class collection to the initializers property
-
ApplicationListener is similar to the above
Core steps of operation
- Environmental preparation
- Enter the prepareContext method
-
Load the main startup class at the end of the prepareContext method
-
Enter the load() method
在@SpringBootApplication =》 @SpringBootConfiguration =》 @Configuration中
- Enter the AbstractApplicationContext.refresh() method
Look at this class, use the global search to find that there is no such class
Search for the AnnotationConfigUtils class, and find that there is an attribute in it, whose value is the same as the previous class
Find the use of this attribute, and found that it is actually operating a ConfigurationClassPostProcessor class
This class happens to be an extended class of BeanFactoryPostProcessor
Continue to execute and enter this method
At this point, the program enters the ConfigurationClassPostProcessor class mentioned earlier
If it is not modified by @Configuration, return directly. Obviously, the main startup class is modified, so continue to execute
Sort the main startup class here
Parse classes modified by @Configuration
Until entering
Finding this method is the real analysis logic
Focus on @Import
There are mainly two
Continue execution and return to the parse method
Find the bottom process method
The EnableAutoConfiguration class here and the corresponding in spring.factories