ApplicationListener源码解读(异步通知、统一ErrorHandler)

简单使用(老司机直接跳过~)

定义一个实体类

@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));
  }
​
}

可见,监听器成功监听并处理了我们发布的事件~


源码分析

通过上面的案例,我们了解到,发布事件是调用的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方法初始化事件多播器

initApplicationEventMulticaster源码如下:

publicstaticfinal String APPLICATION_EVENT_MULTICASTER_BEAN_NAME = "applicationEventMulticaster";
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值