Springboot的自动配置原理分析
一、springboot继承的父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> //点击去追踪依赖
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
yml先加载与properties,代表properties会覆盖yml的配置
application.yml->application.yaml->application.properties
该父项目有对配置文件的追踪
以及再继承父项目
该父项目有很多对版本的控制
二、@SpringBootApplication底层
进入@springbootapplication
[email protected]包含了@configuration:spring的配置引用
该注解主要是自动扫描各个controller service dao层,无需像spring手动写扫描
[email protected] :自动配置
进入该注解
进入该类AtuoconfigurationImportSelector,找个getCandidateConfigurations,观察返回值为configurations可知返回的是配置集合
进入该方法getCandidateConfigurations,该方法是加载某些配置
观察message: No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.
翻译:在META-INF/spring.factories中找不到自动配置类. 如果使用自定义打包,请确保文件正确。
意思是一般我们的自动配置文件是放在META-INF/spring.factories底下的
该spring.factorie是引用的配置文件,在该包下
该包内容
自动配置的功能类
我们选择一个自动配置类前往搜索,例如:
ServerProperties.class:现在我们可以看到一些配置的属性,perfix为前缀,例:server.port
现在我们只是找到了配置属性的源头,还没有找到一些自动配置数据。其配置内容在:
通过引用该json的配置数据,可以看出默认自动配置端口是server.port=8080