Spring注解开发——41-42、ApplicationListener事件监听器用法

 

41、扩展原理-ApplicationListener用法

ApplicationListener

  • 监听容器中发布的事件。事件驱动模型开发;
public interface ApplicationListener<E extends ApplicationEvent>
  • 监听 ApplicationEvent 及其下面的子事件;

步骤:

1)、写一个监听器(ApplicationListener实现类)来监听某个事件(ApplicationEvent及其子类)

  • @EventListener;
  • 原理:使用EventListenerMethodProcessor处理器来解析方法上的@EventListener;

2)、把监听器加入到容器;

3)、只要容器中有相关事件的发布,我们就能监听到这个事件;

  1. ContextRefreshedEvent:容器刷新完成(所有bean都完全创建)会发布这个事件;
  2. ContextClosedEvent:关闭容器会发布这个事件;

4)、发布一个事件:

applicationContext.publishEvent();

42、扩展原理-ApplicationListener原理

  • 有三个事件
  • ContextRefreshedEvent、Test_Ext$1[source=我发布了一个事件]、ContextClosedEvent
  1. ContextRefreshedEvent事件:
    1. 容器创建对象:refresh();
    2. finishRefresh();容器刷新完成会发布ContextRefreshedEvent事件
  2. 自己发布事件;
  3. 容器关闭会发布ContextClosedEvent;

【事件发布流程】:

publishEvent(new ContextRefreshedEvent(this));

1)、获取事件的多播器(派发器):getApplicationEventMulticaster()

2)、multicastEvent派发事件:

3)、获取到所有的ApplicationListener;

for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
  1. 如果有Executor,可以支持使用Executor进行异步派发;

    Executor executor = getTaskExecutor();
    
  2. 否则,同步的方式直接执行listener方法;invokeListener(listener, event);

    1. 拿到listener回调onApplicationEvent方法;

【事件多播器(派发器)】:

1)、容器创建对象:refresh();

2)、initApplicationEventMulticaster();初始化ApplicationEventMulticaster;

  1. 先去容器中找有没有id=“applicationEventMulticaster”的组件;

  2. 如果没有

    this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
    并且加入到容器中,我们就可以在其他组件要派发事件,自动注入这个applicationEventMulticaster;
    

【容器中有哪些监听器】:

1)、容器创建对象:refresh();

2)、registerListeners();

// 从容器中拿到所有的监听器,把他们注册到applicationEventMulticaster中;
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
// 将listener注册到ApplicationEventMulticaster中
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);

 

1 测试功能

写一个实现ApplicationListener接口的监听器和@EventListener注解的监听器来监听某个事件(ApplicationEvent及其子类)

 

package com.suirui.springanno.ext;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

@Component
public class MyApplicationListener implements ApplicationListener<ApplicationEvent> {
    public void onApplicationEvent(ApplicationEvent event) {
        System.out.println("收到的事件 " + event);
    }
}

package com.suirui.springanno.ext;



import com.suirui.springanno.ext.config.ExtConfig;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainTestExt {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ExtConfig.class);

        ac.publishEvent(new ApplicationEvent(new String("发布了一个事件")) {
        });
        ac.close();

    }
}

 

 

2 ApplicationListener源码分析

容器创建并且刷新

在这里插入图片描述

容器刷新完成会发布ContextRefreshedEvent事件

在这里插入图片描述

在这里插入图片描述

事件发布流程

在这里插入图片描述

在这里插入图片描述

自己发布事件,容器关闭会发布ContextClosedEvent(都是使用的publishEvent进行发布的)

在这里插入图片描述

在这里插入图片描述

多播器是怎么注册到容器中去的

1)刷新容器

在这里插入图片描述

2)初始化ApplicationEventMulticaster

在这里插入图片描述

在这里插入图片描述

容器是怎么将容器中的监听器注册到多播器中去的?

1)刷新容器

在这里插入图片描述

2)从容器中拿到所有的监听器,把他们注册到applicationEventMulticaster中

在这里插入图片描述

在这里插入图片描述

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值