最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot實(shí)現(xiàn)事件監(jiān)聽(異步執(zhí)行)的示例代碼

 更新時(shí)間:2024年08月15日 09:49:46   作者:Mxin5  
事件監(jiān)聽是一種機(jī)制,可以定義和觸發(fā)自定義的事件,以及在應(yīng)用程序中注冊(cè)監(jiān)聽器來響應(yīng)這些事件,本文主要介紹了SpringBoot實(shí)現(xiàn)事件監(jiān)聽(異步執(zhí)行)的示例代碼,感興趣的可以了解一下

在Spring Boot中,事件監(jiān)聽是一種機(jī)制,通過該機(jī)制,你可以定義和觸發(fā)自定義的事件,以及在應(yīng)用程序中注冊(cè)監(jiān)聽器來響應(yīng)這些事件,提供了一種解耦的方式來處理應(yīng)用程序中的事件。

事件監(jiān)聽的主要組件包括:

事件(Event):

  • 事件是一個(gè)普通的Java對(duì)象,用于封裝與應(yīng)用程序中發(fā)生的某個(gè)動(dòng)作或狀態(tài)變化相關(guān)的信息。
  • 事件發(fā)布器(Event Publisher):

    • 事件發(fā)布器是一個(gè)負(fù)責(zé)將事件發(fā)布給注冊(cè)的監(jiān)聽器的組件。在Spring中,ApplicationEventPublisher接口定義了事件發(fā)布器的標(biāo)準(zhǔn)。

事件監(jiān)聽器(Event Listener):

  • 事件監(jiān)聽器是用于監(jiān)聽和響應(yīng)特定事件的組件。在Spring中,通過ApplicationListener接口或使用@EventListener注解來定義事件監(jiān)聽器。

在Spring Boot中實(shí)現(xiàn)事件監(jiān)聽的步驟如下:

定義事件類:

  • 創(chuàng)建一個(gè)普通的Java類,用于表示特定的事件。該類通常繼承自ApplicationEvent或其子類。

定義事件發(fā)布器(可選):

  • 可以在需要的地方注入ApplicationEventPublisher并使用它來發(fā)布事件,或者直接通過Spring容器(ApplicationContext)發(fā)布事件。

定義事件監(jiān)聽器:

  • 創(chuàng)建一個(gè)實(shí)現(xiàn)ApplicationListener接口或使用@EventListener注解的類,用于監(jiān)聽特定的事件,并在事件發(fā)生時(shí)執(zhí)行相應(yīng)的邏輯。

注冊(cè)監(jiān)聽器:

  • 將事件監(jiān)聽器注冊(cè)到Spring容器中,可以通過注解、Java配置或XML配置來完成。

以下是一個(gè)簡(jiǎn)單的示例,演示了如何在Spring Boot中實(shí)現(xiàn)事件監(jiān)聽(主要代碼展示):

代碼層級(jí)結(jié)構(gòu):

1.自定義事件類 CoursesTestEvent繼承ApplicationEvent

package com.example.springbootredis.event;

import lombok.Getter;
import lombok.Setter;
import org.springframework.context.ApplicationEvent;

/**
 * 課程事件類,繼承自 ApplicationEvent,表示課程相關(guān)的事件。
 */
@Setter
@Getter
public class CoursesTestEvent extends ApplicationEvent {

    private Integer id;
    /**
     * 課程標(biāo)題
     */
    private String title;
    /**
     * 課程封面
     */
    private String thumb;
    /**
     * 課程價(jià)格(分)
     */
    private Integer charge;
    /**
     * 隨便傳遞幾個(gè)參數(shù)
     * */
    public CoursesTestEvent(Object source, String title, String thumb) {
        super(source);
        this.title = title;
        this.thumb = thumb;
    }

}

2.創(chuàng)建一個(gè)事件監(jiān)聽器類 CoursesTestListener:

package com.example.springbootredis.listener;

import com.example.springbootredis.event.CoursesTestEvent;
import com.example.springbootredis.service.CoursesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 用于測(cè)試監(jiān)聽事務(wù),異步執(zhí)行方法
 * 課程事件監(jiān)聽器類,用于異步更新課程信息。
 */
@Component
@Slf4j
public class CoursesTestListener {

    //根據(jù)實(shí)際的需求進(jìn)行注入
    @Autowired
    private CoursesService coursesService;

