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

  Spring中使用事件的大概流程如下:

  (1)定义事件

  (2)定义事件监听器

  (3)使用容器发布事件


示例:

  (1)定义事件

  自定义事件需要实现ApplicationEvent接口。


package learnspring.learnspring.event;

import org.springframework.context.ApplicationEvent;

/**
* @author 肖政宇
* @date 2019-09-23 14:36
* 说明:自定义事件
*/
public class DemoEvent extends ApplicationEvent {
private static final long serialVersionUID = 1L;
private String msg; DemoEvent(Object source, String msg) {
super(source);
this.msg = msg;
} String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}

  (2)定义事件监听器

  


package learnspring.learnspring.event;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; /**
* @author 肖政宇
* @date 2019-09-23 14:39
* 说明:事件监听器
* 实现ApplicationListener接口,同时声明监听的事件类型
*/
@Component
public class DemoListener implements ApplicationListener<DemoEvent> {
@Override
public void onApplicationEvent(DemoEvent event) {
String msg = event.getMsg();
System.out.println("我(bean-demoListener)接收到了bean-demoPublisher发布的消息:" + msg);
}
}
 

  (3)使用容器发布事件

package learnspring.learnspring.event;
package learnspring.learnspring.event;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; /**
* @author 肖政宇
* @date 2019-09-23 14:43
* 说明:事件发布者
*/
@Component
public class DemoPublisher {
private ApplicationContext applicationContext; @Autowired
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
} public void publish(String msg) {
applicationContext.publishEvent(new DemoEvent(this, msg));
}
}

测试:


package learnspring.learnspring;

import learnspring.learnspring.event.DemoPublisher;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class LearnspringApplicationTests {
DemoPublisher publisher; @Autowired
public void setPublisher(DemoPublisher publisher) {
this.publisher = publisher;
} @Test
public void contextLoads() {
System.out.println("事件发生!");
publisher.publish("Hello World!");
} }
 

运行结果:

  

Spring应用事件(Application Event)的更多相关文章

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

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

  2. 精通SpringBoot--Spring事件 Application Event

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

  3. spring 事件(Application Event)

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

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

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

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

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

  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 -- 事件(Application Event)

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

随机推荐

  1. Android 监听软键盘搜索键

    现在很多的Android应用都有了数据搜索功能,在以往的设计上,会使用搜索框+搜索按钮来实现搜索功能: 现在呢,越来越流行的是,去除搜索按钮,直接监听软键盘搜索键,当用户输入完搜索关键字后,直接点击软 ...

  2. Python3.6正向解析与反向解析域中主机

    公司最近接手的一家跨国企业的项目,该企业单域.多站点,且遍布美国.巴西.日本.东京.新加坡等多个国家,服务器及客户端计算机数量庞大.由于处理一些特殊故障,需要找出一些不在域中的网络设备及存储.NBU等 ...

  3. CDN WAF功能开放公测 提升网络应用安全性能

    阿里云CDN WAF功能,是指CDN融合了云盾Web应用防火墙(Web Application Firewall,简称 WAF)能力,在CDN节点上提供安全防护的功能,该功能目前已经开放公测. WAF ...

  4. shell去掉最后一个字符

    实测过第一种写法,可正常删除 sed 's/.$//' awk '{sub(/.$/,"")}1' awk '{printf $0"\b \n"}' ufile ...

  5. C# Brush Color String 互相转换

    using System.Windows.Media; //String转换成Color Color color = (Color)ColorConverter.ConvertFromString(s ...

  6. 冒泡排序&直接插入排序&快速排序

    一.冒泡排序 0       1      2      3      4      5 假设有一个6个数的数组,0,1,2,3,4,5是索引,冒泡排序就是相邻两个对比,比如5和4比,如果满足条件就互 ...

  7. Project Euler Problem 9-Special Pythagorean triplet

    我是俩循环暴力 看了看给的文档,英语并不好,有点懵,所以找了个中文的博客看了看:勾股数组学习小记.里面有两个学习链接和例题. import math def calc(): for i in rang ...

  8. Python--day48--ORM框架SQLAlchemy操作表

    ORM框架SQLAlchemy操作表: 表结构和数据库连接: #!/usr/bin/env python # -*- coding:utf-8 -*- from sqlalchemy.ext.decl ...

  9. win10下,cmd,power shell设置默认编码为‘UTF-8’?

    这个问题可以终结了,最新版 Windows 10 支持 UTF-8 了.打开这个选项,cmd 和 powershell 默认就是 UTF-8 了.在控制面板-时钟和区域-区域-管理-更改系统区域设置( ...

  10. 【hdu 1112】The Proper Key

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...