SpringBoot -- 事件(Application Event)
Spring的事件为Bean与Bean之间的消息通信提供了支持,当一个Bean处理完一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让一个Bean监听当前Bean所发送的事件。
Spring的事件需要遵循如下流程:
- 自定义事件,集成ApplicationEvent。
- 定义事件监听器,实现ApplicationListener。
- 使用容器发布事件。
一、自定义事件
package com.cenobitor.appevent.event;
import org.springframework.context.ApplicationEvent;
public class DemoEvent extends ApplicationEvent {
private static final long serialVersionUID = 1L;
private String msg;
public DemoEvent(Object source,String msg) {
super(source);
this.msg = msg;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
二、事件监听器
package com.cenobitor.appevent.listener; import com.cenobitor.appevent.event.DemoEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
//实现ApplicationListener接口,并指定监听的事件类型
@Component
public class DemoListener implements ApplicationListener<DemoEvent> { @Override
//使用onApplicationEvent方法对消息进行接收处理
public void onApplicationEvent(DemoEvent demoEvent) {
String msg = demoEvent.getMsg();
System.out.println("(bean-demoListener)接收到了bean-demoPublisher发布的消息:"+msg);
}
}
三、事件发布类
package com.cenobitor.appevent.publisher; import com.cenobitor.appevent.event.DemoEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; @Component
public class DemoPublisher {
@Autowired
//注入ApplicationContext用来发布事件
ApplicationContext applicationContext;
//使用ApplicationContext的publishEvent方法来发布
public void publish(String msg){
applicationContext.publishEvent(new DemoEvent(this,msg));
}
}
四、运行
package com.cenobitor.appevent; import com.cenobitor.appevent.publisher.DemoPublisher;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppeventApplication.class);
DemoPublisher demoPublisher = context.getBean(DemoPublisher.class);
demoPublisher.publish("hello application event");
context.close();
} }
结果:(bean-demoListener)接收到了bean-demoPublisher发布的消息:hello application event
注:摘抄自《JavaEE开发的颠覆者SpringBoot 实战》。
SpringBoot -- 事件(Application Event)的更多相关文章
- Spring Boot实战笔记(四)-- Spring常用配置(事件Application Event)
一.事件(Application Event) Spring的事件为Bean和Bean之间的消息通信提供了支持.当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这时我们就需要 ...
- spring 事件(Application Event)
spring 事件为bean 与 bean之间传递消息.一个bean处理完了希望其余一个接着处理.这时我们就需要其余的一个bean监听当前bean所发送的事件. spring事件使用步骤如下: 1.先 ...
- 精通SpringBoot--Spring事件 Application Event
Spring的事件为Bean与Bean之间的通信提供了支持,当我们系统中某个Spring管理的Bean处理完某件事后,希望让其他Bean收到通知并作出相应的处理,这时可以让其他Bean监听当前这个Be ...
- CL_GUI_ALV_GRID 触发PAI事件(Application event)
*&---------------------------------------------------------------------* *& Report Z_BARRY_A ...
- 从命令模式的维度理解Spring 之Application Event
Spring的事件(Application Event)为Bean与Bean之间的信息通讯提供了支持.当一个Bean处理完一个任务之后,希望另一Bean指定并能做相应的处理,这时我们就需要让另外一个B ...
- spring boot: 一般注入说明(五) @Component, application event事件为Bean与Bean之间通信提供了支持
spring的事件,为Bean与Bean之间通信提供了支持,当一个Bean处理完成之后,希望另一个Bean知道后做相应的事情,这时我们就让另外一个Bean监听当前Bean所发送的事件. spring的 ...
- Spring 事件:Application Event
Spring Application Event Spring 的事件(Application Event)为 Bean 与 Bean 之间的消息通信提供了支持.当一个 Bean 处理完一个任务之后, ...
- SpringBoot事件监听器源码分析
本文涉及到Spring的监听器,如果不太了解请先阅读之前的Spring监听器的文章. SpringBoot事件监听器初始化 SpringBoot中默认定义了11个事件监听器对象,全部定义在META-I ...
- Spring Application Event Example
Spring Application Event 项目结构 工程下载 https://github.com/xiaoheike/SpringApplicationEventExample.git Sp ...
随机推荐
- Linux防火墙配置与管理(16)
防火墙指的是一个由软件和硬件设备组合而成.在内部网和外部网之间.专用网与公共网之间的边界上构造的保护屏障.是一种获取安全性方法的形象说法,它是一种计算机硬件和软件的结合,使Internet与Intra ...
- cisco 的ACL
搞网络好几年了,怎么说呢,水平一直停留在NA-NP之间,系统的学完NA后,做了不少实验,后来也维护了企业的网络,各种网络设备都玩过(在商汤用的Juniper srx 550 我认为在企业环境,非IDC ...
- CentOS 设置 oracle 开机自动启动
CentOS 设置 oracle 开机自动启动 1. [root@localhost ~]# gedit /etc/oratab 文件内容为: # # This file is used by ORA ...
- dubbo+zookeeper注册服务报错问题:No service registed on zookeeper
2019-04-04 11:23:40,372 ERROR [tomcat-threads--1] (com.bill99.dolphin.controller.ExceptionController ...
- 前端基础——html
前端基础——html 在用CSS布局页面的时候,我们会将HTML标签分成两种,块级元素和内联元素(我们平常用到的div和p就是块级元素,链接标签a就是内联元素) 块级(行级)标签 和 内联标签 块级元 ...
- java重点知识
一.java基本知识点 java是由SUN公司在1995年推出的,在2009年SUN公司又被甲骨文公司收购,所以甲骨文公司获得java版权.其底层是由C语言写的,java分为三个体系: JavaSE, ...
- nginx配置文件 nginx.conf 说明
#user nobody; #开启进程数 <=CPU数 worker_processes 1; #错误日志保存位置 #error_log logs/error.log; #error_log ...
- JavaScript -- Anchor
-----052-Anchor.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=" ...
- Element ui级联地址省市区插件
安装 npm install element-china-area-data -S 使用 import { provinceAndCityData, regionData, provinceAndCi ...
- Python学习--11 面向对象高级编程
多重继承 Python里允许多重继承,即一个类可以同时继承多个类: class Mammal(Animal): pass class Runnable(object): def run(self): ...