SpringBoot事件机制
1、是什么?
SpringBoot事件机制是指SpringBoot中的开发人员可以通过编写自定义事件来对应用程序进行事件处理。我们可以创建自己的事件类,并在应用程序中注册这些事件,当事件被触发时,可以对其进行处理。在SpringBoot中,事件可以是任意类型的,可以是基于Spring的事件,也可以是自定义的事件。事件处理可以是在应用程序中特定的方法中进行,也可以是通过Spring提供的事件处理API进行处理。
主要有三大对象:
- 1、事件源 :具体的事件内容
- 2、事件发布者 :发布事件
- 3、事件监听者 :监听事件


9大事件触发顺序&时机
1、ApplicationStartingEvent:应用启动但未做任何事情, 除过注册listeners and initializers.
2、ApplicationEnvironmentPreparedEvent: Environment 准备好,但context 未创建.
3、ApplicationContextInitializedEvent: ApplicationContext 准备好,ApplicationContextInitializers 调用,但是任何bean未加载
4、ApplicationPreparedEvent: 容器刷新之前,bean定义信息加载
5、ApplicationStartedEvent: 容器刷新完成, runner未调用
=以下就开始插入了探针机制====
6、AvailabilityChangeEvent: LivenessState.CORRECT应用存活; 存活探针
7、ApplicationReadyEvent: 任何runner被调用
8、AvailabilityChangeEvent:ReadinessState.ACCEPTING_TRAFFIC就绪探针,可以接请求
9、ApplicationFailedEvent :启动出错
2、怎么玩?
这样把,我们先模拟一个场景:现在有一个用户打卡的需求,用户登录进行签到打卡,打卡会奖励一些积分;当连续打卡次数到了7次我们就送一张优惠券。当然除了前面的操作我们还需要记录一下用户的登录日志
明确一下:
- 1、加积分
- 2、送优惠券
- 3、记录登录日志
常规写法如下:
@Autowired
private AccountService accountService;
@Autowired
private CouponService couponService;
@Autowired
private SystemService systemService;
@GetMapping("/login")
public String login(@RequestParam String username,
@RequestParam String password) {
// 伪代码
// login......
// 加积分
accountService.addAccountScore(username);
// 送优惠券
couponService.addCoupon(username);
// 记录登录日志
systemService.addLoginLog(username);
// 等等....
return "success";
}
问题:上面的代码实现功能是没问题的,但是如果我还需要做其他的操作岂不是每次都得修改代码加功能了...,这不是我们想要的,也不符合【开闭原则】;下面我们就用时间解耦上面的代码
(1) 定义一个事件发布器
package com.ly.event;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
/**
* 事件发布器
*
* @author ly (个人博客:https://www.cnblogs.com/ybbit)
* @date 2023-06-26 20:06
* @tags 喜欢就去努力的争取
*/
@Service
public class EventPublisher implements ApplicationEventPublisherAware {
ApplicationEventPublisher applicationEventPublisher;
/**
* 发送所有事件
*
* @param event
*/
public void sendEvent(ApplicationEvent event) {
// 真正的发送事件
this.applicationEventPublisher.publishEvent(event);
}
/**
* 会被自动调用,把真正发事件的底层组件给我们注入进来
*
* @param applicationEventPublisher
*/
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}
}
(2) 定义一个事件
package com.ly.event;
import com.ly.entity.UserEventEntity;
import org.springframework.context.ApplicationEvent;
/**
* 事件
*
* @author ly (个人博客:https://www.cnblogs.com/ybbit)
* @date 2023-06-26 20:06
* @tags 喜欢就去努力的争取
*/
public class LoginSuccessEvent extends ApplicationEvent {
public LoginSuccessEvent(UserEventEntity source) {
super(source);
}
}
(3) 定义一个事件对象
package com.ly.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @author ly (个人博客:https://www.cnblogs.com/ybbit)
* @date 2023-06-26 20:05
* @tags 喜欢就去努力的争取
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class UserEventEntity {
private String username;
private String password;
}
(4) 编写相关逻辑(伪代码)
AccountService
package com.ly.service;
import com.ly.entity.UserEventEntity;
import com.ly.event.LoginSuccessEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Service;
/**
* 记录积分
*
* @author ly (个人博客:https://www.cnblogs.com/ybbit)
* @date 2023-06-26 20:23
* @tags 喜欢就去努力的争取
*/
@Service
public class AccountService implements ApplicationListener<LoginSuccessEvent> {
public void addAccountScore(String username) {
System.out.println(username + "增加积分");
}
@Override
public void onApplicationEvent(LoginSuccessEvent event) {
UserEventEntity userEventEntity = (UserEventEntity) event.getSource();
System.out.println("============AccountService=============onApplicationEvent");
addAccountScore(userEventEntity.getUsername());
}
}
CouponService
package com.ly.service;
import com.ly.entity.UserEventEntity;
import com.ly.event.LoginSuccessEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
/**
* 优惠券
*
* @author ly (个人博客:https://www.cnblogs.com/ybbit)
* @date 2023-06-26 20:23
* @tags 喜欢就去努力的争取
*/
@Service
public class CouponService {
@EventListener
public void onEnvet(LoginSuccessEvent event) {
// TODO 判断逻辑......
UserEventEntity userEventEntity = (UserEventEntity) event.getSource();
System.out.println("=========CouponService========= onEnvet");
addCoupon(userEventEntity.getUsername());
}
public void addCoupon(String username) {
System.out.println(username + "发送优惠券");
}
}
SystemService
package com.ly.service;
import com.ly.entity.UserEventEntity;
import com.ly.event.LoginSuccessEvent;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
/**
* 登录日志
*
* @author ly (个人博客:https://www.cnblogs.com/ybbit)
* @date 2023-06-26 20:23
* @tags 喜欢就去努力的争取
*/
@Service
public class SystemService {
@Order(1)
@EventListener
public void onEvent(LoginSuccessEvent event) {
System.out.println("==========SystemService========= onEvent");
UserEventEntity userEventEntity = (UserEventEntity) event.getSource();
addLoginLog(userEventEntity.getUsername());
}
public void addLoginLog(String username) {
System.out.println(username + "记录登录日志");
}
}
(5) 修改旧的登录处理逻辑
@Autowired
private EventPublisher eventPublisher;
@Autowired
private AccountService accountService;
@Autowired
private CouponService couponService;
@Autowired
private SystemService systemService;
@GetMapping("/login")
public String login(@RequestParam String username,
@RequestParam String password) {
/* // 伪代码
// login......
// 加积分
accountService.addAccountScore(username);
// 送优惠券
couponService.addCoupon(username);
// 记录登录日志
systemService.addLoginLog(username);
// 等等....*/
// 我们发送登录事件就好了
eventPublisher.sendEvent(new LoginSuccessEvent(new UserEventEntity(username, password)));
return "success";
}

3、事件监听的几种方式
1、在应用上下文直接添加

2、使用spring.factories文件

3、使用@EventListener注解

4、自定义监听器实现ApplicationListener接口并交给Spring管理

事件发布:ApplicationEventPublisherAware 或注入:ApplicationEventMulticaster
事件监听:组件 + @EventListener
SpringBoot事件机制的更多相关文章
- [译]谈谈SpringBoot 事件机制
要"监听"事件,我们总是可以将"监听器"作为事件源中的另一个方法写入事件,但这将使事件源与监听器的逻辑紧密耦合. 对于实际事件,我们比直接方法调用更灵活.我们可 ...
- springBoot的事件机制---GenericApplicationListener用法
springBoot的事件机制---GenericApplicationListener用法 什么是ApplicationContext? 它是Spring的核心,Context我们通常解释为上下文环 ...
- SpringBoot事件监听机制及观察者模式/发布订阅模式
目录 本篇要点 什么是观察者模式? 发布订阅模式是什么? Spring事件监听机制概述 SpringBoot事件监听 定义注册事件 注解方式 @EventListener定义监听器 实现Applica ...
- ApplicationEvent事件机制源码分析
<spring扩展点之三:Spring 的监听事件 ApplicationListener 和 ApplicationEvent 用法,在spring启动后做些事情> <服务网关zu ...
- SpringBoot学习(二)探究Springboot启动机制
引言: SpringBoot为我们做的自动配置,确实方便快捷,但是对于新手来说,如果不大懂SpringBoot内部启动原理,以后难免会吃亏.所以这次博主就跟你们一起探究一下SpringBoot的启动原 ...
- 搞清楚Spring事件机制后:Spring的源码看起来简单多了
本文主讲Spring的事件机制,意图说清楚: 什么是观察者模式? 自己实现事件驱动编程,对标Spring的事件机制 彻底搞懂Spring中的事件机制,从而让大家 本文内容较长,代码干货较多,建议收藏后 ...
- 观察者模式之spring事件机制
ddsspring中的事件机制使用到设计模式中的观察者模式 ,观察者模式有两个概念,1.观察者.被观察者.2.被观察者做出相应得动作,观察者能接收到.不分析设计模式,学习下spring中的事件机制实际 ...
- 【移动端兼容问题研究】javascript事件机制详解(涉及移动兼容)
前言 这篇博客有点长,如果你是高手请您读一读,能对其中的一些误点提出来,以免我误人子弟,并且帮助我提高 如果你是javascript菜鸟,建议您好好读一读,真的理解下来会有不一样的收获 在下才疏学浅, ...
- tkinter事件机制
一.tkinter.Event tkinter的事件机制跟js是一样的,也是只有一个Event类,这个类包罗万象,集成了键盘事件,鼠标事件,包含各种参数. 不像java swing那种强类型事件,sw ...
- [解惑]JavaScript事件机制
群里童鞋问到关于事件传播的一个问题:“事件捕获的时候,阻止冒泡,事件到达目标之后,还会冒泡吗?”. 初学 JS 的童鞋经常会有诸多疑问,我在很多 QQ 群也混了好几年了,耳濡目染也也收获了不少,以后会 ...
随机推荐
- 《SQL与数据库基础》23. 读写分离
目录 读写分离 一主一从 准备 配置 双主双从 准备 配置 主库配置 从库配置 从库关联主库 主库相互复制 双主双从读写分离 本文以 MySQL 为例.以 MyCat 数据库中间件为例,通过 MyCa ...
- springboot项目自动关闭进程重启脚本
话不多说,先上脚本 kill -15 $(netstat -nlp | grep :9095 | awk '{print $7}' | awk -F"/" '{ print $1 ...
- 教育法学期末考试02MOOC
期末考试 返回 期末考试试卷为客观题,总分为100分,占课程成绩的40%.其中包含16道单选题,2道多选题.共18道题.单选题每道5分,多选题每道10分,限时90分钟完成. 倒计时: 01:21: ...
- 栈和堆的区别、FreeRTOS 中的任务栈
栈和堆的区别.FreeRTOS 中的任务栈 01 堆和栈的概念 堆 功能 堆是一块用于动态分配内存的区域,用于存储程序运行时动态创建的对象.堆的大小可以在程序运行时动态调整. 特点 堆的分配和释放是由 ...
- 深入了解 GPU 互联技术——NVLINK
随着人工智能和图形处理需求的不断增长,多 GPU 并行计算已成为一种趋势.对于多 GPU 系统而言,一个关键的挑战是如何实现 GPU 之间的高速数据传输和协同工作.然而,传统的 PCIe 总线由于带宽 ...
- CF1746F Kazaee
prologue 数组范围一定要看好了开,不然容易我一样,调试调了一页多. 还有就是不要傻乎乎地只跑一次和哈希,因为和哈希(从下面地佬的题解中才知道)它其实算作是一种 trick(类比SA(Stimu ...
- client-go实战之七:准备一个工程管理后续实战的代码
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是<client-go实战> ...
- 谱图论:Laplacian算子及其谱性质
1 Laplacian 算子 给定无向图\(G=(V, E)\),我们在上一篇博客<谱图论:Laplacian二次型和Markov转移算子>中介绍了其对应的Laplacian二次型: \[ ...
- ansible 命令行模
ansible 命令行模 ansible命令格式 命令格式:ansible <组名> -m <模块> -a <参数列表> 查看已安装的模块 ansible-doc ...
- c#中原型模式详解
基础介绍: 具体可分为2个角色: Prototype(原型类):声明一个Clone自身的接口: ConcretePrototype(具体原型类):,实现一个Clone自身的操作. ...