2. ApplicationStartingEvent事件的发布
概览:
从获取SpringApplicationRunListeners
到发布ApplicationStartingEvent
事件的发布及
SpringBoot启动流程分析:SpringApplication初始化
中获取到的监听器ApplicationListener
对该事件的处理。
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
//从spring.factories中获取SpringApplicationRunListeners并进行初始化
//目前SpringApplicationRunListeners只有一个,org.springframework.boot.context.event.EventPublishingRunListener
//这个EventPublishingRunListener在springboot启动的过程中起到十分重要的
//作用,后续的事件发布全部都是依赖于它的
SpringApplicationRunListeners listeners = getRunListeners(args);
//使用EventPublishingRunListener发布ApplicationStartingEventh事件,并让监听该事件的listeners进行相应的处理
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] {
ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context