2.3、getRunListeners().starting()

本文详细解析了Spring Boot应用程序的启动流程,从run方法的执行步骤到ApplicationStartingEvent事件的触发,再到监听器如何响应这一事件。深入探讨了事件监听机制在Spring Boot启动过程中的作用。

源码:
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(...)

在这里插入图片描述

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java硕哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值