1、springBoot項(xiàng)目啟動(dòng)過(guò)程中的監(jiān)聽(tīng)機(jī)制
springBoot項(xiàng)目啟動(dòng)過(guò)程有兩層監(jiān)聽(tīng),第一層由組件【SpringApplicationRunListener】實(shí)現(xiàn),第二層由組件【ApplicationListener】實(shí)現(xiàn);
public interface SpringApplicationRunListener {
default void starting() {}
default void environmentPrepared(ConfigurableEnvironment environment) {}
default void contextPrepared(ConfigurableApplicationContext context) {}
default void contextLoaded(ConfigurableApplicationContext context) {}
default void started(ConfigurableApplicationContext context) {}
default void running(ConfigurableApplicationContext context) {}
default void failed(ConfigurableApplicationContext context, Throwable exception) {}
}
@FunctionalInterface
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
void onApplicationEvent(E event);
}
接下來(lái)我們從springBoot項(xiàng)目啟動(dòng)的核心方法【org.springframework.boot.SpringApplication#run】看監(jiān)聽(tīng)機(jī)制的具體實(shí)現(xiàn):
public ConfigurableApplicationContext run(String... args) {
......
/** 加載第一層監(jiān)聽(tīng)組件,
第二層監(jiān)聽(tīng)組件在【SpringApplication】實(shí)例化的時(shí)候加載 **/
SpringApplicationRunListeners listeners = getRunListeners(args);
// mark1
listeners.starting();
try {
.......
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);
......
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
// mark2
listeners.started(context);
}
try {
// mark3
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {
......
// mark4
listeners.environmentPrepared(environment);
......
}
private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment,
SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
......
// mark5
listeners.contextPrepared(context);
......
// mark6
listeners.contextLoaded(context);
}
private void handleRunFailure(ConfigurableApplicationContext context, Throwable exception,
Collection<SpringBootExceptionReporter> exceptionReporters, SpringApplicationRunListeners listeners) {
......
// mark7
listeners.failed(context, exception);
.......
}
接下來(lái),我們主要看下mark4處的處理:
void environmentPrepared(ConfigurableEnvironment environment) {
//循環(huán)所有的【SpringApplicationRunListener】,目前只有一個(gè)實(shí)現(xiàn)【EventPublishingRunListener】
for (SpringApplicationRunListener listener : this.listeners) {
listener.environmentPrepared(environment);
}
}
public void environmentPrepared(ConfigurableEnvironment environment) {
// 委托自己的事件發(fā)布器發(fā)布事件
this.initialMulticaster
.multicastEvent(new ApplicationEnvironmentPreparedEvent(this.application, this.args, environment));
}
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
Executor executor = getTaskExecutor();
/**循環(huán)所有的【ApplicationListener】;
SpringApplication實(shí)例化的時(shí)候加載所有【ApplicationListener】的實(shí)現(xiàn);
實(shí)例化【EventPublishingRunListener】的時(shí)候,把所有【ApplicationListener】的實(shí)現(xiàn)傳遞給了前者的事件發(fā)布器【SimpleApplicationEventMulticaster】**/
for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
invokeListener(listener, event);
}
}
}
2、【bootstrap】的棧幀分析

image.png
/**
6、獲取配置文件名稱(chēng)
**/
getSearchNames:716, ConfigFileApplicationListener$Loader (org.springframework.boot.context.config)
lambda$load$8:446, ConfigFileApplicationListener$Loader (org.springframework.boot.context.config)
accept:-1, 1398260359 (org.springframework.boot.context.config.ConfigFileApplicationListener$Loader$$Lambda$115)
forEach:75, Iterable (java.lang)
load:444, ConfigFileApplicationListener$Loader (org.springframework.boot.context.config)
lambda$load$0:347, ConfigFileApplicationListener$Loader (org.springframework.boot.context.config)
accept:-1, 1037955032 (org.springframework.boot.context.config.ConfigFileApplicationListener$Loader$$Lambda$99)
apply:54, FilteredPropertySource (org.springframework.boot.context.config)
load:335, ConfigFileApplicationListener$Loader (org.springframework.boot.context.config)
addPropertySources:226, ConfigFileApplicationListener (org.springframework.boot.context.config)
postProcessEnvironment:210, ConfigFileApplicationListener (org.springframework.boot.context.config)
onApplicationEnvironmentPreparedEvent:200, ConfigFileApplicationListener (org.springframework.boot.context.config)
/**
5、第二個(gè)SpringApplication啟動(dòng)的時(shí)候,又會(huì)執(zhí)行到BootstrapApplicationListener,但因?yàn)榇藭r(shí)我們的Environment中有屬性【spring.config.name】,
所以會(huì)跳過(guò),接著執(zhí)行下一個(gè)監(jiān)聽(tīng)器【ConfigFileApplicationListener】
**/
onApplicationEvent:188, ConfigFileApplicationListener (org.springframework.boot.context.config)
doInvokeListener:172, SimpleApplicationEventMulticaster (org.springframework.context.event)
invokeListener:165, SimpleApplicationEventMulticaster (org.springframework.context.event)
multicastEvent:139, SimpleApplicationEventMulticaster (org.springframework.context.event)
multicastEvent:127, SimpleApplicationEventMulticaster (org.springframework.context.event)
environmentPrepared:80, EventPublishingRunListener (org.springframework.boot.context.event)
environmentPrepared:53, SpringApplicationRunListeners (org.springframework.boo
prepareEnvironment:345, SpringApplication (org.springframework.boot)
run:308, SpringApplication (org.springframework.boot)
/**
4、在執(zhí)行第二層監(jiān)聽(tīng)器【BootstrapApplicationListener】的時(shí)候,會(huì)創(chuàng)建一個(gè)新的SpringApplication然后啟動(dòng),
這次傳入的主類(lèi)會(huì)是null,會(huì)創(chuàng)建新的StandardEnvironment且設(shè)置屬性【spring.config.name】為【bootstrap】
**/
run:140, SpringApplicationBuilder (org.springframework.boot.builder)
bootstrapServiceContext:212, BootstrapApplicationListener (org.springframework.cloud.bootstrap)
onApplicationEvent:117, BootstrapApplicationListener (org.springframework.cloud.bootstrap)
// 3、執(zhí)行第二層監(jiān)聽(tīng)器
onApplicationEvent:74, BootstrapApplicationListener (org.springframework.cloud.bootstrap)
doInvokeListener:172, SimpleApplicationEventMulticaster (org.springframework.context.event)
invokeListener:165, SimpleApplicationEventMulticaster (org.springframework.context.event)
multicastEvent:139, SimpleApplicationEventMulticaster (org.springframework.context.event)
// 2、監(jiān)聽(tīng)器委托事件廣播器【SimpleApplicationEventMulticaster】廣播事件
multicastEvent:127, SimpleApplicationEventMulticaster (org.springframework.context.event)
// 1、 執(zhí)行第一層監(jiān)聽(tīng)器列表,目前只有一個(gè)【EventPublishingRunListener】
environmentPrepared:80, EventPublishingRunListener (org.springframework.boot.context.event)
environmentPrepared:53, SpringApplicationRunListeners (org.springframework.boot)
prepareEnvironment:345, SpringApplication (org.springframework.boot)
run:308, SpringApplication (org.springframework.boot)
run:1237, SpringApplication (org.springframework.boot)
run:1226, SpringApplication (org.springframework.boot)
main:42, GfundActbApiApplication (com.gome.gfund.actb)