当前位置: 首页 > news >正文

org.springframework.context.ApplicationContext发送消息

1、创建消息的实体类

package com.demo;/*** 监听的实体类**/
public class EventMessage {private String name;public EventMessage(String name) {this.name = name;}public String getName() {return name;}public void setName(String name) {this.name = name;}
}

2、创建消息接收

package com.demo;import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;@Component
public class MessageEventListener {@EventListener(EventMessage.class)public void onApplicationEvent(EventMessage eventMessage) {System.out.println("监听器接收的消息:"+ eventMessage.getName());}
}

3、消息发送的controller

package com.demo;import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
public class MessageController {@Resourceprivate ApplicationContext applicationContext;@RequestMapping("/messageListener")public String messageListener(){applicationContext.publishEvent(new EventMessage("你好帅"));return "success";}
}

4、使用postman触发

在这里插入图片描述

5、结果

在这里插入图片描述

http://www.lryc.cn/news/404657.html

相关文章:

  • Java8-21新特性
  • NodeJS系列面试题
  • QXlsx读写excel
  • 昇思25天学习打卡营第13天 | mindspore 实现 ShuffleNet 图像分类
  • C语言超市管理系统UI界面
  • BUUCTF逆向wp [MRCTF2020]Xor
  • Windows版MySQL5.7解压直用(如何卸载更换位置重新安装)
  • 详解数据结构之二叉树(堆)
  • Linux----Mplayer音视频库的移植
  • STM32测测速---编码电机读取速度的计算
  • 【已解决】服务器无法联网与更换镜像源
  • android11 屏蔽usb通过otg转接口外接鼠标设备
  • HAL库源码移植与使用之RTC时钟
  • GIT命令学习 一
  • VS+QT 打包可执行文件.exe
  • Android笔试面试题AI答之Activity(2)
  • 来自Transformers的双向编码器表示(BERT) 通俗解释
  • 代码随想录第十六天|贪心算法(2)
  • 花几千上万学习Java,真没必要!(二十二)
  • 在RK3568上如何烧录MAC?
  • 1.30、基于卷积神经网络的手写数字旋转角度预测(matlab)
  • Windows如何使用Python的sphinx
  • C++ STL nth_element 用法
  • 【PostgreSQL教程】PostgreSQL 选择数据库
  • C# —— HashTable
  • LeetCode 第407场周赛个人题解
  • 使用Django框架实现音频上传功能
  • [路由器]IP-MAC的绑定与取消
  • Idea配置远程开发
  • lua 实现 函数 判断两个时间戳是否在同一天