1、概述

    1.1、Spring的事件  为Bean与Bean之间的消息通信提供了支持;

        当一个Bean处理完一个任务后,希望另一个Bean知道并能做出相应的处理,这时我们需要   让另一个Bean  监听  当前Bean所发送的事件;

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

        a,自定义事件,继承ApplicationEvent

        b,定义事件监听器,实现ApplicationListener

        c,使用容器发布事件   

    1.3、eg:

package com.an.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; /**
* @description: 事件配置类
* @author: anpeiyong
* @date: Created in 2019/11/19 16:41
* @since:
*/
@Configuration
@ComponentScan(value = "com.an")
public class EventConfig {
}

  

package com.an.event;

import org.springframework.context.ApplicationEvent;

/**
* @description: 自定义事件
* @author: anpeiyong
* @date: Created in 2019/11/19 16:32
* @since:
*/
public class MyEvent extends ApplicationEvent { private String msg; public MyEvent(Object source,String msg) {
super(source);
this.msg=msg;
} public String getMsg() {
return msg;
} public void setMsg(String msg) {
this.msg = msg;
}
}

  

package com.an.event;

import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; /**
* @description: 自定义事件监听器
* 实现ApplicationListener接口,并指定监听的事件类型ApplicationListener<MyEvent>
* 使用onApplicationEvent()方法对消息进行接收处理
* @author: anpeiyong
* @date: Created in 2019/11/19 16:34
* @since:
*/
@Component
public class MyListener implements ApplicationListener<MyEvent> { @Override
public void onApplicationEvent(MyEvent event) {
String msg=event.getMsg();
System.out.println("我接收到的消息:"+msg);
}
}

  

package com.an.event;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component; /**
* @description:
* @author: anpeiyong
* @date: Created in 2019/11/19 16:38
* @since:
*/
@Component
public class MyPublisher { //注入ApplicationContext,用来发布事件
@Autowired
ApplicationContext applicationContext; public void publish(String msg){
//使用ApplicationContext.publishEvent()发布
applicationContext.publishEvent(new MyEvent(this,msg));
}
}

  

package com.an.main;

import com.an.config.EventConfig;
import com.an.event.MyPublisher;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; /**
* @description:
* @author: anpeiyong
* @date: Created in 2019/11/19 16:41
* @since:
*/
public class EventMainTest { public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext=new AnnotationConfigApplicationContext(EventConfig.class);
MyPublisher myPublisher=annotationConfigApplicationContext.getBean(MyPublisher.class);
myPublisher.publish("hello");
annotationConfigApplicationContext.close();
} }

  

结果:

我接收到的消息:hello

  

 

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

  1. spring 事件(Application Event)

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

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

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

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

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

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

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

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

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

  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所发送的事件. ...

  10. spring发布和接收定制的事件(spring事件传播)

    spring发布和接收定制的事件(spring事件传播) 2012-12-26 20:05 22111人阅读 评论(2) 收藏 举报  分类: 开源技术(如Struts/spring/Hibernat ...

随机推荐

  1. 解决kubernetes拉取不了镜像仓库的问题

    将镜像仓库地址k8s.gcr.io改成registry.aliyuncs.com/google_containers

  2. 【2019 Multi-University Training Contest 9】

    01: 02:https://www.cnblogs.com/myx12345/p/11666961.html 03: 04: 05:https://www.cnblogs.com/myx12345/ ...

  3. CSS动画之旋转魔方轮播

    很久没有回头来复习CSS方面的知识了, 正好又到了月底写文章的deadline......所以这次选择了详细巩固一下CSS3动画有关的知识点,因为之前只是用过一些属性并没有深究细节. 在我自己写完这篇 ...

  4. 继承ConstraintLayout

    开发中复杂的布局基本上都可以通过ConstraintLayout实现,所以我们继承ConstraintLayout实现一个EasyConstraintLayout能够为子view添加圆角和阴影效果. ...

  5. oracle sql查询表外键关系

    SELECT F.TABLE_NAME, F.CONSTRAINT_NAME, F.COLUMN_NAME, F.POSITION, P.TABLE_NAME, P.COLUMN_NAME, P.PO ...

  6. /usr/bin/ld: cannot find -lgcc_s 问题解决小记

    /usr/bin/ld: cannot find -lgcc_s 问题解决小记 博客分类: Linux/Ubuntu 由于之前用wubi装的ubuntu并且只给了它10G的硬盘空间,随着学习的深入这种 ...

  7. mariadb(一)基础

    一.数据库介绍 1.什么是数据库? 简单的说,数据库就是一个存放数据的仓库,这个仓库是按照一定的数据结构(数据结构是指数据的组织形式或数据之间的联系)来组织,存储的,我们可以通过数据库提供的多种方法来 ...

  8. uni-app 云打包证书生成笔记

    一.android 云打包生成证书 1.jdk和jre安装,配置环境 2.在 C:\Users\Administrator\.android 文件夹下执行:keytool -genkey -alias ...

  9. Vagrant 手册之 Vagrantfile - 机器设置 config.vm

    原文地址 配置的命名空间:config.vm config.vm 中的设置修改 Vagrant 管理的机器的配置. 1. 可用的设置项 config.vm.boot_timeout Vagrant 等 ...

  10. HDU 2063 过山车 (匈牙利算法)

    题目链接:HDU 2063 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩 ...