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

傷城~ 2022-11-21 03:45 285阅读 0赞

文章目录

  • 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 , 那来吧

在这里插入图片描述

先贴个配置类

  1. package com.artisan.eventlistener2;
  2. import org.springframework.context.annotation.ComponentScan;
  3. import org.springframework.context.annotation.Configuration;
  4. @Configuration
  5. @ComponentScan("com.artisan.eventlistener2")
  6. public class ArtisanConfig {
  7. }

自定义事件

事件类需要继承ApplicationEvent

  1. package com.artisan.eventlistener2;
  2. import org.springframework.context.ApplicationEvent;
  3. public class ArtisanEvent extends ApplicationEvent {
  4. private String msg ;
  5. public ArtisanEvent(Object source) {
  6. super(source);
  7. }
  8. public ArtisanEvent(Object source,String msg) {
  9. super(source);
  10. this.msg = msg ;
  11. }
  12. public void print(){
  13. System.out.println(msg);
  14. }
  15. }

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

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

事件监听器

下面演示两种方式

基于接口

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

基于注解

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

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


事件广播器

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

核心就是ac.publishEvent

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

【测试结果】

在这里插入图片描述

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

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


发表评论

表情:
评论列表 (有 0 条评论,285人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring事件监听机制

    前言 Spring中的事件机制其实就是设计模式中的观察者模式,主要由以下角色构成: 1. 事件 2. 事件监听器(监听并处理事件) 3. 事件发布者(发布事件...