源码:

​​run​​  |- 1 stopWatch.start()

 |- 2 configureHeadlessProperty

 |- 3 listeners.starting()

 |- 4 prepareEnvironment

 |- 5 printBanner(environment)

 |- 6 createApplicationContext()

 |- 7 new FailureAnalyzers(context)

 |- 8 prepareContext(...)

 |- 9 refreshContext(context)

 |- 10 afterRefresh

 |- 11 listeners.finished

 |- 12 stopWatch.stop()

 |- 13 new StartupInfoLogger(...).logStarted(...)

 |- 14 handleRunFailure(...)



2.3、getRunListeners().starting()_spring

run 方法的第3步是发送“应用启动事件”(ApplicationStartingEvent),此时会调用在对此事件感兴趣的监听器,这些监听器在初始化阶段已经实例化了。

public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
FailureAnalyzers analyzers = null;
configureHeadlessProperty();
//3 创建、调用 SpringApplicationRunListeners
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
略......
}
// **************** org.springframework.boot.SpringApplicationRunListeners ******************/
public void starting() {
for (SpringApplicationRunListener listener : this.listeners) {
listener.starting();
}
}

// **************** org.springframework.boot.context.event.EventPublishingRunListener ******************/
private final SimpleApplicationEventMulticaster initialMulticaster;
public void starting() {
this.initialMulticaster
.multicastEvent(new ApplicationStartedEvent(this.application, this.args));
}
// **************** org.springframework.context.event.SimpleApplicationEventMulticaster ******************/

@Override
public void multicastEvent(ApplicationEvent event) {
multicastEvent(event, resolveDefaultEventType(event));
}
@Override
public void multicastEvent(final ApplicationEvent event, ResolvableType eventType) {
//org.springframework.boot.context.event.ApplicationStartedEvent
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
/** getApplicationListeners(event, type) 最终选出来下面4个
* 5 = "o.s.b.context.config.DelegatingApplicationListener"
* 6 = "o.s.b.liquibase.LiquibaseServiceLocatorApplicationListener"
* 8 = "o.s.b.logging.LoggingApplicationListener"
* 9 = "o.s.b.autoconfigure.BackgroundPreinitializer"
*/
for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
Executor executor = getTaskExecutor();
if (executor != null) {
executor.execute(new Runnable() {
@Override
public void run() {
invokeListener(listener, event);
}
});
}
else {
invokeListener(listener, event);
}
}
}
protected Collection<ApplicationListener<?>> getApplicationListeners(
ApplicationEvent event, ResolvableType eventType) {
// eventType 为:org.springframework.boot.context.event.ApplicationStartedEvent
Object source = event.getSource();//SpringApplication实例
Class<?> sourceType = (source != null ? source.getClass() : null);
ListenerCacheKey cacheKey = new ListenerCacheKey(eventType, sourceType);

// Quick check for existing entry on ConcurrentHashMap...
ListenerRetriever retriever = this.retrieverCache.get(cacheKey);
if (retriever != null) {
return retriever.getApplicationListeners();
}

if (this.beanClassLoader == null ||
(ClassUtils.isCacheSafe(event.getClass(), this.beanClassLoader) &&
(sourceType == null || ClassUtils.isCacheSafe(sourceType, this.beanClassLoader)))) {
// Fully synchronized building and caching of a ListenerRetriever
synchronized (this.retrievalMutex) {
retriever = this.retrieverCache.get(cacheKey);
if (retriever != null) {
return retriever.getApplicationListeners();
}
retriever = new ListenerRetriever(true);
Collection<ApplicationListener<?>> listeners =
retrieveApplicationListeners(eventType, sourceType, retriever);
this.retrieverCache.put(cacheKey, retriever);
return listeners;
}
}
else {
// No ListenerRetriever caching -> no synchronization necessary
return retrieveApplicationListeners(eventType, sourceType, null);
}
}
/**
*
*/
private Collection<ApplicationListener<?>> retrieveApplicationListeners(
ResolvableType eventType, Class<?> sourceType, ListenerRetriever retriever) {

LinkedList<ApplicationListener<?>> allListeners = new LinkedList<ApplicationListener<?>>();
Set<ApplicationListener<?>> listeners;
Set<String> listenerBeans;
synchronized (this.retrievalMutex) {
listeners = new LinkedHashSet<ApplicationListener<?>>(this.defaultRetriever.applicationListeners);
listenerBeans = new LinkedHashSet<String>(this.defaultRetriever.applicationListenerBeans);
}
for (ApplicationListener<?> listener : listeners) {
//
if (supportsEvent(listener, eventType, sourceType)) {
if (retriever != null) {
retriever.applicationListeners.add(listener);
}
allListeners.add(listener);
}
}
if (!listenerBeans.isEmpty()) {
BeanFactory beanFactory = getBeanFactory();
for (String listenerBeanName : listenerBeans) {
try {
Class<?> listenerType = beanFactory.getType(listenerBeanName);
if (listenerType == null || supportsEvent(listenerType, eventType)) {
ApplicationListener<?> listener =
beanFactory.getBean(listenerBeanName, ApplicationListener.class);
if (!allListeners.contains(listener) && supportsEvent(listener, eventType, sourceType)) {
if (retriever != null) {
retriever.applicationListenerBeans.add(listenerBeanName);
}
allListeners.add(listener);
}
}
}
catch (NoSuchBeanDefinitionException ex) {
// Singleton listener instance (without backing bean definition) disappeared -
// probably in the middle of the destruction phase
}
}
}
AnnotationAwareOrderComparator.sort(allListeners);
return allListeners;
}