参考文档
SpringBoot配置文件的加载位置实例详解
通过源码了解springboot加载application.properties过程
从SpringBoot源码分析 配置文件的加载原理和优先级
总结
spring.config.location 加载配置文件的路径
没有配置默认值为 classpath:/,classpath:/config/,file:./,file:./config/
spring.config.name 加载配置文件的文件名
没有配置默认值为 application
spring.config.additional-location 加载配置文件的额外路径
没有配置spring.config.location 才加载额外路径
加载配置文件
location+name+[-profile]+fileExtension location为路径
一定会加载 profile 为 null的情况
获取配置文件中的属性优先级
1 配置了 spring.profiles.active=dev,test 配置的顺序倒叙优先
2 文件位置 location classpath:/,classpath:/config/,file:./,file:./config/ 倒叙优先
文章目录
-
- ConfigFileApplicationListener 类关键属性
- ConfigFileApplicationListener 监听事件类型
- springboot 广播监听的触发时机
- 触发onApplicationEvent
- onApplicationEnvironmentPreparedEvent
- postProcessEnvironment
- Loader.load 过程
-
- 关键类
- load
-
- 初始化Profiles initializeProfiles
- load (Profile profile, DocumentFilterFactory filterFactory, DocumentConsumer consumer)
- 获取加载配置文件路径 getSearchLocations
- 获取加载配置文件 文件名 getSearchNames
- 根据文件路径和文件名加载配置
- loadForFileExtension
- load(PropertySourceLoader loader, String location, Profile profile,DocumentFilter filter, DocumentConsumer consumer)
- loadDocuments
- 根据PropertySource创建Document
- addLoadedPropertySources 将加载的PropertySource 添加到Environment
ConfigFileApplicationListener 类关键属性
public class ConfigFileApplicationListener
implements EnvironmentPostProcessor, SmartApplicationListener, Ordered {
private static final String DEFAULT_PROPERTIES = "defaultProperties";
// Note the order is from least to most specific (last one wins)
// springboot 加载配置文件的默认位置
private static final String DEFAULT_SEARCH_LOCATIONS = "classpath:/,classpath:/config/,file:./,file:./config/";
private static final String DEFAULT_NAMES = "application";
/**
* The "active profiles" property name.
*/
public static final String ACTIVE_PROFILES_PROPERTY = "spring.profiles.active";
/**
* The "includes profiles" property name.
*/
public static final String INCLUDE_PROFILES_PROPERTY = "spring.profiles.include";
/**
* The "config name" property name.
*/
public static final String CONFIG_NAME_PROPERTY = "spring.config.name";
/**
* The "config location" property name.
*/
public static final String CONFIG_LOCATION_PROPERTY = "spring.config.location";
/**
* The "config additional location" property name.
*/
public static final String CONFIG_ADDITIONAL_LOCATION_PROPERTY = "spring.config.additional-location";
}
ConfigFileApplicationListener 监听事件类型
ApplicationEnvironmentPreparedEvent
ApplicationPreparedEvent
springboot 广播监听的触发时机
//springboot run方法
org.springframework.boot.SpringApplication#run(java.lang.String...)
// 准备 ConfigurableEnvironment
ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);
//广播ApplicationEnvironmentPreparedEvent事件
listeners.environmentPrepared(environment);
//加载配置文件监听执行
ConfigFileApplicationListener
触发onApplicationEvent
@Override
public void onApplicationEvent(ApplicationEvent event) {
// 监听 ApplicationEnvironmentPreparedEvent
if (event instanceof ApplicationEnvironmentPreparedEvent) {
onApplicationEnvironmentPreparedEvent(
(ApplicationEnvironmentPreparedEvent) event);
}
// 监听ApplicationPreparedEvent
if (event instanceof ApplicationPreparedEvent) {
onApplicationPreparedEvent(event);
}
}
onApplicationEnvironmentPreparedEvent
private void onApplicationEnvironmentPreparedEvent(
ApplicationEnvironmentPreparedEvent event) {
// 加载META-INF/spring.factories下的EnvironmentPostProcessor
// springboot默认有三个实现
List<EnvironmentPostProcessor> postProcessors = loadPostProcessors();
//加入ConfigFileApplicationListener 也实现了EnvironmentPostProcessor
postProcessors.add(this);
AnnotationAwareOrderComparator.sort(postProcessors);
for (EnvironmentPostProcessor postProcessor : postProcessors) {
// 执行 EnvironmentPostProcessor的 postProcessEnvironment(ConfigurableEnvironment environment,SpringApplication application) 方法
postProcessor.postProcessEnvironment(event.getEnvironment(),
event.getSpringApplication());
}
}
META-INF/spring.factories EnvironmentPostProcessor配置
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\
org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor,\
org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor
postProcessEnvironment
postProcessEnvironment
public void postProcessEnvironment(ConfigurableEnvironment environment,
SpringApplication application) {
//默认情况下 application.getResourceLoader()为空
addPropertySources(environment, application.getResourceLoader());
}
addPropertySources
protected void addPropertySources(ConfigurableEnvironment environment,
ResourceLoader resourceLoader) {
// 添加 systemEnvironment RandomValuePropertySource
RandomValuePropertySource.addToEnvironment(environment);
//加载配置文件
new Loader(environment, resourceLoader).load();
}
Loader.load 过程
关键类
Loader
private class Loader {
private final ConfigurableEnvironment environment;
private final PropertySourcesPlaceholdersResolver placeholdersResolver;
private final ResourceLoader resourceLoader;
//
private final List<PropertySourceLoader> propertySourceLoaders;
private Deque<Profile> profiles;
private List<Profile> processedProfiles;
private boolean activatedProfiles;
//存储 MutablePropertySources
private Map<Profile, MutablePropertySources> loaded;
private Map<DocumentsCacheKey, List<Document>> loadDocumentsCache = new HashMap<>();
Loader(ConfigurableEnvironment environment