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

  

 

最新文章

  1. 动画的使用&mdash;View Animation
  2. iOS Developer Library地址
  3. DIV的不能包住子集解决办法
  4. iptables原理详解(一)
  5. “添加到收藏夹”功能(share)
  6. Date和Calendar时间操作常用方法及示例
  7. Win7家庭组的使用
  8. HTML video 视频标签全属性详解
  9. HIVE: collect_set(输出未包含在groupby的字段);
  10. D3.js 做一个简单的图表(条形图)
  11. jquery.autocomplete.js 插件的自定义搜索规则
  12. 在cocos2d-x界面中嵌入Android的WebView
  13. 爬虫保存cookies时重要的两个参数(ignore_discard和ignore_expires)的作用
  14. ISO 2501 quality model division 学习笔记
  15. flask异常处理
  16. java.net.UnknownHostException 异常处理
  17. 1159 Palindrome
  18. Mybatis控制台打印sql
  19. day_5.5 单例
  20. [android] ndk环境的搭建

热门文章

  1. C# 修改注册表立即刷新 转载
  2. Oracle数据库字符集问题
  3. sql-hive笔试题整理 1 (学生表-成绩表-课程表-教师表)
  4. css中div垂直居中的方法。
  5. springboot启动报错start bean &#39;eurekaAutoServiceRegistration&#39; NullPointerException
  6. SQLite多线程下的并发操作
  7. 【SpringBoot】 项目中运用的一些技巧,mybatis-plus 自动编译等(持续更新)
  8. HTML--JS 9*9乘法口诀
  9. maven项目 @Resource报错 ava.lang.NoSuchMethodError: javax.annotation.Resource.lookup()Ljava/lang/String;
  10. Docker安装Web前端性能测试工具Sitespeed.io