一篇文章看懂SpringBoot配置文件的加载过程

本文详细解析了SpringBoot中application.properties的加载过程,包括调用线程、readEnvironment过程、resolveAndLoad的ConfigDataLocationResolvers和StandardConfigDataLocationResolver,以及ConfigDataImporter和ConfigDataEnvironmentContributors的角色。文章介绍了配置数据如何从类路径、当前目录和config目录加载,以及配置数据如何合并和处理,展示了SpringBoot配置管理的灵活性和便利性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

application.properties的读取过程

基于 springboot3.2 和 springframework6.1.4

调用的线程

在这里插入图片描述

读取过程

在 SprigApplication 中,通过 prepareEnvironment 方法创建和配置环境,然后将环境传递给 SpringApplicationRunListenersenvironmentPrepared 方法。
EventPublishingRunListenerenvironmentPrepared 方法中,并发布ApplicationEnvironmentPreparedEvent事件。

// SpringApplication.java
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
		DefaultBootstrapContext bootstrapContext, ApplicationArguments applicationArguments) {
   
	// Create and configure the environment
	ConfigurableEnvironment environment = getOrCreateEnvironment();
	configureEnvironment(environment, applicationArguments.getSourceArgs());
	ConfigurationPropertySources.attach(environment);
	listeners.environmentPrepared(bootstrapContext, environment);
	DefaultPropertiesPropertySource.moveToEnd(environment);
	Assert.state(!environment.containsProperty("spring.main.environment-prefix"),
			"Environment prefix cannot be set via properties.");
	bindToSpringApplication(environment);
	if (!this.isCustomEnvironment) {
   
		EnvironmentConverter environmentConverter = new EnvironmentConverter(getClassLoader());
		environment = environmentConverter.convertEnvironmentIfNecessary(environment, deduceEnvironmentClass());
	}
	ConfigurationPropertySources.attach(environment);
	return environment;
}

// EventPublishingRunListener.java
public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
   
	multicastInitialEvent(new ApplicationEnvironmentPreparedEvent(bootstrapContext, this.application, this.args, environment));
}

ApplicationEnvironmentPreparedEvent 事件的监听器 EnvironmentPostProcessorApplicationListener 会监听该事件。

// EnvironmentPostProcessorApplicationListener.java
@Override
public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
   
	return ApplicationEnvironmentPreparedEvent.class.isAssignableFrom(eventType)
			|| ApplicationPreparedEvent.class.isAssignableFrom(eventType)
			|| ApplicationFailedEvent.class.isAssignableFrom(eventType);
}

@Override
public void onApplicationEvent(ApplicationEvent event) {
   
	if (event instanceof ApplicationEnvironmentPreparedEvent environmentPreparedEvent) {
   
		onApplicationEnvironmentPreparedEvent(environmentPreparedEvent);
	}
	if (event instanceof ApplicationPreparedEvent) {
   
		onApplicationPreparedEvent();
	}
	if (event instanceof ApplicationFailedEvent) {
   
		onApplicationFailedEvent();
	}
}

onApplicationEnvironmentPreparedEvent方法中,会调用EnvironmentPostProcessorpostProcessEnvironment方法。

private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
   
	ConfigurableEnvironment environment = event.getEnvironment();
	SpringApplication application = event.getSpringApplication();
	for (EnvironmentPostProcessor postProcessor : getEnvironmentPostProcessors(application.getResourceLoader(),
			event.getBootstrapContext())) {
   
		postProcessor.postProcessEnvironment(environment, application);
	}
}

获取在spring.factory中配置的EnvironmentPostProcessor


@Override
public List<EnvironmentPostProcessor> getEnvironmentPostProcessors(DeferredLogFactory logFactory,
		ConfigurableBootstrapContext bootstrapContext) {
   
	ArgumentResolver argumentResolver = ArgumentResolver.of(DeferredLogFactory.class, logFactory);
	argumentResolver = argumentResolver.and(ConfigurableBootstrapContext.class, bootstrapContext);
	argumentResolver = argumentResolver.and(BootstrapContext.class, bootstrapContext);
	argumentResolver = argumentResolver.and(BootstrapRegistry.class, bootstrapContext);
	return this.loader.load(EnvironmentPostProcessor.class, argumentResolver);
}

spring.factory中配置的EnvironmentPostProcessorConfigDataEnvironmentPostProcessor是对application.properties的处理。

# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor,\
org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor,\
org.springframework.boot.env.RandomValuePropertySourceEnvironmentPostProcessor,\
org.springframework.boot.env.SpringApplicationJsonEnvironmentPostP
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值