首页 > 其他分享 >spring 使用 事件机制

spring 使用 事件机制

时间:2024-06-19 15:11:42浏览次数:18  
标签:applicationEventPublisher spring void 事件 article knowledgeBaseArticleRepository 

概述

在编写代码的时候,比如我删除一篇文章,这个时候,如果我想做些额外的逻辑,这是就需要修改删除部分的代码。spring 提供了事件机制更优雅的实现这个,用户只需要实现事件监听即可。

代码实现

注入发布者

public class KnowledgeBaseService implements ApplicationEventPublisherAware {

  private final KnowledgeBaseArticleRepository knowledgeBaseArticleRepository;
  private ApplicationEventPublisher applicationEventPublisher;

  public KnowledgeBaseService(
      KnowledgeBaseArticleRepository knowledgeBaseArticleRepository) {
    this.knowledgeBaseArticleRepository = knowledgeBaseArticleRepository;
  }
  // 注入事件发布者
  @Override
  public void setApplicationEventPublisher(
      @NotNull ApplicationEventPublisher applicationEventPublisher) {
    this.applicationEventPublisher = applicationEventPublisher;
  }

发布事件

public void deleteArticle(UUID id) {
    knowledgeBaseArticleRepository.findById(id).ifPresent(article -> {
      knowledgeBaseArticleRepository.delete(article);
      applicationEventPublisher.publishEvent(
          new KnowledgeBaseArticleDeletedEvent(
              article.toKnowledgeBaseArticle()));
    });
  }

处理事件

@EventListener
  @Async
  public void handleArticleDeleted(KnowledgeBaseArticleDeletedEvent event) {
    documentLoader.deleteDocumentsByArticle(
        (KnowledgeBaseArticle) event.getSource());
  }

标签:applicationEventPublisher,spring,void,事件,article,knowledgeBaseArticleRepository,
From: https://www.cnblogs.com/yg_zhang/p/18256296

相关文章

  • 大脑如何通过DNA损伤与修复机制来巩固记忆?
    近日《自然》杂志刊登了一篇重磅论文,揭示了记忆形成过程中的一个关键机制——大脑如何通过DNA损伤和修复机制来巩固记忆。这一发现为我们理解记忆形成提供了全新的视角,也可能为治疗某些记忆障碍疾病提供新的思路。记忆是我们大脑中非常重要的功能之一,它使我们能够存储过去......
  • [com.t.extend.SpringContextLoaderListener] - generate index.html sucess,ERROR or
    错误:2024-06-1913:23:09,873INFO[com.t.extend.SpringContextLoaderListener]-generateindex.htmlsucess13:23:10.159[RMITCPConnection(3)-127.0.0.1]ERRORorg.apache.struts2.dispatcher.Dispatcher-Dispatcherinitializationfailedcom.opensymphony.xwor......
  • Springboot 集成 Shardingsphere-JDBC
    Springboot集成Shardingsphere-JDBCShardingsphere系列目录:背景调研前提新增依赖分表策略简单分库分表策略垂直分库广播表水平分库(单表)水平分库(多表)水平分表HINT配置逻辑代码自定义分库分表(精准定位+范围查询)配置代码精准定位数据库精准定位+范围查询表代码仓......
  • SpringData初步学习-连接MySQL数据库
    1.添加mysql驱动和spring-data-jpa依赖<dependencies><!--SpringDataJPA--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId><......
  • java spring-data-jpa 使用方法
    SpringDataJPA是Spring生态系统的一部分,它提供了对JPA(JavaPersistenceAPI)的抽象,简化了数据访问层的开发。以下是使用SpringDataJPA的基本步骤和一些核心概念:1.添加依赖        在Maven项目的pom.xml文件中添加SpringDataJPA和相关数据库驱......
  • 基于SpringBoot+Vue的高校爱心捐赠系统设计与实现(源码+lw+部署+讲解)
    文章目录前言详细视频演示具体实现截图技术可行性分析技术简介后端框架SpringBoot前端框架Vue系统开发平台系统架构设计业务流程分析为什么选择我们自己的公众号(一点毕设)海量实战案例代码参考数据库参考源码及文档获取前言......
  • springboot框架怎么用?是什么?
    SpringBoot框架是一个用于简化Spring应用初始搭建以及开发过程的全新框架,旨在通过特定的配置方式,使开发人员不再需要定义样板化的配置,从而加速应用开发。以下是关于SpringBoot框架的详细使用和介绍:1.SpringBoot框架的使用安装Java开发环境:首先,确保已经正确配置了JDK。......
  • [转帖]springboot中Hikari连接池常用参数含义(一)
    <divid="content_views"class="htmledit_views"><p>yml配置<br><imgalt=""height="235"src="https://img-blog.csdnimg.cn/7724916bc5d449b48114ed52462ba48d.png"......
  • Spring容器系列-FactoryBean使用/原理
    Spring-FactoryBean使用/原理  概要  在某些情况下,实例化Bean过程比较复杂,若按照传统的方式,则需要在中提供大量的配置信息,不够灵活,这时采用编码的方式能得到一个简单的方案。  Spring为此提供了一个org.springframework.bean.factory.FactoryBean的工厂类接口,用户......
  • SpringBoot整合JWT(JSON Web Token)生成token与验证
    目录JWT什么是JWTJWT使用流程确定要传递的信息:生成JWT:JWT传输:客户端保存JWT:客户端发送JWT:服务器验证JWT:服务器响应:Token的使用示例:工具类R结果集返回一个生成的token创建拦截器JWT什么是JWTJWT(JSONWebToken)是是目前最流行的跨域认证解决方案。它通常被......