由于该事件涉及的内容比较多,特地将此事件单独写出
执行监听器
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 <