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. apache如何发布地图服务

    svg jpg Tomcat和apache是什么关系呢?:https://www.cnblogs.com/zangdalei/p/8057325.html geoserver又是怎么来的呢? Tomc ...

  2. (4)Linux(ubuntu)下配置Opencv3.1.0开发环境的详细步骤

    Ubuntu下配置opencv3.1.0开发环境 1.最近工作上用到在Ubuntu下基于QT和opencv库开发应用软件(计算机视觉处理方面),特把opencv的配置过程详细记录,以供分享 2.步骤说 ...

  3. phpjm解密程序,也适用于其他混淆加密的破解

    <?php $file = 'plugin.php'; //要破解的文件 $fp = fopen($file, 'r'); $str = fread($fp, filesize($file)); ...

  4. 牛客多校第五场G

    subsequence 1 只要处理长度等于t的. 转移方程没想出来QAQ $dp(i,j,0)$代表到$s[i]$为止有多少个前缀序列与$t[0\cdots j]$相同 所以有$dp(i,j,0)= ...

  5. DAO层单元测试编码和问题排查

    DAO层单元测试编码和问题排查 SecKillDaoTest .java(注意接口参数使用注解@Parm(“parameter”)) package org.secKill.dao; import o ...

  6. 台哥原创:java 俄罗斯方块源码

    大四的时候,用java开发,耗时一周 界面参照当时用的联想手机里的俄罗斯方块 ​ 这里的级别,标识难度,1是初级,方块下降速度很慢,5是最高级,下降速度最快 ​ 得分:每消除一行,会给10分,同时消除 ...

  7. android7.0后对于detected problems with app native libraries提示框显示

    log信息: 03-27 09:08:25.887   397   400 W linker  : /data/app/com.guagua.qiqi-1/lib/arm/libMedia.so ha ...

  8. php面向对象重的抽象类,接口类与静态

    static 静态 <?php class ren { public $name; public static $sex; static function shao() { echo " ...

  9. SqL语句基础之增删改查

    增查删改的SQL语句,如此的实用,下面我就来简单介绍一下它简单的用法. 1.什么是SQL? SQL是用于访问和处理数据库的标准的一种计算机语言. 2.SQL可以做什么?  (1)可以向数据库进行查询 ...

  10. Integer自动装箱和拆箱

    Integer a=3;   =>    Integer a=Integer.valueOf(3); /** *@description: 自动装箱和拆箱 *@auther: yangsj *@ ...