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. Android---------------ContentProvider的学习

    1.Uri  uri = Intent.getData()------------->可以获得Uri的地址 2.Cursor cursor = getContentResolver().quer ...

  2. Python 读取文件中unicode编码转成中文显示问题

    Python读取文件中的字符串已经是unicode编码,如:\u53eb\u6211,需要转换成中文时有两种方式 1.使用eval: eval("u"+"\'" ...

  3. haproxy监测页面参数简释

    Queue Cur: current queued requests //当前的队列请求数量Max:max queued requests     //最大的队列请求数量Limit:         ...

  4. @transactional作用和事务

    今天在博客园看到有发布spring的注解,留意到@transactional这个注解.立马就百度.学习了 使用这个注解的类或者方法表示该类里面的所有方法或者这个方法的事务由spring处理,来保证事务 ...

  5. python(29)----时间模块

    time模块 1. 三种时间表现形式 时间戳(timestamp) 格式化的时间字符串 元祖/结构化时间(struct_time) 2. 时间戳(timestamp) 通常来说,时间戳表示的是从197 ...

  6. 【2019北京集训2】duck 线段树优化建图+tarjan

    题目大意:给你$n$个点,第$i$个点有点权$v_i$.你需要将这$n$个点排成一排,第$i$个点的点权能被累加当且仅当这个点前面存在编号在$[l_i,r_i]$中的点,问你这些点应该如何排列,点权和 ...

  7. (转)Python: super 没那么简单

    原文:https://mozillazg.com/2016/12/python-super-is-not-as-simple-as-you-thought.html python 约定¶ 单继承¶ 多 ...

  8. 磁盘分区(20G升50G)

      不多说,直接上干货! 本博文的主要内容有 .磁盘分区的概述 .常用的磁盘管理工具    ./下分5G,给/home扩容    .系统自带的fdisk和parted这两款工具 .磁盘空间管理 前言 ...

  9. Vue + Element UI 实现权限管理系统 前端篇(五):国际化实现

    国际化支持 1.安装依赖 执行以下命令,安装 i18n 依赖. yarn add vue-i18n $ yarn add vue-i18n yarn add v1.9.4 warning packag ...

  10. jmeter安装教程与新手入门(附jdk安装教程)

    一.前言 最近要对网站做性能测试,提到了并发数测试,查了下,还是决定使用jmeter来完成这项测试,这里总结了jmeter完整的安装教程,附上新手使用教程. 二.jmeter安装 1.jdk安装(jm ...