SpringBoot基础-Environment解析监听器发布事件

本文详细介绍了SpringBoot在启动过程中如何执行Environment监听器,包括执行监听器的流程、发布事件的方式,以及EnvironmentPostProcessor的各个实现类的作用,如CloudFoundryVcapEnvironmentPostProcessor、ConfigDataEnvironmentPostProcessor等,它们分别负责添加特定属性、设置configurationProperties、处理random属性和systemEnvironment属性等。同时,文中探讨了SpringApplicationJsonEnvironmentPostProcessor处理JSON属性和SystemEnvironmentPropertySourceEnvironmentPostProcessor更新systemEnvironment属性的目的。
摘要由CSDN通过智能技术生成

由于该事件涉及的内容比较多,特地将此事件单独写出

执行监听器

void environmentPrepared(ConfigurableBootstrapContext bootstrapContext, ConfigurableEnvironment environment) {
   
	doWithListeners("spring.boot.application.environment-prepared",
			(listener) -> listener.environmentPrepared(bootstrapContext, environment));
}

private void doWithListeners(String stepName, Consumer<SpringApplicationRunListener> listenerAction,
			Consumer<StartupStep> stepAction) {
   
	StartupStep step = this.applicationStartup.start(stepName);
	this.listeners.forEach(listenerAction);
	if (stepAction != null) {
   
		stepAction.accept(step);
	}
	step.end();
}

在doWithListeners中传入了一个action:(listener) -> listener.environmentPrepared(bootstrapContext, environment)),并且在方法中遍历所有的listeners监听器,执行传入的事件

发布器执行事件

@Override
public void environmentPrepared(ConfigurableBootstrapContext bootstrapContext,
		ConfigurableEnvironment environment) {
   
	this.initialMulticaster.multicastEvent(
			new ApplicationEnvironmentPreparedEvent(bootstrapContext, this.application, this.args, environment));
}

@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
   
	ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
	Executor executor = getTaskExecutor();
	for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
   
		if (executor != null) {
   
			executor.execute(() -> invokeListener(listener, event));
		}
		else {
   
			invokeListener(listener, event);
		}
	}
}

multicastEvent 发布ApplicationEnvironmentPreparedEvent事件,根据事件属性通过getApplicationListeners(event, type),获取需要关心此次事件的监听器,然后依次执行
在这里插入图片描述

可以看出有org.springframework.boot.env.EnvironmentPostProcessorApplicationListener对此事件感兴趣

EnvironmentPostProcessorApplicationListener执行对应的事件

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

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

EnvironmentPostProcessorApplicationListener中对三个事件:ApplicationEnvironmentPreparedEvent,ApplicationPreparedEvent,ApplicationFailedEvent比较感兴趣,此时发布的事ApplicationEnvironmentPreparedEvent事件,则执行:onApplicationEnvironmentPreparedEvent((ApplicationEnvironmentPreparedEvent) event)

依次遍历执行EnvironmentPostProcessor,可以看出共有8个事件需要执行

  • org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor
  • org.springframework.boot.context.config.ConfigDataEnvironmentPostProcessor
  • org.springframework.boot.env.RandomValuePropertySourceEnvironmentPostProcessor
  • org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor
  • org.springframework.boot.env.SystemEnvironmentPropertySourceEnvironmentPostProcessor
  • org.springframework.boot.reactor.DebugAgentEnvironmentPostProcessor
  • org.springframework.boot.test.web.SpringBootTestRandomPortEnvironmentPostProcessor
  • org.springframework.boot.context.config.ConfigFileApplicationListener

在这里插入图片描述

Processor属性执行器

CloudFoundryVcapEnvironmentPostProcessor
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
   
	if (CloudPlatform.CLOUD_FOUNDRY.isActive(environment)) {
   
		Properties properties = new Properties();
		JsonParser jsonParser <
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值