文章目录

  • Spring事件概览
    • 事件
      • 自定义事件
    • 事件监听器
      • 基于接口
      • 基于注解
    • 事件广播器


Spring事件概览

Spring事件体系包括三个组件:事件,事件监听器,事件广播器

事件

Spring的内置事件中由系统内部进行发布,只需注入监听器

  • ContextRefreshedEvent

当容器被实例化或refreshed时发布.如调用refresh()方法, 此处的实例化是指所有的bean都已被加载,后置处理器都被激活,所有单例bean都已被实例化, 所有的容器对象都已准备好可使用. 如果容器支持热重载,则refresh可以被触发多次(XmlWebApplicatonContext支持热刷新,而 GenericApplicationContext则不支持)

  • ContextStartedEvent

当容器启动时发布,即调用start()方法, 已启用意味着所有的Lifecycle bean都已显式接收到了start 信号

  • ContextStoppedEvent

当容器停止时发布,即调用stop()方法, 即所有的Lifecycle bean都已显式接收到了stop信号 , 关闭的容器可以通过start()方法重启

  • ContextClosedEvent

当容器关闭时发布,即调用close方法, 关闭意味着所有的单例bean都已被销毁.关闭的容器不能被重启或refresh


23岁的程序猿大爷都说了,让整个小demo , 那来吧

先贴个配置类

package com.artisan.eventlistener2;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration
@ComponentScan("com.artisan.eventlistener2")
public class ArtisanConfig {}

自定义事件

事件类需要继承ApplicationEvent

package com.artisan.eventlistener2;import org.springframework.context.ApplicationEvent;public class ArtisanEvent  extends ApplicationEvent {private String msg ;public ArtisanEvent(Object source) {super(source);}public ArtisanEvent(Object source,String msg) {super(source);this.msg = msg ;}public void print(){System.out.println(msg);}}

作为测试,使用一个简单的pojo

  1. 自定义事件需要继承ApplicationEvent
  2. 因为ApplicationEvent extends EventObject ,所以子类的构造方法需要调用super()

事件监听器

下面演示两种方式

基于接口

package com.artisan.eventlistener2;import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;@Component
public class ArtisanListener implements ApplicationListener<ArtisanEvent> {@Overridepublic void onApplicationEvent(ArtisanEvent event) {System.out.println("实现ApplicationListener  监听到ArtisanEvent.....");event.print();}
}
  1. 事件监听器需要实现ApplicationListener接口,泛型接口,泛型类类型就是事件类型
  2. 其次需要是spring容器托管的bean,所以这里加了@component,重写onApplicationEvent方法

基于注解

package com.artisan.eventlistener2;import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;@Component
public class ArtisanListenerByAnno   {@EventListener(ArtisanEvent.class)public void onApplicationEvent(ArtisanEvent event) {System.out.println("EventListener  监听到ArtisanEvent.....");event.print();}
}
  1. 方法上需要标注 @EventListener(ArtisanEvent.class),方法名任意
  2. 其次需要是spring容器托管的bean,所以这里加了@component

@EventListener,修饰在方法上,是不是比基于实现类的方式要好,不用一个事件一个类了,确实如此。


事件广播器

package com.artisan.eventlistener2;import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class ArtisanTest {public static void main(String[] args) {AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ArtisanConfig.class);ac.publishEvent(new ArtisanEvent("xxxx","msg from artisanEvent"));}
}

核心就是ac.publishEvent

ac.publishEvent(new ArtisanEvent("xxxx","msg from artisanEvent"));

【测试结果】

是不是发现,这不就是【观察者模式】吗? ----------确实确实,这就是观察者模式的典型应用,那spring是怎么实现的呢?

下篇我们继续分析Spring的源码实现


