栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

Spring-boot事件监听和源码分析

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring-boot事件监听和源码分析

springboot的事件监听:为bean之间的消息通信提供支持。当一个bean做完一件事以后,通知另一个bean知晓并做出相应处理。这时,我们需要另一个bean,监听当前bean所发生的事件。

实现步骤:四个步骤,五种方式

第一种方式
  1、自定义事件,一般是继承ApplicationEvent抽象类  2、定义事件监听器,一般是实现ApplicationListener接口  3、 1)把监听器加入到SpringApplication中:ApplicationListener.addListener();然后发布事件      2)或放置到spring容器:@Conponent
      3)或在application.properties配置文件中配置context.listener.classes=监听器全类名      4)或在meta-INF/spring.factories配置文件中配置org.springframework.context.ApplicationListener=监听器全类名      5)或编写一个类,纳入spring容器中,编写一个带参的方法,参数为事件的父类或需要监听的事件,加上@EventListener
  3、发布事件

第一步:自定义事件,继承ApplicationEvent抽象类

public class MyApplicationEvent extends ApplicationEvent {    public MyApplicationEvent(Object source) {        super(source);
    }
}

第二步:自定义事件监听器,实现ApplicationListener接口

public class MyApplicationListener implements ApplicationListener {    @Override
    public void onApplicationEvent(MyApplicationEvent myApplicationEvent) {
        System.out.println(myApplicationEvent.getClass().getName() + "被监听......");
    }
}

第三、四步:把监听器加入spring中,然后发布事件

@SpringBootApplicationpublic class BlogApplication {    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(BlogApplication.class, args);        //把监听器加入到SpringApplication中
        context.addApplicationListener(new MyApplicationListener());        //发布事件
        context.publishEvent(new MyApplicationEvent(new Object()));
        context.close();
    }
}

console result

第二种方式:换一种方式将监听器加入spring容器中(只需修改BlogApplication)

通过@component注解,将监听器加入spring容器中


BlogApplication.class

第三种方式:在application.properties配置文件中配置context.listener.classes=监听器全类名

application.properties

第四种方式:在meta-INF/spring.factories配置文件中配置org.springframework.context.ApplicationListener=监听器全类名第五种方式:编写一个类MyEventHandler,替代上文中的MyApplicationListener,将其纳入spring容器中,编写一个带参的方法,参数为需要监听的事件的父类或需要监听的事件,加上@EventListener。(参数的范围越广泛,监听到的事件越多)

MyEventHandler.class

实现原理

第三种方式:配置application.properties的方式的实现原理,
参照DelegatingApplicationListener.class的源码:

DelegatingApplicationListener.class


我们发现这个类中申明了一个常量PROPERTY_NAME = "context.listener.classes",接着往下看onApplicationEvent方法,源码作者首先从application.properties配置文件中读取到以"context.listener.classes"为key的Listener全类名,通过ClassLoader把他们实例化成Listener对象,继而存入一个List> listeners集合中,最后将他们排序后将他们挨个加入事件广播中。有兴趣的读者可以结合我的解析阅读以下源码!


实际工作如何使用

上文我们说到了如何定义一个事件和监听器,那在工作中,如果我们要使用事件为我们达到一些目的,难道需要实现这么麻烦的过程嘛?答案是no!spring和springboot已经给我们编写好了丰富的事件供我们使用。

spring提供的event


springboot提供的event

我们这里做一个简单的小范例,监听一个ContextClosedEvent事件。
编写监听器

@Componentpublic class ContextClosedEventHandler {    @EventListener
    public void listener(ContextClosedEvent event) {
        System.out.println("spring context stop...." + event.getClass().getName());
    }
}

编写BlogApplication

@SpringBootApplicationpublic class BlogApplication {    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(BlogApplication.class, args);
        context.close();
    }
}

console result

至此,springboot的五种事件监听方式就介绍完毕啦~



作者:八目朱勇铭
链接:https://www.jianshu.com/p/942f10493c47


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235449.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号