Analysis of Springboot’s automatic configuration principles

1. Parent project inherited by springboot

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId> // Click to track dependencies
    <version>2.2.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

Yml is loaded with properties first, which means that properties will override the configuration of yml.

application.yml->application.yaml->application.properties

The parent project has tracking for configuration files

And then inherit the parent project

The parent project has a lot of version control

2. @SpringBootApplication bottom layer

Enter @springbootapplication

1.@springbootconfiguration contains @configuration: spring configuration reference

2.@ComponentScan

This annotation is mainly used to automatically scan each controller service dao layer. There is no need to manually write and scan like spring.

3.@EnableAutoConfiguration: automatic configuration

Enter this annotation

Enter the AtuoconfigurationImportSelector class, find getCandidateConfigurations, and observe that the return value is configurations. It can be seen that the returned configuration collection is

Enter the method getCandidateConfigurations, which loads certain configurations

Observe the message:
No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.

Translation: Autoconfiguration class not found in META-INF/spring.factories. If using custom packaging, make sure the file is correct.

This means that generally our automatic configuration files are placed under META-INF/spring.factories

The spring.factorie is the referenced configuration file under the package

 

Contents of the package

Automatically configured function classes

We select an automatic configuration class to search for, for example:

ServerProperties.class: Now we can see some configuration properties, prefixed by perfix, for example: server.port

Now we have only found the source of configuration properties, not some automatic configuration data. Its configuration content is:

By referencing the json configuration data, we can see that the default automatic configuration port is server.port=8080

 

3. Summary

Springboot uses ServerProperties to load json configuration data. After loading, it goes to the automatic configuration class ServletWebServerFactoryAutoConfiguration to introduce the ServerProperties and is finally loaded by springfactories.

Guess you like

Origin blog.csdn.net/weixin_42736075/article/details/108882239