SpringBoot框架(6)--事件监听
一、场景:类与类之间的消息通信,例如创建一个对象前后做拦截,日志等等相应的事件处理。
二、事件监听步骤
(1)自定义事件继承ApplicationEvent抽象类
(2)自定义事件监听器,一般实现ApplicationListener接口,传入自定义事件类
(3)配置监听器,启动时,需要把监听器加入到spring容器中(见实现方式)
(4)事件发布
三、实现方式
1、手动方式 --> 调用SpringBoot的addListeners方法把事件监听类添加的SpringBoot容器中
2、注解方式--> 事件监听类添加@Component
3、配置方式--> 在properties文件中配置,context.listener.classes=监听类全路径
4、事件处理类方式-->其实也是一种注解的方式,只是定义了专门的事件处理类+注解@EventListener(MyApplicationEvent.class) --> 该方法还可以指定监听的事件(推荐)
注:自定义事件无论采取哪种实现方式都需要通过SpringBoot容器的publishEvent方法类发布事件。
四、代码
1、建立Spring Initializr项目:boot-event
代码结构

首先实验第一种方式:手动方式
第一步 自定义事件类 MyApplicationEvent extends ApplicationEvent
package com.boot.event.bootevent;
import org.springframework.context.ApplicationEvent;
public class MyApplicationEvent extends ApplicationEvent {
public MyApplicationEvent(Object source) {
super(source);
}
}
MyApplicationEvent.java
第二步 自定义事件监听类
package com.boot.event.bootevent;
import org.springframework.context.ApplicationListener;
public class MyApplicationEventListener implements ApplicationListener<MyApplicationEvent> {
@Override
public void onApplicationEvent(MyApplicationEvent event) {
System.out.println("接收到了事件:" + event.getClass());
}
}
MyApplicationEventListener.java
第三、四步 添加事件监听到spring容器 并发布自定义事件
package com.boot.event.bootevent; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext; @SpringBootApplication
public class BootEventApplication { public static void main(String[] args) {
SpringApplication app = new SpringApplication(BootEventApplication.class); //第一种方式:手动添加监听事
app.addListeners(new MyApplicationEventListener());
ConfigurableApplicationContext contex = app.run(args); //发布事件
contex.publishEvent(new MyApplicationEvent(new Object()));
contex.close();
}
}
BootEventApplication.java
打印结果:
接收到了事件:class com.boot.event.bootevent.MyApplicationEvent
以上手动调用addListeners把事件监听添加到Sring容器中管理。
第二种方式:注解方式 --> 事件监听类添加@Component
在第一种方式的基础上,在MyApplicationEventListener类添加注解@Component,如下
@Component
public class MyApplicationEventListener implements ApplicationListener<MyApplicationEvent> {
@Override
public void onApplicationEvent(MyApplicationEvent event) {
System.out.println("接收到了事件:" + event.getClass());
}
}
把BootEventApplication.java中main方法的addListeners注释掉
//第一种方式:手动添加监听事
//app.addListeners(new MyApplicationEventListener());
运行项目,打印结果
接收到了事件:class com.boot.event.bootevent.MyApplicationEvent
可以看到自定义的事件同样被监听到。
第三种方式:配置方式
在第二种方法的基础上继续第三种方法,首先在application.properties配置文件中添加以下配置信息
context.listener.classes=com.boot.event.bootevent.MyApplicationEventListener
同时把MyApplicationEventListener类的注解@Component注释掉,如下所示
//第二种方式:添加注解
//@Component
public class MyApplicationEventListener implements ApplicationListener<MyApplicationEvent> {
//......
}
打印结果
接收到了事件:class com.boot.event.bootevent.MyApplicationEvent
说明配置的方法也成功让自定义的事件监听起作用
第四中方式:事件处理类方式 -->定义专门的事件处理类HandleEnvent.java(推荐)
package com.boot.event.bootevent; import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component; @Component
public class HandleEnvent {
@EventListener(MyApplicationEvent.class)
public void handleEvent(MyApplicationEvent event) {
System.out.println("接收到了事件:" + event.getClass());
System.out.println("接收到了事件:" + event.getSource());
} @EventListener(ContextClosedEvent.class)
public void handleEvent(Object event) {
System.out.println("接收到了事件:" + event.getClass());
}
}
HandleEnvent.java
把第三中方法中的配置信息注释掉,其他不变
#context.listener.classes=com.boot.event.bootevent.MyApplicationEventListener
打印结果
接收到了事件:class com.boot.event.bootevent.MyApplicationEvent
接收到了事件:java.lang.Object@1c4ee95c
接收到了事件:class org.springframework.context.event.ContextClosedEvent
SpringBoot框架(6)--事件监听的更多相关文章
- SpringBoot入门之事件监听
spring boot在启动过程中增加事件监听机制,为用户功能拓展提供极大的便利,sptingboot支持的事件类型有以下五种: ApplicationStartingEvent Applicatio ...
- SpringBoot事件监听机制及观察者模式/发布订阅模式
目录 本篇要点 什么是观察者模式? 发布订阅模式是什么? Spring事件监听机制概述 SpringBoot事件监听 定义注册事件 注解方式 @EventListener定义监听器 实现Applica ...
- java Gui编程 事件监听机制
1. GUI编程引言 以前的学习当中,我们都使用的是命令交互方式: 例如:在DOS命令行中通过javac java命令启动程序. 软件的交互的方式: 1. 命令交互方式 图书管理系统 ...
- SpringBoot事件监听机制源码分析(上) SpringBoot源码(九)
SpringBoot中文注释项目Github地址: https://github.com/yuanmabiji/spring-boot-2.1.0.RELEASE 本篇接 SpringApplicat ...
- springboot 中事件监听模型的一种实现
目录 定义事件本身 定义事件源 定义监听者 一.需要实现 ApplicationListener 二.使用 @EventListener 注解 测试 项目结构 前言: 事件监听模型是一种常用的设计模式 ...
- SpringBoot Application事件监听
SpringBoot Application共支持6种事件监听,按顺序分别是: ApplicationStartingEvent:在Spring最开始启动的时候触发 ApplicationEnviro ...
- springBoot高级:自动配置分析,事件监听,启动流程分析,监控,部署
知识点梳理 课堂讲义 02-SpringBoot自动配置-@Conditional使用 Condition是Spring4.0后引入的条件化配置接口,通过实现Condition接口可以完成有条件的加载 ...
- SpringBoot的事件监听
事件监听的流程分为三步:1.自定义事件,一般是继承ApplicationEvent抽象类.2.定义事件监听器,一般是实现ApplicationListener接口.3.a.启动的时候,需要将监听器加入 ...
- Javascript事件模型系列(三)jQuery中的事件监听方式及异同点
作为全球最知名的js框架之一,jQuery的火热程度堪称无与伦比,简单易学的API再加丰富的插件,几乎是每个前端程序员的必修课.从读<锋利的jQuery>开始,到现在使用jQuery有一年 ...
随机推荐
- Java Map集合 遍历 五种方式(包含 Lambda 表达式遍历)
示例代码如下: package com.miracle.luna.lambda; import java.util.HashMap; import java.util.Iterator; import ...
- 源码安装zabbix4.0.1
本篇是纯粹安装zabbix4.0.1, 前提需要安装lnmp请安装 https://www.cnblogs.com/effortsing/p/9982028.html 环境准备: 一台虚拟机 192. ...
- java:struts框架3(自定义拦截器,token令牌,文件上传和下载(单/多))
1.自定义拦截器: struts.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE ...
- Junit4 简单使用
一.环境搭建 对于习惯使用Eclipse开发平台来说,Junit早已是非常通常的插件,在Eclipse开发平台中,可以非常方便地搭建Junit测试环境. 1.在Eclipse上创建工程,任何Java工 ...
- 【Linux 网络编程】MTU(Maximum Transmission Uint)
(1)以太网和IEEE802.3对数据帧的长度都是有限制的,其最大分别是1500和1492字节,成为MTU. (2)如果IP层有一个数据要传输,而且数据的长度比链路层的MTU要大,那么IP层就要进行分 ...
- 【转帖】联芸Maxio展示国产PCIe SSD主控:速度可达3.5GB/s
联芸Maxio展示国产PCIe SSD主控:速度可达3.5GB/s https://www.cnbeta.com/articles/tech/855223.htm 国产主控 紫光做国产颗粒 国产器件对 ...
- springcloud用法
springcloud用法 使用springcloud搭建微服务肯定要在父工程下面编写子工程 一.搭建eureka注册中心 1. 创建maven项目(在springboot项目下建立子工程eur ...
- shiro三连斩之概念
1, 什么是Shiro? Shiro是一个安全框架,用于解决系统的认证和授权问题,同时提供了会话管理,数据加密,与WEB集成,缓存等机制. Authentication:身份认证/登录,验证用户是不是 ...
- next_permutation() 全排列函数
next_permutation() 全排列函数 这个函数是STL自带的,用来求出该数组的下一个排列组合 相当之好用,懒人专用 适用于不想自己用dfs写全排列的同学(结尾附上dfs代码) 洛谷oj可去 ...
- Python:什么是进阶,如何进阶?
目录 Python:什么是进阶,如何进阶? 1. 什么是进阶? 2. 如何进阶? 3. 除此之外呢? Python:什么是进阶,如何进阶? 1. 什么是进阶? 一门编程语言的基础部分,往往非常简单.如 ...