1、springBoot项目启动过程中的监听机制
springBoot项目启动过程有两层监听,第一层由组件【SpringApplicationRunListener】实现,第二层由组件【ApplicationListener】实现;
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);
}
接下来我们从springBoot项目启动的核心方法【org.springframework.boot.SpringApplication#run】看监听机制的具体实现:
public ConfigurableApplicationContext run(String... args) {
......
/** 加载第一层监听组件,
第二层监听组件在【SpringApplication】实例化的时候加载 **/
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);
.......
}
接下来,我们主要看下mark4处的处理:
void environmentPrepared(ConfigurableEnvironment environment) {
//循环所有的【SpringApplicationRunListener】,目前只有一个实现【EventPublishingRunListener】
for (SpringApplicationRunListener listener : this.listeners) {
listener.environmentPrepared(environment);
}
}
public void environmentPrepared(ConfigurableEnvironment environment) {
// 委托自己的事件发布器发布事件
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();
/**循环所有的【ApplicationListener】;
SpringApplication实例化的时候加载所有【ApplicationListener】的实现;
实例化【EventPublishingRunListener】的时候,把所有【ApplicationListener】的实现传递给了前者的事件发布器【SimpleApplicationEventMulticaster】**/
for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
invokeListener(listener, event);
}
}
}
2、【bootstrap】的栈帧分析

/**
6、获取配置文件名称
**/
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、第二个SpringApplication启动的时候,又会执行到BootstrapApplicationListener,但因为此时我们的Environment中有属性【spring.config.name】,
所以会跳过,接着执行下一个监听器【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、在执行第二层监听器【BootstrapApplicationListener】的时候,会创建一个新的SpringApplication然后启动,
这次传入的主类会是null,会创建新的StandardEnvironment且设置属性【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、执行第二层监听器
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、监听器委托事件广播器【SimpleApplicationEventMulticaster】广播事件
multicastEvent:127, SimpleApplicationEventMulticaster (org.springframework.context.event)
// 1、 执行第一层监听器列表,目前只有一个【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)
网友评论