简单使用(老司机直接跳过~)
定义一个实体类
@Data@AllArgsConstructor@NoArgsConstructorpublicclassUserInfo{
private Integer age;
private String name;
}
定义一个事件,需要继承ApplicationEvent
@Getter@SetterpublicclassCustomEventextendsApplicationEvent{
private UserInfo userInfo;
publicCustomEvent(UserInfo userInfo){
super(userInfo);
this.userInfo = userInfo;
}
}
定义一个监听器,需要实现ApplicationListener接口
@ComponentpublicclassCustomListenerimplementsApplicationListener<CustomEvent> {
@OverridepublicvoidonApplicationEvent(@NonNull CustomEvent event){
System.out.println("CustomListener接收到事件~");
System.out.println(JSONObject.toJSONString(event.getUserInfo()));
}
}
测试
@SpringBootTestpublicclassListenerTest{
@Resourceprivate ApplicationContext applicationContext;
@Testpublicvoidtest(){
UserInfo userInfo = new UserInfo(1, "1");
// 发布事件
applicationContext.publishEvent(new CustomEvent(userInfo));
}
}
可见,监听器成功监听并处理了我们发布的事件~
![](https://i-blog.csdnimg.cn/blog_migrate/878b6d774b79bfda7a4ef0c4b3faa8f2.png)
源码分析
通过上面的案例,我们了解到,发布事件是调用的publishEvent方法,所以我们直接从publishEvent开始看~
publishEvent
protectedvoidpublishEvent(Object event, @Nullable ResolvableType eventType){
Assert.notNull(event, "Event must not be null");
// 如果event是ApplicationEvent实例,则向上转型
ApplicationEvent applicationEvent;
if (event instanceof ApplicationEvent) {
applicationEvent = (ApplicationEvent) event;
}
else {
applicationEvent = new PayloadApplicationEvent<>(this, event);
if (eventType == null) {
eventType = ((PayloadApplicationEvent<?>) applicationEvent).getResolvableType();
}
}
// 早期发布的事件,此时listener还未注册,所以先将event存起来if (this.earlyApplicationEvents != null) {
this.earlyApplicationEvents.add(applicationEvent);
}
else {
// 获取事件发布器,并进行发布
getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
}
// 如果父context不为空,则要通过父context再发布一次if (this.parent != null) {
if (this.parent instanceof AbstractApplicationContext) {
((AbstractApplicationContext) this.parent).publishEvent(event, eventType);
}
else {
this.parent.publishEvent(event);
}
}
}
publishEvent方法,核心逻辑在于获取时间发布器进行发布,即下面这行代码
getApplicationEventMulticaster().multicastEvent(applicationEvent, eventType);
applicationEventMulticaster(事件多播器的获取和初始化)
getApplicationEventMulticaster只是简单的获取applicationEventMulticaster,并做一下参数校验,没什么好说的。
@Nullableprivate ApplicationEventMulticaster applicationEventMulticaster;
ApplicationEventMulticaster getApplicationEventMulticaster()throws IllegalStateException {
if (this.applicationEventMulticaster == null) {
thrownew IllegalStateException("ApplicationEventMulticaster not initialized - " +
"call 'refresh' before multicasting events via the context: " + this);
}
returnthis.applicationEventMulticaster;
}
重点在于,applicationEventMulticaster是怎么初始化的?
熟悉Spring源码的小伙伴们,对下面这张图肯定不会陌生~
没错,正是Spring初始化过程中非常重要的refresh方法 ,在refresh方法中,会调用initApplicationEventMulticaster方法初始化事件多播器
![](https://i-blog.csdnimg.cn/blog_migrate/1b0e905f97850dce162ba0a13f9c0a8d.png)
initApplicationEventMulticaster源码如下:
publicstaticfinal String APPLICATION_EVENT_MULTICASTER_BEAN_NAME = "applicationEventMulticaster";