iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >基于SpringBoot怎么应用ApplicationEvent
  • 228
分享到

基于SpringBoot怎么应用ApplicationEvent

2023-07-05 11:07:25 228人浏览 独家记忆
摘要

这篇文章主要介绍“基于SpringBoot怎么应用ApplicationEvent”,在日常操作中,相信很多人在基于springBoot怎么应用ApplicationEvent问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希

这篇文章主要介绍“基于SpringBoot怎么应用ApplicationEvent”,在日常操作中,相信很多人在基于springBoot怎么应用ApplicationEvent问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”基于SpringBoot怎么应用ApplicationEvent”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

一、案例场景

发起restful请求,根据请求参数发布不同的事件。

事件监听者,监听到事件后,做预定操作。

本例是事件同步处理机制,即发布事件后,会同步监听事件。

二、使用类

org.springframework.context.ApplicationEvent,spring的事件对象。

org.springframework.context.ApplicationListener,事件监听者接口。

org.springframework.context.ApplicationEventPublisher,事件发布者接口。

三、代码

1.事件对象

事件对象实现ApplicationEvent。

1.1 ExampleApplicationEvent

ExampleApplicationEvent,一个抽象类。继承ApplicationEvent,自定义拓展微服务中需求的一些属性。

public abstract class ExampleApplicationEvent extends ApplicationEvent {  private static final String eventSource = "Example";  private String eventType = null;  private Object eventData = null;  public ExampleApplicationEvent(Object eventData) {      super(eventSource);      this.eventData = eventData;  }  public ExampleApplicationEvent(Object eventData, String eventType) {      super(eventSource);      this.eventData = eventData;      this.eventType = eventType;  }  public String getEventType() {      return eventType;  }  public Object getEventData() {      return eventData;  }}
1.2 ExampleLocalApplicationEvent

ExampleLocalApplicationEvent,是抽象类ExampleApplicationEvent的实现类,在此处按需拓展属性。

public class ExampleLocalApplicationEvent extends ExampleApplicationEvent {  public ExampleLocalApplicationEvent(Object eventData) {      super(eventData);  }  public ExampleLocalApplicationEvent(Object eventData, String eventType) {      super(eventData, eventType);  }}
1.3 EventTypeEnum

EventTypeEnum,自定义事件类型枚举,按需扩展。

public enum EventTypeEnum {  CHANGE("change", "变更事件"),  ADD("add", "新增事件");  private String id;  private String name;  public String getId() {   return id;  }  public String getName() {   return name;  }  EventTypeEnum(String id, String name) {   this.id = id;   this.name = name;  }  public static EventTypeEnum getEventTypeEnum(String id) {   for (EventTypeEnum var : EventTypeEnum.values()) {       if (var.getId().equalsIgnoreCase(id)) {           return var;       }   }   return null;  }}

2.事件监听者

事件监听者包括接口和抽象类。

2.1 IEventListener

IEventListener,一个接口,继承ApplicationListener接口。

@SuppressWarnings("rawtypes")public interface IEventListener extends ApplicationListener {}
2.2 AbstractEventListener

AbstractEventListener,一个抽象类,实现IEventListener接口。并提供抽象方法便于实现类扩展和代码解耦。

public abstract  class AbstractEventListener implements IEventListener { @Override public void onApplicationEvent(ApplicationEvent event){  if(!(event instanceof ExampleApplicationEvent)){    return;  }  ExampleApplicationEvent exEvent = (ExampleApplicationEvent) event;  try{    onExampleApplicationEvent(exEvent);  }catch (Exception e){    e.printStackTrace();  } } protected abstract void onExampleApplicationEvent(ExampleApplicationEvent event);}
2.3 OrderEventListener

OrderEventListener,实现类AbstractEventListener抽象类。监听事件,并对事件做处理,是一个业务类。

@Slf4j@Componentpublic class OrderEventListener extends AbstractEventListener {  @Override  protected void onExampleApplicationEvent(ExampleApplicationEvent event) {   log.info("OrderEventListener->onSpApplicationEvent,监听事件.");   Object eventData = event.getEventData();   log.info("事件类型: " + EventTypeEnum.getEventTypeEnum(event.getEventType()));   log.info("事件数据: " + eventData.toString());  }}

3.事件发布者

事件监听者包括接口和实现类。

3.1 IEventPublisher

