1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
|
公共 ConfigurableApplicationContext (文字列...引数) { ストップウォッチストップウォッチ=新しい新しいストップウォッチ(); stopWatch.startを();
コンテキスト= ConfigurableApplicationContextのヌル ; FailureAnalyzers =アナライザのヌル ; この .configureHeadlessProperty(); リスナーでSpringApplicationRunListeners = この .getRunListeners(引数); listeners.starting ();
トライ { ApplicationArguments applicationArguments = 新しい新しい DefaultApplicationArguments(引数)を、 ConfigurableEnvironment環境= この .prepareEnvironment(リスナー、applicationArguments)を、
バナーprintedBanner = この .printBanner(環境)。
コンテキスト= この .createApplicationContext()。新しい FailureAnalyzers(コンテキスト)。この .prepareContext(コンテキスト、環境、リスナー、applicationArguments、printedBanner)。この .refreshContext(文脈)この .afterRefresh(文脈、applicationArguments)。
listeners.finished(コンテキスト(のThrowable)NULL); stopWatch.stop(); もし(この .logStartupInfo){ (新しい StartupInfoLogger(この .mainApplicationClass))logStarted(。この .getApplicationLog()、ストップウォッチ)。 }
戻り値のコンテキスト。 } キャッチ(Throwableをvar9){ この .handleRunFailure(文脈、リスナー、(FailureAnalyzers)分析器、var9)。スロー新しい IllegalStateExceptionを(var9を)。 } }
|
コアステップ1:スタートSpringApplicationRunListener
1 2
|
SpringApplicationRunListenersリスナー= この .getRunListeners(引数)。 listeners.starting();
|
SpringApplicationRunListenerインタフェース:
1 2 3 4 5 6 7 8 9 10 11
|
パブリック インターフェース SpringApplicationRunListener { ボイド開始() 。
空 environmentPrepared (ConfigurableEnvironment VAR1) 。
空 contextPrepared (ConfigurableApplicationContext VAR1) 。
空 contextLoaded (ConfigurableApplicationContext VAR1) 。
void finished(ConfigurableApplicationContext var1, Throwable var2); }
|
SpringApplicationRunListener 接口目前只有 EventPublishingRunListener 一个实现类。
1 2 3 4
|
private SpringApplicationRunListeners getRunListeners(String[] args) { Class<?>[] types = new Class[]{SpringApplication.class, String[].class}; return new SpringApplicationRunListeners(logger, this.getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args)); }
|
同理也是实例化 SpringApplicationRunListener 接口实现类,最终实际是调用 EventPublishingRunListener 的 starting() 方法:
1 2 3
|
public void starting() { this.initialMulticaster.multicastEvent(new ApplicationStartedEvent(this.application, this.args)); }
|
最终调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) { ResolvableType type = eventType != null ? eventType : this.resolveDefaultEventType(event); Iterator var4 = this.getApplicationListeners(event, type).iterator();
while(var4.hasNext()) { final ApplicationListener<?> listener = (ApplicationListener)var4.next(); Executor executor = this.getTaskExecutor(); if (executor != null) { executor.execute(new Runnable() { public void () { SimpleApplicationEventMulticaster.this.invokeListener(listener, event); } }); } else { this.invokeListener(listener, event); } }
}
|
SpringApplicationRunListener 会被构建为 AppicationEvent,然后被 ApplicationEventMulticaster 广播,最后转变为 ApplicationListener。
这中间比较复杂,看的也不是特别的深入,后续继续深入分析。
原文:大专栏 Spring Boot 启动源码解析系列六:执行启动方法一