springboot(4)-principle of automatic configuration

1 Detailed source code process

Insert picture description hereClick on the note: @SpringBootApplicationthe main entrance of the program.
Insert picture description here
Note: @SpringBootConfiguration @EnableAutoConfiguration
special note: @EnableAutoConfigurationautomatically import the configuration.
Click on the note: @EnableAutoConfiguration
Insert picture description here
note: @Import(AutoConfigurationImportSelector.class)
Insert picture description here
Insert picture description here
note: List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);candidate configuration.
Insert picture description here
Click: loadFactoryNames
Insert picture description here
Insert picture description here
note: FACTORIES_RESOURCE_LOCATION
Insert picture description here
get:public static final String FACTORIES_RESOURCE_LOCATION = "META-INF/spring.factories";
Insert picture description here

2 How the configuration file operates

In spring.factories, we find the launcher and org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\enter HttpEncodingAutoConfigurationit to see the Insert picture description here
automatic binding properties. @EnableConfigurationProperties(ServerProperties.class)Click to see. Insert picture description here
We can application.yamlmodify it through the configuration file .
Insert picture description here
Click private final Encoding encoding = new Encoding();to get the
Insert picture description here
configuration file. What can be written? What is the connection with spring.factories?
xxxAutoConfiguration: The default value of xxxProperties is bound to the configuration file, and I can use a custom configuration.

一但这个配置类生效;这个配置类就会给容器中添加各种组件;
这些组件的属性是从对应的properties类中获取的,这些类里面的每一个属性又是和配置文件绑定的;
所有在配置文件中能配置的属性都是在xxxxProperties类中封装着;
配置文件能配置什么就可以参照某个功能对应的这个属性类

1. SpringBoot will load a large number of automatic configuration classes.
2. Let's see if the functions we need are in the automatic configuration classes written by SpringBoot by default;
3. Let's look at which components are configured in this automatic configuration class; (as long as The components we want to use exist in it, so we don't need to configure them manually)
4. When adding components to the container's automatic configuration class, certain properties will be obtained from the properties class. We only need to specify the value of these properties in the configuration file;
xxxxAutoConfigurartion: automatic configuration class; add components to the container
xxxxProperties: package the relevant properties in the configuration file;

Guess you like

Origin blog.csdn.net/zs18753479279/article/details/112426413