springboot加载properties文件过程详解

参考文档
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 类关键属性

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
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值