[Source code analysis] DEBUG SpringBoot automatic assembly source code

SpringBoot automatic assembly

SpringBoot startup process

注意:此博文只为记录自己DEBUG 源码核心过程,大家可能看不太明白,还请谅解!

Start preparation

  1. Start class as entry

Insert picture description here

  1. Enter the run method

Insert picture description here

  1. Created a SpringBoot program

Insert picture description here

  1. SpringApplication Constructor

Insert picture description here

  1. Enter the getSpringFactoriesInstances method

Insert picture description here

  1. Enter the loadFactoryNames method

Insert picture description here

  1. Enter loadSpringFactories

Insert picture description here

The resource path loaded here is exactly the path of spring.factories!

Back to the getSpringFactoriesInstances method

Insert picture description here

​ Instantiate through reflection

Insert picture description here

  1. After loading the classes under ApplicationContextInitializer in spring.factories, a total of seven classes are loaded

Insert picture description here

​ The seven categories are:

Insert picture description here

Insert picture description here

  1. Set the obtained class collection to the initializers property

Insert picture description here

  1. ApplicationListener is similar to the above

    Insert picture description here

Core steps of operation

  1. Environmental preparation

Insert picture description here

Insert picture description here

  1. Enter the prepareContext method

Insert picture description here

  1. Load the main startup class at the end of the prepareContext method

    Insert picture description here

  2. Enter the load() method

    Insert picture description here

    Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

在@SpringBootApplication =》 @SpringBootConfiguration =》 @Configuration中

Insert picture description here

Insert picture description here

Insert picture description here

  1. Enter the AbstractApplicationContext.refresh() method

Insert picture description here

Insert picture description here

Insert picture description here

Look at this class, use the global search to find that there is no such class

Insert picture description here

Search for the AnnotationConfigUtils class, and find that there is an attribute in it, whose value is the same as the previous class

Insert picture description here

Find the use of this attribute, and found that it is actually operating a ConfigurationClassPostProcessor class

Insert picture description here

This class happens to be an extended class of BeanFactoryPostProcessor

Insert picture description here

Continue to execute and enter this method

Insert picture description here

Insert picture description here

Insert picture description here

At this point, the program enters the ConfigurationClassPostProcessor class mentioned earlier

Insert picture description here

If it is not modified by @Configuration, return directly. Obviously, the main startup class is modified, so continue to execute

Insert picture description here

Sort the main startup class here
Insert picture description here

Parse classes modified by @Configuration

Insert picture description here

Insert picture description here

Insert picture description here

Until entering

Insert picture description here

Finding this method is the real analysis logic

Insert picture description here

Insert picture description here

Insert picture description here

Focus on @Import

Insert picture description here

Insert picture description here

Insert picture description here

There are mainly two

Insert picture description here

Insert picture description here

Continue execution and return to the parse method

Insert picture description here

Find the bottom process method
Insert picture description here

Insert picture description here

Insert picture description here
Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

Insert picture description here

The EnableAutoConfiguration class here and the corresponding in spring.factories

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_42380734/article/details/108088513