IEventPublisher,自定义事件发布接口,方便扩展功能和属性。

public interface IEventPublisher {    boolean publish(ExampleApplicationEvent event);}
3.2 LocalEventPublisher

LocalEventPublisher,事件发布实现类,此类使用@Component,spring的ioc容器会加载此类。此类调用ApplicationEventPublisher的publishEvent发布事件。

@Slf4j@Component("localEventPublisher")public class LocalEventPublisher implements IEventPublisher {  @Override  public boolean publish(ExampleApplicationEvent event) {    try{      log.info("LocalEventPublisher->publish,发布事件.");      log.info("事件类型: " + EventTypeEnum.getEventTypeEnum(event.getEventType()));      log.info("事件数据: " + event.getEventData().toString());      SpringUtil.getApplicationContext().publishEvent(event);    }catch (Exception e){      log.info("事件发布异常.");      e.printStackTrace();      return false;    }    return true;  }}

4.Restful请求触发事件

使用Restful请求触发事件发生。

4.1 EventController

EventController,接收Restful请求。

@Slf4j@RestController@RequestMapping("/event")public class EventController {  @Autowired  private LocalEventPublisher eventPublisher;  @PostMapping("/f1")  public Object f1(@RequestBody Object obj) {   log.info("EventController->f1,接收参数,obj = " + obj.toString());   Map objMap = (Map) obj;   OrderInfo orderInfo = new OrderInfo();   orderInfo.setUserName((String) objMap.get("userName"));   orderInfo.setTradeName((String) objMap.get("tradeName"));   orderInfo.setReceiveTime(DateUtil.fORMat(new Date(),           "yyyy-MM-dd HH:mm:ss"));   String flag = (String) objMap.get("flag");   if (StringUtils.equals("change", flag)) {       eventPublisher.publish(new ExampleLocalApplicationEvent(orderInfo,               EventTypeEnum.CHANGE.getId()));   } else if (StringUtils.equals("add", flag)) {       eventPublisher.publish(new ExampleLocalApplicationEvent(orderInfo,               EventTypeEnum.ADD.getId()));   } else {       eventPublisher.publish(new ExampleLocalApplicationEvent(orderInfo));   }   log.info("EventController->f1,返回.");   return ResultObj.builder().code("200").message("成功").build();  }}
4.2 OrderInfo

OrderInfo,数据对象,放入事件对象中传递。

@Data@NoArgsConstructorpublic class OrderInfo {  private String userName;  private String tradeName;  private String receiveTime;}
4.3 ResultObj

ResultObj,restful返回通用对象。

@Data@NoArgsConstructor@AllArgsConstructor@Builderpublic class ResultObj {    private String code;    private String message;}

5.测试

5.1 请求信息

URL请求: Http://127.0.0.1:8080/server/event/f1

入参:

{    "userName": "HangZhou",    "tradeName": "Vue进阶教程",    "flag": "add"}

返回值:

{    "code": "200",    "message": "成功"}
5.2 日志

输出日志:

基于SpringBoot怎么应用ApplicationEvent

到此,关于“基于SpringBoot怎么应用ApplicationEvent”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!

--结束END--

本文标题: 基于SpringBoot怎么应用ApplicationEvent

