Spring 事件通知
Spring事件通知
1.编写自定义的事件载体对象
- /**
- * @Author:qmfang
- * @Description: 继承自事件对象,表明将其作为一个事件通知对象
- * @Date:Created in 10:35 2018/4/8
- * @Modified By:
- */
- @Data
- public class BlackListEvent extends ApplicationEvent {
- private final String address;
- private final String test;
- public BlackListEvent(Object source, String address, String test) {
- super(source);
- this.address = address;
- this.test = test;
- }
- }
2.编写事件触发对象
- /**
- * @Author:qmfang
- * @Description: 实现以aware结尾的接口之后在bean被实例化之后会Spring会注入相应的资源
- * @Date:Created in 10:37 2018/4/8
- * @Modified By:
- */
- public class EmailService implements ApplicationEventPublisherAware {
- private List<String> blackList;
- private ApplicationEventPublisher publisher;
- public void setBlackList(List<String> blackList) {
- this.blackList = blackList;
- }
- @Override
- public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
- /**
- * 此处也就是注册进入应用事件发布对象
- */
- this.publisher = applicationEventPublisher;
- }
- public void sendEmail(String address, String test) {
- if (blackList.contains(address)) {
- /**
- * 如果是包含的地址将发布该消息
- */
- BlackListEvent event = new BlackListEvent(this, address, test);
- publisher.publishEvent(event);
- return;
- }
- }
- }
3.编写相应的事件监听对象
- /**
- * @Author:qmfang
- * @Description: 实现ApplicationListener的接口将监听相应的事件,当有事件时,将触发onApplicationEvent方法
- * @Date:Created in 10:44 2018/4/8
- * @Modified By:
- */
- public class BlackListNotifier implements ApplicationListener<BlackListEvent> {
- @Override
- public void onApplicationEvent(BlackListEvent event) {
- System.out.println("应用事件:" + event);
- }
- }
4.将相应的对象注入到容器
- /**
- * @Author:qmfang
- * @Description:
- * @Date:Created in 9:58 2018/4/8
- * @Modified By:
- */
- @Configuration
- public class MvcConfig {
- @Bean
- public EmailService emailService() {
- EmailService emailService = new EmailService();
- List list = Arrays.asList("known.spammer@example.org", "known.hacker@example.org", "john.deo@example.org");
- emailService.setBlackList(list);
- return emailService;
- }
- @Bean
- public BlackListNotifier blackListNotifier() {
- BlackListNotifier blackListNotifier = new BlackListNotifier();
- return blackListNotifier;
- }
- }
4.编写测试
- @RestController
- public class TestController {
- @Autowired
- private EmailService emailService;
- /**
- * 127.0.0.1:8886/test
- *
- * @return
- */
- @GetMapping("/test")
- public Object get() {
- emailService.sendEmail("known.spammer@example.org", "test@test");
- return "hello";
- }
- }
基于注解的事件处理
基于注解的可以将BlackListNotifier修改为如下形式
- /**
- * @Author:qmfang
- * @Description: 实现ApplicationListener的接口将监听相应的事件,当有事件时,将触发onApplicationEvent方法
- * @Date:Created in 10:44 2018/4/8
- * @Modified By:
- */
- public class BlackListNotifier {
- /**
- * 也可以基于注解的形式 参数表名了需要监听的事件类型
- *
- * @param event
- */
- @EventListener
- public void onApplicationEvent(BlackListEvent event) {
- System.out.println("应用事件:" + event);
- }
- /**
- * 如果方法需要监听好几个事件,或者如果定义的方法没有任何参数,也可以在注解自身上指定事件类型
- */
- @EventListener({ContextStartedEvent.class, ContextRefreshedEvent.class})
- public void handleContextStart() {
- System.out.println("监听到事件");
- }
- /**
- * 也可以通过定义SpEL表达式的注解condition属性添加额外的运行时过滤,
- * 他们应该匹配以实际调用特定事件的方法,例如如果事件的test属性等于foo
- * 才回调该方法
- * <p>
- * SpEL可用的元数据
- * 名字 位置 描述 例子
- * Event事件 根对象 实际应用事件 #root.event
- * Arguments array参数数组 根对象 用于目标数组 #root.args[0]
- * Argumentname参数名称 评估上下文 任意方法参数的名字
- */
- @EventListener(condition = "#event.test=='foo'")
- public void onApplicationEvent2(BlackListEvent event) {
- System.out.println("应用事件2:" + event);
- }
- /**
- * 如果需要发布一个事件作为另一个事件的结果,只需要更改方法签名返回应该发布的事件即可
- * 注意点是:异步事件不支持此功能
- */
- @EventListener
- public OtherEvent handlerBlackListEvent(BlackListEvent event) {
- }
- }
Spring 事件通知相关推荐
- spring事件通知机制详解
优势 解耦 对同一种事件有多种处理方式 不干扰主线(main line) 起源 要讲spring的事件通知机制,就要先了解一下spring中的这些接口和抽象类: ApplicationEventPub ...
- 案例学习BlazeDS+Spring之八InSync06“松耦合”UI同步事件通知
InSync06:增加"松耦合"UI同步事件通知 一.运行DEMO: 1.运行程序:http://localhost:8400/spring-flex-testdrive/insy ...
- Spring5源码 - 11 Spring事件监听机制_源码篇
文章目录 pre 事件监听机制的实现原理[观察者模式] 事件 ApplicationEvent 事件监听者 ApplicationEvent 事件发布者 ApplicationEventMultica ...
- Spring IoC 源码系列(三)Spring 事件发布机制原理分析
在 IoC 容器启动流程中有一个 finishRefresh 方法,具体实现如下: protected void finishRefresh() {clearResourceCaches();init ...
- Spring事件机制详解
一.前言 说来惭愧,对应Spring事件机制之前只知道实现 ApplicationListener 接口,就可以基于Spring自带的事件做一些事情(如ContextRefreshedEvent),但 ...
- Spring事件的观察者模式
介绍 观察者模式的本质是"定义对象之间的一对多依赖关系,以便当一个对象改变状态时,其所有依赖关系都会得到通知并自动更新." GoF. 观察者模式是发布/订阅模式的子集,它允许许多观 ...
- dubbo学习之事件通知实践
目录 实践 dubbo-demo-interface dubbo-demo-xml-provider notify-provider.xml UserNotifyServiceImpl Provide ...
- windows事件id大全_技术转载 || springboot+redis做过期事件通知业务
我的业务场景 系统管理员要给维护员分配巡查路口设施的工作,由于路口比较多,管理员不知道哪些路口已经被分配了,况且过了一个时间周期后,所有的路口要再次被巡查. 思路 我建立了一个表,里面是所有路口和是否 ...
- Spring事件发布机制
目录 前言 一.事件驱动机制 二.Spring的事件发布机制 三.Spring事件使用实例 四.Spring事件驱动的实现原理 总结: 前言 在设计模式中有一种设计模式叫做观察者设计模式,观察者模式的 ...
最新文章
- Fiddler监控面板显示Server栏(Fiddler v5.0)
- 试编写一个汇编语言程序,要求对键盘输入的小写字母用大写字母显示出来
- pycharm以及flask的安装
- python二十四:python练习题
- swoole 定时器
- 风格迁移应用_[风格迁移][超分][ECCV2016]Perceptual Losses for Real...
- linux配置chrony时间同步
- python编译安装没有c扩展_为什么在安装simplejson时得到“C扩展无法编译”?
- leetcode24. 两两交换链表中的节点
- 计算机组成与系统原理中的节拍是什么意思?
- Python小白的数学建模课-06.固定费用问题
- JavaScript原生对象属性和方法详解——Array对象
- Nginx-1.6.2更改端口
- 8.剑指Offer --- 英文版新增面试题
- 23. Element lastChild 属性
- 是时候考虑怎么用好云了
- 最完整的国内手机号段
- linux串口互斥,UART0串口编程之在UC/OS—II中遭遇的危机
- 南宁装修工长带队,价格公道不乱增加项目
- C# Invoke,begininvoke的用法详解