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. Storm概念学习系列之事务

    不多说,直接上干货! 事务 这里的事务是专门针对Topology提出来的,是为了解决元组在处理失败重新发送后的一系列问题的.简而言之,事务拓扑(transactional topology)就是指St ...

  2. java-类的定义和用法

    1.类的定义 public class Human{ }//每个源文件必须也只能有一个public类 class boy{ }//可以定义多个class类 class girl{ } 上面的类定义好后 ...

  3. JAVA 面试重点知识个人总结

    一.集合: 1 .Collection(是java.util下的接口) 和 Collections(是java.util下的类). 2 .List, Set,是否继承自Collection接口,Map ...

  4. vue-cli之脚手架

    一.创建VUE项目 npm install vue-cli -g vue init webpack myprject cd myproject npm run dev 补充: 组件:它是可扩展的htm ...

  5. li浮动 第二行第一个位置空白

    li浮动 第二行第一个位置空白:解决办法 li加上 vertical-align:bottom;overflow:hidden; 一行字多了 省略号代替: text-overflow: ellipsi ...

  6. HTML5&CSS挑战

    地址:https://www.w3cschool.cn/codecamp/list?pename=html5_and_css_camp 开始学习HTML标签:欢迎来到编程训练营的第一个编程挑战!你可以 ...

  7. TestNG并发测试包

    https://www.yiibai.com/testng/basic-annotations.html

  8. FTP添加虚拟目录(图)

    设置 访问效果 可以看到有这个文件:

  9. UOJ#210. 【UER #6】寻找罪犯 2-sat

    #210. [UER #6]寻找罪犯 链接:http://uoj.ac/problem/210 想法:2-sat模型.每个人拆点,分别表示为犯人.非犯人.每个句供词拆点,分别表示真话.假话.供词与对应 ...

  10. js学习笔记之随机数

    一. JS获取任意两个数之间的随机数 参考:https://www.jb51.net/article/89629.htm 二.获取一个10–100范围的数 参考:https://zhidao.baid ...