Bootstrap

SpringBoot源码阅读(3)——监听器

ApplicationListener类初始化位置

在类SpringApplication的构造方法,第267行
在这里插入图片描述
META-INFO/spring.factories中配置的实现类
spring-boot

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.ClearCachesApplicationListener,\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.context.logging.LoggingApplicationListener,\
org.springframework.boot.env.EnvironmentPostProcessorApplicationListener

spring-boot-autoconfigure

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.autoconfigure.BackgroundPreinitializer

相关类

在这里插入图片描述

  • ApplicationEvent 事件祖先类
  • org.springframework.context.event.ApplicationEventMulticaster 事件多播器
  • org.springframework.context.event.SmartApplicationListener 监听器
  • org.springframework.context.event.GenericApplicationListener 监听器
  • org.springframework.context.event.EventListener 监听器

SpringBoot启动过程中的事件,都由org.springframework.context.event.ApplicationEventMulticaster播出去,让监听器处理。

监听器触发逻辑

从加载开始

入口在SpringApplication的构造方法,第267行
类工厂加载ApplicationListener的实现类

setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));

然后在SpringApplication的实例方法run中,第297行,加载SpringApplicationRunListeners类,这个类中持有多播器的引用。

SpringApplicationRunListeners listeners = getRunListeners(args);

SpringApplicationRunListeners实例化的时候,会通过工厂类加载SpringApplicationRunListener

private SpringApplicationRunListeners getRunListeners(String[] args) {
	Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };
	return new SpringApplicationRunListeners(logger,
			getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args),
			this.applicationStartup);
}

SpringApplicationRunListener的实现类

# Run Listeners
org.springframework.boot.SpringApplicationRunListener=\
org.springframework.boot.context.event.EventPublishingRunListener

EventPublishingRunListener中持有的监听器,就是SpringApplication中加载的ApplicationListener的实现类

public EventPublishingRunListener(SpringApplication application, String[] args) {
	this.application = application;
	this.args = args;
	this.initialMulticaster = new SimpleApplicationEventMulticaster();
	for (ApplicationListener<?> listener : application.getListeners()) {
		this.initialMulticaster.addApplicationListener(listener);
	}
}
  • ClearCachesApplicationListener
  • ParentContextCloserApplicationListener
  • FileEncodingApplicationListener
  • AnsiOutputApplicationListener
  • DelegatingApplicationListener
  • LoggingApplicationListener
  • EnvironmentPostProcessorApplicationListener
  • BackgroundPreinitializer

存储

然后这些监听器被放入了SimpleApplicationEventMulticaster简单多播器里面

@Override
public void addApplicationListener(ApplicationListener<?> listener) {
	synchronized (this.defaultRetriever) {
		// Explicitly remove target for a proxy, if registered already,
		// in order to avoid double invocations of the same listener.
		Object singletonTarget = AopProxyUtils.getSingletonTarget(listener);
		if (singletonTarget instanceof ApplicationListener) {
			this.defaultRetriever.applicationListeners.remove(singletonTarget);
		}
		this.defaultRetriever.applicationListeners.add(listener);
		this.retrieverCache.clear();
	}
}

默认的defaultRetriever则是一个内部类DefaultListenerRetriever
在这里插入图片描述

触发

会在多个地方触发,比如
SpringApplicationrun方法,第298行

listeners.starting(bootstrapContext, this.mainApplicationClass);

触发过程又涉及到事件的设计

;