1. 首先创建需要监听的事件类
import org.springframework.context.ApplicationEvent;
public class GoodsEvent extends ApplicationEvent {private static final long serialVersionUID=-4745768546266023205L;private String desc = "这是监听器";public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}public GoodsEvent(Object source, String desc) {super(source);this.desc= desc;}
}

2.创建监听类

@Component
public class GoodsListener implements Serializable{public static final long serialVersionUID = 3072098993210509725L;@EventListener//或者implements ApplicationListener<GoodsEvent>@Async//支持异步public void execute(GoodsEvent event) throws Exception         {          Thread.sleep(5000);System.out.println("------------------11111111"+event.getDesc()); //------------------11111111这是监听器;}
}

3.创建触发监听的类

@Component("goods_service")
public class GoodsService {public void executeGoodsData() throws Exception {
//因为采用了异步注解
SpringContextUtil.getApplicationContext().publishEvent(new GoodsEvent(this,"54321"));System.out.println("------------------22222222")  //此处先打印}}}}

中规中矩的Spring事件监听相关推荐

  1. Spring5源码 - 13 Spring事件监听机制_@EventListener源码解析

    文章目录 Pre 概览 开天辟地的时候初始化的处理器 @EventListener EventListenerMethodProcessor afterSingletonsInstantiated 小 ...

  2. Spring5源码 - 12 Spring事件监听机制_异步事件监听应用及源码解析

    文章目录 Pre 实现原理 应用 配置类 Event事件 事件监听 EventListener 发布事件 publishEvent 源码解析 (反推) Spring默认的事件广播器 SimpleApp ...

  3. Spring5源码 - 11 Spring事件监听机制_源码篇

    文章目录 pre 事件监听机制的实现原理[观察者模式] 事件 ApplicationEvent 事件监听者 ApplicationEvent 事件发布者 ApplicationEventMultica ...

  4. Spring事件监听原理

    1 简述Spring的生命周期 不论是Spring的监听机制原理还是Spring AOP的原理,都是依托于Spring的生命周期,所以要了解Spring的监听机制原理就需要先了解Spring的生命周期 ...

  5. spring 事件监听

    用一个简单的例子来实现spring事件监听的功能 这个例子主要功能是,记录那些用户是第一次登入系统,如果用户是第一次登入系统,则调用spring的事件监听,记录这些用户. 主要用到的spring的类和 ...

  6. Spring事件监听流程分析【源码浅析】

    一.简介 Spring早期是通过实现ApplicationListener接口来定义监听事件,在spring4.2的时候开始我们可以通过@EventListener注解来定义监听事件,Applicat ...

  7. Spring5源码 - 10 Spring事件监听机制_应用篇

    文章目录 Spring事件概览 事件 自定义事件 事件监听器 基于接口 基于注解 事件广播器 Spring事件概览 Spring事件体系包括三个组件:事件,事件监听器,事件广播器 事件 Spring的 ...

  8. spring 扫描所有_自定义Spring事件监听机制

    开头提醒一下大家: 尽管我简化了Spring源码搞了个精简版的Spring事件机制,但是没接触过Spring源码的朋友阅读起来还是有很大难度,请复制代码到本地,边Debug边看 既然要简化代码,所以不 ...

  9. 如何实现业务解耦?spring中事件监听了解一下

    一.前言 二.业务解耦之事件监听实战         1.事件监听三大组件          2.事件发布以及监听实战案例          3.事件发布以及监听原理 一.前言 耦合这个词在平常的开发 ...

最新文章

  1. Non-static field ‘func1‘ cannot be referenced from from a static context
  2. php使用接口实现工厂设计模式
  3. C++中友元函数,友元类数详解
  4. youcans 的 OpenCV 学习课—3.图像的创建与修改
  5. SpringCloud Alibaba-Nacos 的使用
  6. php获取扫码枪的数据,js 获取扫码枪输入数据的方法
  7. 深度学习笔记(30) Inception网络
  8. ajax请求解析json,如何为Ajax请求解析json响应?
  9. android 饿了么地图,饿了么送餐位置地图定位代码
  10. 信息管理与信息系统专业渊源
  11. 《Dotnet9》系列-开源C# WPF控件库3《HandyControl》强力推荐
  12. c#数据格式化之DataFormatString
  13. lomboz连接mysql数据库_Lomboz如何配置Tomcat数据库连接池
  14. 第四章 基于QT和DCMTK的Dicom 图像浏览器---检查文件夹下Dicom序列个数
  15. 02 stata入门【计量经济学及stata应用】
  16. 高德地图 地理编码报result参数报 USERKEY_PLAT_NOMATCH INVALID_USER_SCODE
  17. 五分钟学会python函数_Python——带你五分钟了解函数式编程与闭包
  18. 求阶乘之和 0到n阶乘的和
  19. mysql5.0忘记root密码_【咨询】mysql忘记root密码的处理方法(5.5/5.0)
  20. 倩女手游经验计算机,倩女幽魂手游卡级屯经验指南 短期反超成吨经验

热门文章

  1. 哔哩哔哩2020校园招聘后端笔试卷(一)
  2. 新一配:perl循环调用python爬虫批量下载喜马拉雅音频
  3. tesla p100 linux,服务器(Tesla P100)Ubuntu16.04+显卡驱动+cuda8.0+cudnn7.0.5
  4. AE制作粉笔字特效教程 3分钟快速制作Vlog片头
  5. AEJoy ——表达式之彩色文字尾随特效【JS】
  6. 互联网数据中心——IDC(Internet Data Center)
  7. c语言保存mat图片的方法,把opencv Mat 按位存成bmp二值图像 (1bit 1pixel)
  8. netfilter连接跟踪(conntrack)详述
  9. JavaMail QQ邮箱发送内容+图片
  10. Flutter CircularProgressIndicator进度指示器/Loading