事件传播器
在分析Spring的事件传播器之前,还是先来看一下Spring的事件监听的简单用法.
1、定义监听器事件
public class TestEvent extends ApplicationEvent {
public String msg;
public TestEvent(Object source) {
super(source);
}
public TestEvent(Object source, String msg) {
super(source);
this.msg = msg;
}
public void print(){
System.out.println(msg);
}
}
2、定义监听器
public class TestListener implements ApplicationListener {
@Override
public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof TestEvent){
TestEvent testEvent = (TestEvent) event;
testEvent.print();
}
}
}
3、配置文件
<bean id="testListener" class="com.ipluto.demo.listener.TestListener"/>
4、测试
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
TestEvent testEvent = new TestEvent("hello","msg");
applicationContext.publishEvent(testEvent);
Spring 会把发出的TestEvent事件转给我们自定义的TestListener进行进一步的处理.其实这里用的设计模式就是观察者模式.
下面就来看看ApplicationEventMulticaster是如何被初始化的,以确保功能的正确运行.
initApplicationEventMulticaster的方式比较简单:
- 如果用户自定义了事件广播器,那么使用用户自定义的事件广播器
- 如果用户没有自定义,使用默认的ApplicationEventMulticaster.
protected void initApplicationEventMulticaster() {
ConfigurableListableBeanFactory beanFactory = this.getBeanFactory();
if (beanFactory.containsLocalBean("applicationEventMulticaster")) {
this.applicationEventMulticaster = (ApplicationEventMulticaster)beanFactory.getBean("applicationEventMulticaster", ApplicationEventMulticaster.class);
if (this.logger.isTraceEnabled()) {
this.logger.trace("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
} else {
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton("applicationEventMulticaster", this.applicationEventMulticaster);
if (this.logger.isTraceEnabled()) {
this.logger.trace("No 'applicationEventMulticaster' bean, using [" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
}
}
}
作为广播器,一定是用于存放监听器并在合适的时候调用监听器.
分析一下SimpleApplicationEventMulticaster来一探究竟.
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = eventType != null ? eventType : this.resolveDefaultEventType(event);
Executor executor = this.getTaskExecutor();
// 获取事件对应的监听器
Iterator var5 = this.getApplicationListeners(event, type).iterator();
while(var5.hasNext()) {
ApplicationListener<?> listener = (ApplicationListener)var5.next();
if (executor != null) {
executor.execute(() -> {
// 执行监听器的方法,最终会调用listener的onApplicationEvent方法
this.invokeListener(listener, event);
});
} else {
this.invokeListener(listener, event);
}
}
}
注册监听器
前面注册事件广播器的时候,就已经介绍了监听器,那么监听器是怎么注册的呢?
protected void registerListeners() {
// 硬编码方式注册的监听器处理
Iterator var1 = this.getApplicationListeners().iterator();
while(var1.hasNext()) {
ApplicationListener<?> listener = (ApplicationListener)var1.next();
this.getApplicationEventMulticaster().addApplicationListener(listener);
}
// 配置文件注册的监听器处理
String[] listenerBeanNames = this.getBeanNamesForType(ApplicationListener.class, true, false);
String[] var7 = listenerBeanNames;
int var3 = listenerBeanNames.length;
for(int var4 = 0; var4 < var3; ++var4) {
String listenerBeanName = var7[var4];
this.getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (!CollectionUtils.isEmpty(earlyEventsToProcess)) {
Iterator var9 = earlyEventsToProcess.iterator();
while(var9.hasNext()) {
ApplicationEvent earlyEvent = (ApplicationEvent)var9.next();
this.getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}