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 ...
随机推荐
- IO模型 IO多路复用
阻塞IO 用socket 一定会用到accept recv recvfrom这些方法正常情况下 accept recv recvfrom都是阻塞的 非阻塞IO 如果setblocking(False) ...
- 522. Longest Uncommon Subsequence II
Given a list of strings, you need to find the longest uncommon subsequence among them. The longest u ...
- Linux系统文件权限管理(6)
Linux操作系统是多任务(Multi-tasks)多用户(Multi-users)分时操作系统,linux操作系统的用户就是让我们登录到linux的权限,每当我们使用用户名登录操作系统时,linux ...
- MariaDB 插入&更新&删除数据(8)
MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可MariaDB的目的是完全兼容MySQL,包括API和命令行,MySQL由于现在闭源了,而能轻松成为MySQ ...
- CSP攻略
看完三篇文章应该就懂了csp是干嘛的. https://www.cnblogs.com/Wayou/p/intro_to_content_security_policy.html https://ww ...
- POJ 2643
#include<iostream> #include<stdio.h> #include<string> #include<algorithm> #d ...
- nginx lua集成
版本说明: linux: 1.8.1 luajit:2.0.2(不要使用标准lua,应当使用luajit, 后者的效率比前者高很多) ngx_devel_kit: 0.2.18 lua-nginx-m ...
- 工具-CrashMonkey4IOS,Monkey测试方案
在TesterHome看到了CrashMonkey4IOS,顿时觉得之前用instrument在做monkey测试,非常的弱智!crash后啥都看不到,无crashlog,无crash步骤,并且也不能 ...
- [Java初探实例篇02]__流程控制语句知识相关的实例练习
本例就流程控制语句的应用方面,通过三个练习题来深入学习和巩固下学习的流程控制语句方面的知识,设计到,if条件判断语句,switch多分支语句,for循环语句及其嵌套多层使用,while循环语句. 练习 ...
- Ceph 块设备 - 块设备快速入门
目录 一.准备工作 二.安装 Ceph 三.使用块存储 一.准备工作 本文描述如何安装 ceph 客户端,使用 Ceph 块设备 创建文件系统并挂载使用. 必须先完成 ceph 存储集群的搭建,并 ...