    /**
     * 異步事件監(jiān)聽方法,用于監(jiān)聽CoursesTestEvent進(jìn)行更新課程相關(guān)信息。
     * @param event 觸發(fā)的課程的事件。
     */
//    @Async("myTaskExecutor") // 異步執(zhí)行的注解,線程池
//    @Async() // 異步執(zhí)行的注解
    @EventListener  // 事件監(jiān)聽器的注解
    public void updateLoginInfo(CoursesTestEvent event)  {
        //檢查是否能夠獲取到CoursesTestEvent
        System.out.println("title:"+event.getTitle());
        System.out.println("thumb:"+event.getThumb());
        System.out.println(3);
        // 打印當(dāng)前線程的信息
        System.out.println("執(zhí)行當(dāng)前線程的名稱3: " + Thread.currentThread().getName());
    }
}

3.在業(yè)務(wù)邏輯中進(jìn)行測(cè)試事件監(jiān)聽:

package com.example.springbootredis.service.impl;

import com.example.springbootredis.domain.Courses;
import com.example.springbootredis.event.CoursesTestEvent;
import com.example.springbootredis.mapper.CoursesMapper;
import com.example.springbootredis.service.CoursesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@Slf4j
public class CoursesServiceImpl implements CoursesService {

    @Autowired
    private CoursesMapper coursesMapper;

    //用于管理和維護(hù)Bean以及處理Bean之間依賴關(guān)系的核心容器。
    @Autowired
    private ApplicationContext applicationContext;

    //進(jìn)行異步測(cè)試
    @Override
    public List<Courses> asyTest() {

        List<Courses> courses = coursesMapper.findAll();
        System.out.println(1);
        // 打印當(dāng)前線程的信息
        System.out.println("執(zhí)行當(dāng)前線程的名稱1: " + Thread.currentThread().getName());
        // 發(fā)布自定義的課程測(cè)試事件
        applicationContext.publishEvent(new CoursesTestEvent(this,courses.get(0).getTitle(),courses.get(0).getThumb()));
        System.out.println(2);
        System.out.println("執(zhí)行當(dāng)前線程的名稱2: " + Thread.currentThread().getName());
        return courses;
    }

}

4.代碼執(zhí)行結(jié)果(沒有使用異步):

測(cè)試達(dá)到了監(jiān)聽的效果了,但是都是同一個(gè)線程執(zhí)行,按照順序進(jìn)行執(zhí)行,沒有達(dá)到異步的效果。為了增加響應(yīng)的效率,對(duì)監(jiān)聽事件進(jìn)行異步的執(zhí)行。

Spring Boot的異步任務(wù)通常使用以下幾個(gè)核心注解:

@EnableAsync:在Spring Boot應(yīng)用程序的配置類上添加@EnableAsync注解,以啟用異步任務(wù)支持。這樣Spring會(huì)為異步方法創(chuàng)建一個(gè)代理,允許它們?cè)趩为?dú)的線程中執(zhí)行。

package com.example.springbootredis;


import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

@SpringBootApplication
@MapperScan("com.example.springbootredis.mapper")
@EnableAsync //開啟異步任務(wù)支持(主要)
public class SpringbootRedisApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootRedisApplication.class, args);
    }

}

@Async:在需要異步執(zhí)行的方法上添加@Async注解。這告訴Spring框架將這個(gè)方法的調(diào)用包裝在一個(gè)新的線程中執(zhí)行。

package com.example.springbootredis.listener;

import com.example.springbootredis.event.CoursesTestEvent;
import com.example.springbootredis.service.CoursesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * 用于測(cè)試監(jiān)聽事務(wù),異步執(zhí)行方法
 * 課程事件監(jiān)聽器類,用于異步更新課程信息。
 */
@Component
@Slf4j
public class CoursesTestListener {

    //根據(jù)實(shí)際的需求進(jìn)行注入
    @Autowired
    private CoursesService coursesService;

    /**
     * 異步事件監(jiān)聽方法,用于監(jiān)聽CoursesTestEvent進(jìn)行更新課程相關(guān)信息。
     * @param event 觸發(fā)的課程的事件。
     */
//    @Async("myTaskExecutor") // 異步執(zhí)行的注解,線程池
    @Async() // 異步執(zhí)行的注解
    @EventListener  // 事件監(jiān)聽器的注解
    public void updateLoginInfo(CoursesTestEvent event)  {
        //檢查是否能夠獲取到CoursesTestEvent
        System.out.println("title:"+event.getTitle());
        System.out.println("thumb:"+event.getThumb());
        System.out.println(3);
        // 打印當(dāng)前線程的信息
        System.out.println("執(zhí)行當(dāng)前線程的名稱3: " + Thread.currentThread().getName());
    }
}

再進(jìn)行測(cè)試(異步):

