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. Pytorch - GPU ID 指定 pytorch gpu 指定

    PyTorch 关于多 GPUs 时的指定使用特定 GPU. PyTorch 中的 Tensor,Variable 和 nn.Module(如 loss,layer和容器 Sequential) 等可 ...

  2. Microsoft.SQL.Server2012.Performance.Tuning.Cookbook学习笔记(二)

    Creating trace with system stored procedures Following are the stored procedures which you should kn ...

  3. Python关键点常识

    关键点常识 Python的发音与拼写 Python的作者是Guido van Rossum(龟叔) Python正式诞生于1991年 Python的解释器如今有多个语言实现,我们常用的是CPython ...

  4. hdu 1532 Drainage Ditches(最大流模板题)

    Drainage Ditches Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. oracle用EXPLAIN PLAN 分析SQL语句

    EXPLAIN PLAN 是一个很好的分析SQL语句的工具,它甚至可以在不执行SQL的情况下分析语句. 通过分析,我们就可以知道ORACLE是怎么样连接表,使用什么方式扫描表(索引扫描或全表扫描)以及 ...

  6. SuperSocket进程级别隔离

    在 SuperSocket 1.5 中, 我们增加了 AppDomain 级别隔离的功能,让你可以运行多个服务器实例在相互独立的 AppDomain 上. 此功能提供了较高级别的安全性和资源的隔离,并 ...

  7. 【CSS3 + 原生JS】移动的标签

    左图为本博客右侧截取的GIF图,右图为代码效果 HTML: <!DOCTYPE html> <html lang="en"> <head> &l ...

  8. Python--day26--类方法和静态方法

    类方法: 静态方法:

  9. Mule自带例子之stockquote

    1 配置效果图 2 配置文件 <?xml version="1.0" encoding="UTF-8"?> <mule version=&qu ...

  10. p2p平台详细运营框架

    市场拓展部1.负责完成公司市场销售.市场拓展.费用控制等年度目标任务,并负责将目标责任制分解落实,确保各项工作目标得以实现.2.对营销政策.市场及同业营销动态等方面进行调研分析,及时调整营销策略和计划 ...