大家对ApplicationListener应该不会陌生,但是大家是否了解Spring事件监听机制是怎么实现的呢?让我们一起来看源码!
Spring的事件监听机制是观察者模式的一个典型应用,观察者模式试图定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新。
首先我们明确事件监听的三个要素:事件、事件监听器、事件源。
在spring的事件监听机制中,ApplicationEvent充当事件的角色。所有的事件都要继承ApplicationEvent。
public abstract class ApplicationEvent extends EventObject {
/** use serialVersionUID from Spring 1.2 for interoperability. */
private static final long serialVersionUID = 7099057708183571937L;
/** System time when the event happened. */
private final long timestamp;
/**
* Create a new ApplicationEvent.
* @param source the object on which the event initially occurred (never {@code null})
*/
public ApplicationEvent(Object source) {
super(source);
this.timestamp = System.currentTimeMillis();
}
ApplicationListener充当事件监听器的角色,当事件发生时,进行对应的操作。它对应着观察者模式中的观察者Observer。当监听的事件发生后该方法会被执行。
@FunctionalInterface
public interface ApplicationListener extends EventListener {
/**
* Handle an application event.
* @param event the event to respond to
*/
void onApplicationEvent(E event);
}
事件源是事件发起的地方,ApplicationEventPublisher接口下的方法就是发布事件的,实现该接口便可以作为事件源,AbstractApplicationContext实现了该接口,在finishRefresh()方法中就会调用publishEvent()方法发布事件。
@FunctionalInterface
public interface ApplicationEventPublisher {
/**
* Notify all matching listeners registered with this
* application of an application event. Events may be framework events
* (such as RequestHandledEvent) or application-specific events.
* @param event the event to publish
* @see org.springframework.web.context.support.RequestHandledEvent
*/
default void publishEvent(ApplicationEvent event) {
publishEvent((Object) event);
}
/**
* Notify all matching listeners registered with this
* application of an event.
* If the specified {@code event} is not an {@link ApplicationEvent},
* it is wrapped in a {@link PayloadApplicationEvent}.
* @param event the event to publish
* @since 4.2
* @see PayloadApplicationEvent
*/
void publishEvent(Object event);
}
从一个简单的例子开始分析源码
定义一个MyConfigApplicationContext继承与AnnotationConfigApplicationContext
public class MyConfigApplicationContext extends AnnotationConfigApplicationContext {
public MyConfigApplicationContext(Class c) {
super(c);
}
@Override
protected void onRefresh() throws BeansException {
this.publishEvent(new ApplicationEvent("我手动发布了一个事件") {
@Override
public Object getSource() {
return super.getSource();
}
});
super.onRefresh();
}
}
我们先不讨论为何要这样写,继续写配置类
@Configuration
@ComponentScan(basePackages = {"com.example.demo"})
public class MainConfig {
}
然后主类
public class DemoApplication {
public static void main(String[] args) {
MyConfigApplicationContext ctx = new MyConfigApplicationContext(MainConfig.class);
}
}
运行一下
顺着这个跟踪一下源码
可以看到在创建自定义的bean之前监听器已经监听到事件发生了,所以我们从ApplicationContext的构造器开始看起
public AnnotationConfigApplicationContext(Class>... annotatedClasses) {
//创建关键组件
this();
//注册配置类MainConfig的bean定义(BeanDefinition),此时还未实例化bean
register(annotatedClasses);
refresh();
}
重点看refresh()方法
@Override
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
可以看到initApplicationEventMulticaster()方法,看这名字就觉得跟事件监听的有关,该方法初始化事件广播器。
protected void initApplicationEventMulticaster() {
//先获取beanFactory
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
//看看是否有applicationEventMulticaster这个bean存在
if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
this.applicationEventMulticaster =
beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
if (logger.isTraceEnabled()) {
logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
}
else {
//创建一个SimpleApplicationEventMulticaster,并注册到容器中
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
if (logger.isTraceEnabled()) {
logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
"[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
}
}
}
然后看registerListeners()方法,明显也跟事件监听有关
protected void registerListeners() {
//去容器中把applicationListener 捞取出来注册到广播器上去(系统的)
for (ApplicationListener> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
//注册我们自己实现了ApplicationListener 的组件
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
// 发布早期事件(防止某些事件出现在广播器还没有初始化的时候,漏掉该部分事件)
Set earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
//广播器广播早期事件
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
容器在registerListeners()方法之前publishEvent的都是早期事件,所以我们重写了onRefresh()方法,并在其中发布了一个事件,该事件为早期事件,然后在registerListeners时,被广播器广播到监听器。
finishRefresh()方法中有发布事件。
protected void finishRefresh() {
// Clear context-level resource caches (such as ASM metadata from scanning).
clearResourceCaches();
// Initialize lifecycle processor for this context.
initLifecycleProcessor();
// Propagate refresh to lifecycle processor first.
getLifecycleProcessor().onRefresh();
// Publish the final event.
publishEvent(new ContextRefreshedEvent(this));
// Participate in LiveBeansView MBean, if active.
LiveBeansView.registerApplicationContext(this);
}
protected void publishEvent(Object event, @Nullable ResolvableType eventType) {
Assert.notNull(event, "Event must not be null");
// Decorate event as an ApplicationEvent if necessary
ApplicationEvent applicationEvent;
//支持两种类型的事件
//1、直接继承ApplicationEven
//2、其他事件,会被包装为PayloadApplicationEvent,可以使用getPayload获取真实的通知内容
if (event instanceof ApplicationEvent) {
applicationEvent = (ApplicationEvent) event;
}
else {
applicationEvent = new PayloadApplicationEvent<>(this, event);
if (eventType == null) {
eventType = ((PayloadApplicationEvent>) applicationEvent).getResolvableType();
}
}
// 如果earlyApplicationEvents不为空,便把事件加入到早期事件集合中
if (this.earlyApplicationEvents != null) {
this.earlyApplicationEvents.add(applicationEvent);
}
else {
//广播事件
getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
}
// 如果设置了父容器,父容器同样发布事件
if (this.parent != null) {
if (this.parent instanceof AbstractApplicationContext) {
((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
}
else {
this.parent.publishEvent(event);
}
}
}
那什么时候earlyApplicationEvents不为空,什么时候为空呢?
可以看到在refresh()方法执行perpareRefresh()方法时,实例化earlyApplicationEvents集合,所以在此方法执行后发布的事件会被加入早期事件集合,到执行registerListeners方法,该方法会广播早期事件,而且把earlyApplicationEvents设置为null,所以在registerListeners后发布的事件不是早期事件,广播器直接广播。
看下SimpleApplicationEventMulticaster.multicastEvent方法,看看其是如何广播的
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
//获取事件类型,如:org.springframework.boot.context.event.ApplicationStartingEvent
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
//获取监听该事件的监听器,并循环每个监听器
for (final ApplicationListener> listener : getApplicationListeners(event, type)) {
Executor executor = getTaskExecutor();
//是否异步执行
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
invokeListener(listener, event);
}
}
}
multicastEvent()方法里面调用了invokeListener()
protected void invokeListener(ApplicationListener> listener, ApplicationEvent event) {
ErrorHandler errorHandler = getErrorHandler();
//是否设置了错误处理器
if (errorHandler != null) {
try {
doInvokeListener(listener, event);
}
catch (Throwable err) {
errorHandler.handleError(err);
}
}
else {
doInvokeListener(listener, event);
}
}
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
try {
//可以看到回调了监听器的onApplicationEvent()方法,典型的观察者设计模式
//广播器持有监听器(观察者),然后在事件发生时,广播器回调监听器的方法
listener.onApplicationEvent(event);
}
catch (ClassCastException ex) {
String msg = ex.getMessage();
if (msg == null || matchesClassCastMessage(msg, event.getClass())) {
// Possibly a lambda-defined listener which we could not resolve the generic event type for
// -> let's suppress the exception and just log a debug message.
Log logger = LogFactory.getLog(getClass());
if (logger.isTraceEnabled()) {
logger.trace("Non-matching event type for listener: " + listener, ex);
}
}
else {
throw ex;
}
}
}
而广播器广播其实就是观察者模式,广播器持有监听器,在广播时回调监听器的onApplicationEvent()方法。过程如下图:
这就是spring事件监听机制的大致流程。
不知道大家有没有一些疑惑呢?例如:
初始化广播器的时候为什么要判断是否已经存在广播器的bean?什么时候会走第一个分支?
protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
this.applicationEventMulticaster =
beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
if (logger.isTraceEnabled()) {
logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
}
else {
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
if (logger.isTraceEnabled()) {
logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
"[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
}
}
}
什么时候监听器会异步执行?什么时候出异常会被异常处理器处理?代码中都没有设置这些值!
@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
for (final ApplicationListener> listener : getApplicationListeners(event, type)) {
Executor executor = getTaskExecutor();
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
invokeListener(listener, event);
}
}
}
protected void invokeListener(ApplicationListener> listener, ApplicationEvent event) {
ErrorHandler errorHandler = getErrorHandler();
if (errorHandler != null) {
try {
doInvokeListener(listener, event);
}
catch (Throwable err) {
errorHandler.handleError(err);
}
}
else {
doInvokeListener(listener, event);
}
}
看以下代码你应该就会理解了!
@Configuration
@ComponentScan(basePackages = {"com.example.demo"})
public class MainConfig {
@Autowired
private Executor syncTaskExecutor;
@Bean("applicationEventMulticaster")
public AbstractApplicationEventMulticaster applicationEventMulticaster() {
SimpleApplicationEventMulticaster abstractApplicationEventMulticaster = new SimpleApplicationEventMulticaster();
abstractApplicationEventMulticaster.setTaskExecutor(syncTaskExecutor);
return abstractApplicationEventMulticaster;
}
}