這樣就達(dá)到異步的效果了,對(duì)監(jiān)聽事件進(jìn)行異步執(zhí)行。如果想直接進(jìn)行測(cè)試,下面是gitee地址:創(chuàng)建一個(gè)數(shù)據(jù)庫將courses.sql文件進(jìn)行執(zhí)行,啟動(dòng)即可測(cè)試:https://gitee.com/sophisticatedxin/springboot-asy-demo.git

到此這篇關(guān)于SpringBoot實(shí)現(xiàn)事件監(jiān)聽(異步執(zhí)行)的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot 事件監(jiān)聽內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Apache?Commons?CLI構(gòu)建命令行應(yīng)用利器教程

    Apache?Commons?CLI構(gòu)建命令行應(yīng)用利器教程

    這篇文章主要為大家介紹了構(gòu)建命令行應(yīng)用利器Apache?Commons?CLI的使用教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • java將html轉(zhuǎn)成圖片代碼實(shí)例(html2image)

    java將html轉(zhuǎn)成圖片代碼實(shí)例(html2image)

    這篇文章主要介紹了java將html轉(zhuǎn)成圖片的相關(guān)資料,在Java開發(fā)中,將HTML轉(zhuǎn)換為圖片可以使用html2image庫,文中通過代碼及圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2024-09-09
  • 一文帶你秒懂Java為什么只有值傳遞

    一文帶你秒懂Java為什么只有值傳遞

    值傳遞是指在調(diào)用函數(shù)時(shí)將實(shí)際參數(shù)復(fù)制一份傳遞到函數(shù)中,引用傳遞是指在調(diào)用函數(shù)時(shí)將實(shí)際參數(shù)的引用直接傳遞到函數(shù)中,本文將詳細(xì)介紹Java中值傳遞的相關(guān)知識(shí),感興趣的可以了解下
    2024-11-11
  • SpringBoot中實(shí)現(xiàn)啟動(dòng)任務(wù)的實(shí)現(xiàn)步驟

    SpringBoot中實(shí)現(xiàn)啟動(dòng)任務(wù)的實(shí)現(xiàn)步驟

    這篇文章主要介紹了SpringBoot中實(shí)現(xiàn)啟動(dòng)任務(wù)的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Spring與Redis集成的正確方式流程詳解

    Spring與Redis集成的正確方式流程詳解

    這篇文章主要為大家介紹了Spring與Redis集成的正確方式流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • 一文學(xué)會(huì)Java運(yùn)算符的使用

    一文學(xué)會(huì)Java運(yùn)算符的使用

    計(jì)算機(jī)的最基本用途之一就是執(zhí)行數(shù)學(xué)運(yùn)算,作為一門計(jì)算機(jī)語言,Java也提供了一套豐富的運(yùn)算符來操縱變量,本篇通過示例詳細(xì)講解了Java中不同運(yùn)算符的使用,需要的朋友可以參考下
    2022-05-05
  • Java圖形界面Swing原理及用法解析

    Java圖形界面Swing原理及用法解析

    這篇文章主要介紹了Java圖形界面Swing原理及用法解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-10-10
  • Spring Boot 約定大于配置之如何實(shí)現(xiàn)自定義配置

    Spring Boot 約定大于配置之如何實(shí)現(xiàn)自定義配置

    本文介紹了SpringBoot的“約定大于配置”理念以及如何實(shí)現(xiàn)自定義配置,通過實(shí)現(xiàn)接口和添加@Configuration注解,開發(fā)者可以靈活地?cái)U(kuò)展和定制SpringBoot的默認(rèn)行為,感興趣的朋友一起看看吧
    2025-03-03
  • Springboot如何通過流返回文件

    Springboot如何通過流返回文件

    這篇文章主要介紹了Springboot如何通過流返回文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java Durid進(jìn)行JDBC連接詳解

    Java Durid進(jìn)行JDBC連接詳解

    今天給大家?guī)淼氖顷P(guān)于Java的相關(guān)知識(shí),文章簡(jiǎn)單使用Durid進(jìn)行JDBC連接,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下
    2021-09-09

最新評(píng)論

兴业县| 永兴县| 湖口县| 屏东县| 汾阳市| 井研县| 甘德县| 克什克腾旗| 岳西县| 会宁县| 正定县| 榆中县| 定西市| 康马县| 电白县| 青岛市| 安岳县| 大余县| 扶绥县| 高安市| 灵川县| 南阳市| 庐江县| 镇平县| 乡城县| 鄂托克前旗| 老河口市| 开封县| 容城县| 丰镇市| 平阳县| 惠安县| 凯里市| 湾仔区| 阿鲁科尔沁旗| 双辽市| 左云县| 兴国县| 鹰潭市| 怀远县| 鸡西市|