Spring的事件为Bean与Bean之间的通信提供了支持,当我们系统中某个Spring管理的Bean处理完某件事后,希望让其他Bean收到通知并作出相应的处理,这时可以让其他Bean监听当前这个Bean所发送的事件。

要实现事件的监听,我们要做两件事:
1:自定义事件,继承ApplicationEvent接口
2:定义事件监听器,实现ApplicationListener
3:事件发布类


/**
* @TODO // 自定义事件,继承ApplicationEvent接口
* @Author Lensen
* @Date 2018/7/22
* @Description
*/
public class SendMsgEvent extends ApplicationEvent { private static final long serialVersionID = 1L; // 收件人
public String receiver; // 收件内容
public String content; public SendMsgEvent(Object source) {
super(source);
} public SendMsgEvent(Object source, String receiver, String content) {
super(source);
this.receiver = receiver;
this.content = content;
} public void output(){
System.out.println("I had been sand a msg to " + this.receiver);
}
}

/**
* @TODO //定义事件监听器,实现ApplicationListener
* @Author Lensen
* @Date 2018/7/22
* @Description
*/@Component
public class MsgListener implements ApplicationListener<SendMsgEvent> {
@Override
public void onApplicationEvent(SendMsgEvent sendMsgEvent) {
sendMsgEvent.output();
System.out.println(sendMsgEvent.receiver + "received msg : " + sendMsgEvent.content );
}
}

事件发布类

@Component
public class Publisher {
@Autowired
ApplicationContext applicationContext; public void publish(Object source, String receiver, String content){
applicationContext.publishEvent(new SendMsgEvent(source, receiver, content));
}
}

测试消息:WebConfig.class主要是为了扫描Publisher 和Listener类。里面有两个注解@ComponenScan和@Configuration。

    public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(WebConfig.class);
Publisher publisher = applicationContext.getBean(Publisher.class);
publisher.publish("Hello,World!","Mr.Lensen", "I Love U");
}

结果:

I had been sand a msg to Mr.Lensen
Mr.Lensen received msg : I Love U

精通SpringBoot--Spring事件 Application Event的更多相关文章

  1. spring 事件(Application Event)

    spring 事件为bean 与 bean之间传递消息.一个bean处理完了希望其余一个接着处理.这时我们就需要其余的一个bean监听当前bean所发送的事件. spring事件使用步骤如下: 1.先 ...

  2. 从命令模式的维度理解Spring 之Application Event

    Spring的事件(Application Event)为Bean与Bean之间的信息通讯提供了支持.当一个Bean处理完一个任务之后,希望另一Bean指定并能做相应的处理,这时我们就需要让另外一个B ...

  3. Spring Boot实战笔记(四)-- Spring常用配置(事件Application Event)

    一.事件(Application Event) Spring的事件为Bean和Bean之间的消息通信提供了支持.当一个Bean处理完一个任务之后,希望另一个Bean知道并能做相应的处理,这时我们就需要 ...

  4. CL_GUI_ALV_GRID 触发PAI事件(Application event)

    *&---------------------------------------------------------------------* *& Report Z_BARRY_A ...

  5. SpringBoot -- 事件(Application Event)

    Spring的事件为Bean与Bean之间的消息通信提供了支持,当一个Bean处理完一个任务之后,希望另外一个Bean知道并能做相应的处理,这时我们就需要让一个Bean监听当前Bean所发送的事件. ...

  6. spring boot: 一般注入说明(五) @Component, application event事件为Bean与Bean之间通信提供了支持

    spring的事件,为Bean与Bean之间通信提供了支持,当一个Bean处理完成之后,希望另一个Bean知道后做相应的事情,这时我们就让另外一个Bean监听当前Bean所发送的事件. spring的 ...

  7. Spring 事件:Application Event

    Spring Application Event Spring 的事件(Application Event)为 Bean 与 Bean 之间的消息通信提供了支持.当一个 Bean 处理完一个任务之后, ...

  8. Spring Application Event Example

    Spring Application Event 项目结构 工程下载 https://github.com/xiaoheike/SpringApplicationEventExample.git Sp ...

  9. SpringBoot零XML配置的Spring Boot Application

    Spring Boot 提供了一种统一的方式来管理应用的配置,允许开发人员使用属性properties文件.YAML 文件.环境变量和命令行参数来定义优先级不同的配置值.零XML配置的Spring B ...

随机推荐

  1. Python中的集合类型分类和集合类型操作符解析

    集合类型    数学上,把set称作由不同的元素组成的集合,集合(set)的成员通常被称作集合元素(set elements).    Python把这个概念引入到它的集合类型对象里.集合对象是一组无 ...

  2. Maven插件和细节

    1.父工程统一版本号 <!-- 集中定义依赖版本号 --> <properties> <junit.version>4.12</junit.version&g ...

  3. 排序算法对比,步骤,改进,java代码实现

    前言 发现是时候总结一番算法,基本类型的增删改查的性能对比,集合的串并性能的特性,死记太傻了,所以还是写在代码里,NO BB,SHOW ME THE CODE! github地址:https://gi ...

  4. Windows下使用nvm管理多个Node.js 版本

    下载 https://github.com/coreybutler/nvm-windows/releases 安装       配置 使用 # nvm install 版本号   # nvm list ...

  5. webpack 的异步组件 生成commonchunks

    new webpack.optimize.CommonsChunkPlugin({ async: 'async-common', minChunks: function (module, count) ...

  6. 生产消费者模式与python+redis实例运用(中级篇)

    上一篇文章介绍了生产消费者模式与python+redis实例运用(基础篇),但是依旧遗留了一个问题,就是如果消费者消费的速度跟不上生产者,依旧会浪费我们大量的时间去等待,这时候我们就可以考虑使用多进程 ...

  7. hibernate课程 初探单表映射2-4 transaction简介

    1 hibernate是非自动提交.如果transaction不写的话,会只创建表结构而不插入语句.   如果不写transaction而想实现插入的功能的话,需要重写session的dowork方法 ...

  8. windows 下设置MTU数值

    输入:netsh interface ipv4 show subinterfaces 查询到目前系统的MTU值.再分别输入一行按一次回车键. netsh interface ipv4 set subi ...

  9. ArcGIS中Features与JSON的互相转化

    实际操作过程非常简单,这里就简单记录下转换工具的位置:

  10. android-上下文菜单的创建 - 随心

    //Menu设置//覆盖两个方法onCreateOptionsMenu(Menu menu).onOptionsItemSelected(MenuItem Item)//onCreateOptions ...