Spring5源码 - 10 Spring事件监听机制_应用篇相关推荐

  1. Spring5源码 - 13 Spring事件监听机制_@EventListener源码解析

    文章目录 Pre 概览 开天辟地的时候初始化的处理器 @EventListener EventListenerMethodProcessor afterSingletonsInstantiated 小 ...

  2. Spring5源码 - 11 Spring事件监听机制_源码篇

    文章目录 pre 事件监听机制的实现原理[观察者模式] 事件 ApplicationEvent 事件监听者 ApplicationEvent 事件发布者 ApplicationEventMultica ...

  3. Spring5源码 - 12 Spring事件监听机制_异步事件监听应用及源码解析

    文章目录 Pre 实现原理 应用 配置类 Event事件 事件监听 EventListener 发布事件 publishEvent 源码解析 (反推) Spring默认的事件广播器 SimpleApp ...

  4. spring 扫描所有_自定义Spring事件监听机制

    开头提醒一下大家: 尽管我简化了Spring源码搞了个精简版的Spring事件机制,但是没接触过Spring源码的朋友阅读起来还是有很大难度,请复制代码到本地,边Debug边看 既然要简化代码,所以不 ...

  5. Springboot事件监听机制:工作原理

    目录 前言 1.观察者模式 1.1观察者模式的核心元素 1.2观察者模式的工作流程 2.springboot事件监听机制的基本工作原理 2.1事件发布器是什么时候在哪里产生的呢? 2.2事件监听器是什 ...

  6. spring 事件监听

    用一个简单的例子来实现spring事件监听的功能 这个例子主要功能是,记录那些用户是第一次登入系统,如果用户是第一次登入系统,则调用spring的事件监听,记录这些用户. 主要用到的spring的类和 ...

  7. Spring5源码 - 07 Spring Bean 生命周期流程 源码解读02

    文章目录 Pre 通俗流程 finishBeanFactoryInitialization Pre Spring5源码 - 06 Spring Bean 生命周期流程 概述 01 接上文 通俗流程 下 ...

  8. spring中的事件监听机制

    Spring event listener 介绍 example 简单原理解释 自定义事件.监听和发布 事件 监听器 发布者 测试 更加一般的事件 @EventListener原理 介绍 exampl ...

  9. Spring容器的事件监听机制(简单明了的介绍)

    文章目录 前言 事件 1. 定义事件 2. 定义监听器 3. 定义发布器 Spring容器的事件监听机制 1.事件的继承类图 监听器的继承类图 总结 前言 上一篇我们介绍了SpringFactorie ...

最新文章

  1. Nginx之负载均衡(四)
  2. iOS开发中显示实时的FPS值
  3. unbalanced enable irq 问题的解决 以及共享的gpio中断引起的问题
  4. Python执行系统命令的四种方法
  5. CORS 跨域-同源策略
  6. javaee编程题_在JavaEE中使用CDI的简单面向方面的编程(AOP)
  7. wampserver php扩展openssl 不可用_PHP基础及WAMP集成基础
  8. 测试基础-05-bug的定义生命周期
  9. java io类filereader,39. Java IO: FileReader
  10. 思特威电子通过注册:拟募资28亿 小米红杉联想是股东
  11. 基础晶体管放大电路设计七步走
  12. 用PyTorch完成手写数字识别
  13. 河南城建学院计算机学院排名,河南城建学院2019年度“读书之星”评选结果公示...
  14. 面试乐融集团Python开发工程师助理有感
  15. VASP+Phono3py计算声子linewidth
  16. EL$JSTL简化jsp开发中的代码量
  17. 【Kafka】Kafka消费者相关策略
  18. 静态工具类使用单例对象线程安全问题注意1
  19. SCU - 4438——Censor(哈希)
  20. Couldn't resolve error at 'ntdll!NtOpenProcess'

热门文章

  1. sql item_map
  2. opencv 入门 demo
  3. Leetcode 989. 数组形式的整数加法 (每日一题 20210826 同类型题)
  4. MATLAB应用实战系列(七十七)-【图像处理】COVID-19 防疫应用口罩检测
  5. R语言应用实战系列(一)-基于R对QQ群聊天记录数据分析
  6. tableau2020.2版本可视化数据分析 新功能介绍
  7. 肤色检测算法 - 基于二次多项式混合模型的肤色检测
  8. Project interpreter not specified(eclipse+pydev)
  9. sts引入lombok_Spring Boot中lombok的安装与使用详解
  10. 服务器可以ghost备份吗_Ghost超详细图文教程 – 克隆备份系统全程图解(下部)...