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

  Spring的事件需要遵循如下流程:

  1. 自定义事件,集成ApplicationEvent。
  2. 定义事件监听器,实现ApplicationListener。
  3. 使用容器发布事件。

一、自定义事件

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)的更多相关文章

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

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

  2. spring 事件(Application Event)

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

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

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

  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. SpringBoot事件监听器源码分析

    本文涉及到Spring的监听器,如果不太了解请先阅读之前的Spring监听器的文章. SpringBoot事件监听器初始化 SpringBoot中默认定义了11个事件监听器对象,全部定义在META-I ...

  9. Spring Application Event Example

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

随机推荐

  1. GC日志时间分析

    在GC日志里,一条完整的GC日志记录最后,会带有本次GC所花费的时间,如下面这一条新生代GC: [GC [DefNew: 3298K->149K(5504K), secs] [Times: us ...

  2. flex定位下overflow失效的问题研究

    概述 这是我在写移动端页面遇到的问题及解决方法,记录下来供以后开发时参考,相信对其他人也有用. 问题 之前写移动端页面,有一个顶条是导航条,需要固定在页面顶部,并且里面的元素需要可以左右滚动. 但是当 ...

  3. OS之内存管理 --- 虚拟内存管理(二)

    关于虚拟内存管理之前的请看:OS之内存管理 - 虚拟内存管理(一) 帧分配 每个进程对的最小帧数是由操作系统的体系结构决定的,但是最大帧数是由可用物理内存的数量决定的.所以在这之间,对于进程的帧的分配 ...

  4. POJ 2485

    #include<iostream> #define MAXN 505 #define inf 1000000000 using namespace std; typedef int el ...

  5. Wilcoxon-Mann-Whitney rank sum test

    Wilcoxon-Mann-Whitney ranksum test 无节点状况,假定为样本服从类似形状,如果不是类似形状的话,秩的比较没有过多意义. X有m个数,Y有n个数 \(H_0:\mu_1= ...

  6. C++ 操作符、局部 全局变量及自动转换原则

    1.&:表示与操作,eg:2&1=2&&:表示and操作,eg:1&&0=0|:表示或操作,eg:2|1=3||:表示or操作,eg:1||0=12.全 ...

  7. 【转】Intellij IDEA使用总结

    原文地址:http://totohust.iteye.com/blog/1035550 1. IDEA内存优化 先看看你机器本身的配置而配置. \IntelliJ IDEA 8\bin\idea.ex ...

  8. 虚幻4引擎角色蓝图Character的Movement组件学习

    Jumping/Falling Air Control :角色在空中时的控制参数.数值为1 代表完全控制. Air Control Boost Multiplier :当角色的速度超过 Velocit ...

  9. CentOS安装PHP7+Nginx+MySQL

    本文属于动手搭建PHP开发环境的一部分,更多点击链接查看. 本文以centos6为例.命令部分均省略sudo命令. 安装PHP 下载 http://cn2.php.net/distributions/ ...

  10. 24-hadoop-hiveserver2&jdbc-正则数据导入

    hive 可以 类似jdbc链接, 但启动的必须是hiveserver2, 才可以使用 hiveserver2 默认监听 10000 端口 1, 启动: nohup $HIVE_HOME/bin/hi ...