JAVA全系列 教程
3762个小节阅读:7091.4k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
查看注解@SpringBootApplication
的源码
@EnableAutoConfiguration:SpringBoot自动配置功能开启
查看注解@EnableAutoConfiguration
的源码
@Import(AutoConfigurationImportSelector.class)
导入了AutoConfifigurationImportSelector
类
查看AutoConfigurationImportSelector源码
SpringFactoriesLoader.loadFactoryNames
方法的作用就是从这个文件中读取指定类对应的类名称列表
点开spring-boot-autoconfigure
的spring.factories
文件
有关配置类的信息如下:
上面配置文件存在大量的以Configuration
为结尾的类名称,这些类就是存有自动配置信息的类,而SpringApplication
在获取这些类名后再加载。
我们ServletWebServerFactoryAutoConfiguration
为例来分析源码:
@EnableConfigurationProperties(ServerProperties.class)
代表加载ServerProperties
服务器配置属性类。
进入ServerProperties
类源码如下:
prefifix = "server"
表示SpringBoot配置文件中的前缀,SpringBoot会将配置文件中以server开始的属性映射到该类的字段中。所以配置网络端口的方式为server.port
如果我们没有在配置文件中配置默认端口,SpringBoot就会读取默认配置,而默认配置存放在META-INF/spring-configuration-metadata.json
中,打开spring-boot-autoconfigure
的spring.factories
文件
该文件中保存的就是所有默认配置信息。
1. 在SpringBoot的配置类信息保存在文件下
A spring-configuration-metadata.json
B spring.factories
C org.springframework.boot.autoconfigure.AutoConfiguration.imports
D ServerProperties.java
2. 在SpringBoot的默认配置信息保存在文件下
A spring-configuration-metadata.json
B spring.factories
C AutoConfifigurationImportSelector.java
D ServerProperties.java
答案
1=>C 2=>A