本文链接: https://www.lsjlt.com/news/351242.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 基于SpringBoot怎么应用ApplicationEvent
    这篇文章主要介绍“基于SpringBoot怎么应用ApplicationEvent”,在日常操作中,相信很多人在基于SpringBoot怎么应用ApplicationEvent问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希...
    99+
    2023-07-05
  • 基于SpringBoot应用ApplicationEvent案例场景
    目录一、案例场景二、使用类三、代码1.事件对象1.1 ExampleApplicationEvent1.2 ExampleLocalApplicationEvent1.3 Event...
    99+
    2023-03-09
    Spring Boot应用ApplicationEvent Spring Boot ApplicationEvent
  • SpringBoot中ApplicationEvent和ApplicationListener怎么使用
    本篇内容主要讲解“SpringBoot中ApplicationEvent和ApplicationListener怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“SpringBoot中App...
    99+
    2023-07-05
  • 基于Maven怎么安装SpringBoot
    这篇“基于Maven怎么安装SpringBoot”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“基于Maven怎么安装Spri...
    99+
    2023-06-29
  • 怎么用css实现基于用户滚动应用
    这篇文章主要介绍怎么用css实现基于用户滚动应用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!   通过将当前滚动偏移映射到html元素上的属性,我们可以根据当前滚动位置设置页面上...
    99+
    2024-04-02
  • 怎么轻松搭建基于Serverless的Go应用
    怎么轻松搭建基于Serverless的Go应用,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。首先介绍下在本文出现的几个比较重要的概念:函数计算(Function Compu...
    99+
    2023-06-04
  • 基于SpringBoot应用监控Actuator安全隐患及解决方式
    概述 微服务作为一项在云中部署应用和服务的新技术是当下比较热门话题,而微服务的特点决定了功能模块的部署是分布式的,运行在不同的机器上相互通过服务调用进行交互,业务流会经过多个微服务的...
    99+
    2024-04-02
  • springboot ErrorPageFilter怎么应用
    本篇内容主要讲解“springboot ErrorPageFilter怎么应用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“springboot ErrorPageFilte...
    99+
    2023-06-29
  • PHP基于MVC的Web应用
    预计更新 第一章:入门 1.1 环境搭建1.2 变量和数据类型1.3 控制流程 第二章:函数 2.1 函数的定义和调用2.2 函数的参数和返回值2.3 匿名函数和闭包 第三章:数组 3.1 数组的基本...
    99+
    2023-08-31
    php mvc 前端
  • 基于RethinkDB +React Native怎么开发Web应用程序
    这篇文章主要讲解了“基于RethinkDB +React Native怎么开发Web应用程序”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“基于RethinkDB +React Native怎...
    99+
    2023-06-17
  • 基于原生JavaScript怎么实现SPA单页应用
    这篇文章主要介绍“基于原生JavaScript怎么实现SPA单页应用”,在日常操作中,相信很多人在基于原生JavaScript怎么实现SPA单页应用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”基于原生Jav...
    99+
    2023-07-05
  • 怎么开发基于Netty的HTTP/HTTPS应用程序
    这篇文章主要介绍“怎么开发基于Netty的HTTP/HTTPS应用程序”,在日常操作中,相信很多人在怎么开发基于Netty的HTTP/HTTPS应用程序问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么开发基...
    99+
    2023-06-20
  • 基于Springboot使用logback的注意事项
    Springboot logback的注意事项 项目使用SpringBoot搭建的,开发环境没有发现问题,日志输出位置也正常。 项目的日志没有使用默认配置文件名方式,而是一个环境一套...
    99+
    2024-04-02
  • 基于SpringBoot使用MyBatis插件的问题
    1:MyBatis MyBatis-Plus为我们提供了强大的mapper和service模板,能够大大的提高开发效率。但是在真正开发过程中,MyBatis-Plus并不能为我们解决...
    99+
    2024-04-02
  • python怎么应用于数据的基础统计分析
    小编给大家分享一下python怎么应用于数据的基础统计分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!python是什么意思Python是一种跨平台的、具有解释性、编译性、互动性和面向对象的脚本语言,其最初的设计是用于编...
    99+
    2023-06-14
  • 基于python怎么使用PyScript
    本文小编为大家详细介绍“基于python怎么使用PyScript”,内容详细,步骤清晰,细节处理妥当,希望这篇“基于python怎么使用PyScript”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。作用PyScr...
    99+
    2023-07-02
  • 基于SpringBoot多线程@Async的使用体验
    目录多线程@Async的使用体验场景1.线程池配置2.子父线程之间共享一个Request的配置方案3.阻塞主线程,等待所有子线程执行完毕后继续执行主线程1.CountDownLatc...
    99+
    2024-04-02
  • SpringBoot怎么集成EasyExcel应用
    这篇文章主要讲解了“SpringBoot怎么集成EasyExcel应用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“SpringBoot怎么集成EasyExcel应用”吧!1、介绍特点:Ja...
    99+
    2023-06-08
  • Springboot基于BCrypt非对称加密字符串怎么实现
    这篇文章主要讲解了“Springboot基于BCrypt非对称加密字符串怎么实现”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Springboot基于BCrypt非对称加密字符串怎么实现”吧...
    99+
    2023-07-05
  • 基于pycharm的beautifulsoup4库怎么用
    这篇文章主要为大家展示了“基于pycharm的beautifulsoup4库怎么用”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“基于pycharm的beautifulsoup4库怎么用”这篇文章...
    99+
    2023-06-26
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作