Spring----事件(Application Event)
1、概述
1.1、Spring的事件 为Bean与Bean之间的消息通信提供了支持;
当一个Bean处理完一个任务后,希望另一个Bean知道并能做出相应的处理,这时我们需要 让另一个Bean 监听 当前Bean所发送的事件;
1.2、Spring的事件需要遵循如下流程:
a,自定义事件,继承ApplicationEvent
b,定义事件监听器,实现ApplicationListener
c,使用容器发布事件
1.3、eg:
package com.an.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* @description: 事件配置类
* @author: anpeiyong
* @date: Created in 2019/11/19 16:41
* @since:
*/
@Configuration
@ComponentScan(value = "com.an")
public class EventConfig {
}
package com.an.event; import org.springframework.context.ApplicationEvent; /**
* @description: 自定义事件
* @author: anpeiyong
* @date: Created in 2019/11/19 16:32
* @since:
*/
public class MyEvent extends ApplicationEvent { private String msg; public MyEvent(Object source,String msg) {
super(source);
this.msg=msg;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}
package com.an.event; import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; /**
* @description: 自定义事件监听器
* 实现ApplicationListener接口,并指定监听的事件类型ApplicationListener<MyEvent>
* 使用onApplicationEvent()方法对消息进行接收处理
* @author: anpeiyong
* @date: Created in 2019/11/19 16:34
* @since:
*/
@Component
public class MyListener implements ApplicationListener<MyEvent> { @Override
public void onApplicationEvent(MyEvent event) {
String msg=event.getMsg();
System.out.println("我接收到的消息:"+msg);
}
}
package com.an.event; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; /**
* @description:
* @author: anpeiyong
* @date: Created in 2019/11/19 16:38
* @since:
*/
@Component
public class MyPublisher { //注入ApplicationContext,用来发布事件
@Autowired
ApplicationContext applicationContext; public void publish(String msg){
//使用ApplicationContext.publishEvent()发布
applicationContext.publishEvent(new MyEvent(this,msg));
}
}
package com.an.main; import com.an.config.EventConfig;
import com.an.event.MyPublisher;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* @description:
* @author: anpeiyong
* @date: Created in 2019/11/19 16:41
* @since:
*/
public class EventMainTest { public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext=new AnnotationConfigApplicationContext(EventConfig.class);
MyPublisher myPublisher=annotationConfigApplicationContext.getBean(MyPublisher.class);
myPublisher.publish("hello");
annotationConfigApplicationContext.close();
} }
结果:
我接收到的消息:hello
Spring----事件(Application Event)的更多相关文章
- spring 事件(Application Event)
spring 事件为bean 与 bean之间传递消息.一个bean处理完了希望其余一个接着处理.这时我们就需要其余的一个bean监听当前bean所发送的事件. spring事件使用步骤如下: 1.先 ...
- 从命令模式的维度理解Spring 之Application Event
Spring的事件(Application Event)为Bean与Bean之间的信息通讯提供了支持.当一个Bean处理完一个任务之后,希望另一Bean指定并能做相应的处理,这时我们就需要让另外一个B ...
- Spring Boot实战笔记(四)-- Spring常用配置(事件Application Event)
一.事件(Application Event) Spring的事件为Bean和Bean之间的消息通信提供了支持.当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这时我们就需要 ...
- 精通SpringBoot--Spring事件 Application Event
Spring的事件为Bean与Bean之间的通信提供了支持,当我们系统中某个Spring管理的Bean处理完某件事后,希望让其他Bean收到通知并作出相应的处理,这时可以让其他Bean监听当前这个Be ...
- CL_GUI_ALV_GRID 触发PAI事件(Application event)
*&---------------------------------------------------------------------* *& Report Z_BARRY_A ...
- 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 处理完一个任务之后, ...
- Spring Application Event Example
Spring Application Event 项目结构 工程下载 https://github.com/xiaoheike/SpringApplicationEventExample.git Sp ...
- SpringBoot -- 事件(Application Event)
Spring的事件为Bean与Bean之间的消息通信提供了支持,当一个Bean处理完一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让一个Bean监听当前Bean所发送的事件. ...
- spring发布和接收定制的事件(spring事件传播)
spring发布和接收定制的事件(spring事件传播) 2012-12-26 20:05 22111人阅读 评论(2) 收藏 举报 分类: 开源技术(如Struts/spring/Hibernat ...
随机推荐
- js常见的几种设计模式
一.单例模式 单例模式也称作为单子模式,更多的也叫做单体模式.为软件设计中较为简单但是最为常用的一种设计模式. 在JavaScript里,实现单例的方式有很多种,其中最简单的一个方式是使用对象字面量的 ...
- [CSP-S模拟测试]:不等式(数学)
题目描述 小$z$热衷于数学.今天数学课的内容是解不等式:$L\leqslant S\times x\leqslant R$.小$z$心想这也太简单了,不禁陷入了深深的思考:假如已知$L,R,S,M$ ...
- ArayList的一些常用法<一> 转
转:http://yy.azj.blog.163.com/blog/static/18508700320122893451389/ import java.util.ArrayList; import ...
- SQL SERVER 2012文件表(FILETABLE)新体验之一
SQLSERVER 2012 文件表功能很COOL,让我们体验一下吧. 1,创建数据库 [sql] DREATE DATABASE FileTableTest ON PRIMARY ( NA ...
- ElasticSearch ClusterBlockException[blocked by: [FORBIDDEN/12/index read-only / allow delete (api)]锁定状态,无法插入数据
PUT /twitter/_settings { "index.blocks.read_only_allow_delete": null } 官网给出的解决办法
- rap安装mysql
1.yum仓库下载MySQL: yum localinstall https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm ...
- rpm --qf 命令
1. 环境准备: sudo apt-get install rpm (Ubuntu系统) wget ftp://rpmfind.net/linux/fedora-secondary/developme ...
- stl vector创建二维数组
vector<vector<); for (auto it = v.begin(); it != v.end(); it++) { ; (*it).reserve();//预留空间为5,但 ...
- String.prototype.includes
if (!String.prototype.includes) { String.prototype.includes = function(search, start) { 'use s ...
- centos7系统乱码问题解决
操作步骤: 查看当前系统的默认语言 echo $LANG 查看系统支持的语言库 locale 如果没有要设置的语言需要安装一下 yum groupinstall chinese-support -y ...