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 ...
随机推荐
- nginx-2.nginx是什么
Nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器: Nginx可以作为一个HTTP服务器进行网站的发布处理,另外Nginx可以 ...
- python中的三次握手以及四次挥手
三次握手1.客户端对服务端说:我的序号是x,我要向你请求连接:(第一次握手,发送SYN包,然后进入SYN-SEND状态)2.服务端听到之后对客户端说:我的序号是y,期待你下一句序号是x+1的话(意思就 ...
- SSH 学习笔记
零.背景 在看 pm2 的 deploy 功能的时候,对 ssh 的不熟悉导致错误频出,包括之前对 github 的配置也用到了 SSH,所以找个机会整理一下. 一.介绍 SSH 是每一台 Linux ...
- SubLime Text 3 配置SublimeREPL来交互式调试程序
1. 安装 SublimeREPL 插件 等待一下,输入sublimerepl,选择sublimeREPL,然后它就会在后台安装. 安装完之后,查看如下图 选择你要执行的*.py文件,通过这个路径,选 ...
- python安装mysqlclient模块报fatal error: Python.h:解决方法
在搭建Flask框架安装mysqlclient模块时候老是报fatal error: Python.h:错误,折腾老半天,百度了老半天看了不少大神帖子,就是没解决, 后来发现这不是个BUG,都是自己的 ...
- [工具]Tomcat CVE-2017-12615 远程代码执行
工具: K8_TocmatExp编译: VS2012 C# (.NET Framework v2.0)组织: K8搞基大队[K8team]作者: K8拉登哥哥博客: http://qqhack8.b ...
- POJ 2707
#include<iostream> #include<stdio.h> #include<algorithm> using namespace std; int ...
- SqlServer 查看被锁的表和解除被锁的表
查看被锁的表 1 2 select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableName ...
- 摆脱定时任务的cron表达式的困扰
一.背景 最近因为需要,需要适用Spring的task定时任务进行跑定时任务,以前也接触过,但是因为懒没有好好地理解@Scheduled的cron表达式,这次便对它做了一个全方位的了解和任务,记录下来 ...
- Python爬取简书主页信息
主要学习如何通过抓包工具分析简书的Ajax加载,有时间再写一个Multithread proxy spider提升效率. 1. 关键点: 使用单线程爬取,未登录,爬取简书主页Ajax加载的内容.主要有 ...