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

SpringBoot啟動項目時實現(xiàn)加載緩存

 更新時間:2026年02月02日 08:28:51   作者:上下上下左右左右  
在SpringBoot項目啟動時執(zhí)行初始化工作有多種方法,包括使用@PostConstruct注解、實現(xiàn)ApplicationRunner或CommandLineRunner接口,以及監(jiān)聽Spring的生命周期事件,每種方法都有其適用場景和優(yōu)勢,可以根據(jù)具體需求選擇合適的方法

在SpringBoot項目中,執(zhí)行啟動時的初始化工作(如加載緩存)是一個常見的需求??梢酝ㄟ^多種方式實現(xiàn),包括使用@PostConstruct注解、實現(xiàn)ApplicationRunner或CommandLineRunner接口,以及監(jiān)聽Spring的生命周期事件。下面詳細(xì)介紹這些方法,并給出相應(yīng)的代碼示例。

方法一:使用 @PostConstruct 注解

@PostConstruct注解可以用來標(biāo)記一個方法,在依賴注入完成后立即執(zhí)行。

這個方法在Spring容器初始化完畢后自動調(diào)用,非常適合做一些初始化工作。

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class CacheLoader {

    @PostConstruct
    public void loadCache() {
        // 初始化緩存
        System.out.println("Loading cache at startup...");
        // 例如:緩存某些數(shù)據(jù)
    }
}

方法二:實現(xiàn) CommandLineRunner 接口

實現(xiàn)CommandLineRunner接口的run方法,SpringBoot啟動時會調(diào)用這個方法,適合用來執(zhí)行啟動時的初始化任務(wù)。

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class CacheLoader implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        // 初始化緩存
        System.out.println("Loading cache at startup...");
        // 例如:緩存某些數(shù)據(jù)
    }
}

方法三:實現(xiàn) ApplicationRunner 接口

與CommandLineRunner類似,ApplicationRunner接口也是在SpringBoot啟動完成后調(diào)用,但提供了更為豐富的ApplicationArguments對象,可以用于處理傳入的命令行參數(shù)。

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class CacheLoader implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 初始化緩存
        System.out.println("Loading cache at startup...");
        // 例如:緩存某些數(shù)據(jù)
    }
}

方法四:監(jiān)聽 Spring 的生命周期事件

通過實現(xiàn)ApplicationListener接口或使用@EventListener注解,監(jiān)聽 ApplicationReadyEvent事件,在SpringBoot啟動完成后執(zhí)行初始化工作。

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@Component
public class CacheLoader implements ApplicationListener<ApplicationReadyEvent> {
    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
        // 初始化緩存
        System.out.println("Loading cache at startup...");
        // 例如:緩存某些數(shù)據(jù)
    }
}

或者使用 @EventListener 注解:

import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
@Component
public class CacheLoader {
    @EventListener(ApplicationReadyEvent.class)
    public void onApplicationReady() {
        // 初始化緩存
        System.out.println("Loading cache at startup...");
        // 例如:緩存某些數(shù)據(jù)
    }
}

選擇合適的方法

選擇哪種方法取決于具體的需求和使用場景:

  • 如果初始化工作是一個Bean的屬性,則推薦使用@PostConstruct注解。
  • 如果需要訪問命令行參數(shù),推薦使用ApplicationRunner或CommandLineRunner。
  • 如果需要監(jiān)聽?wèi)?yīng)用上下文事件,推薦使用ApplicationListener或 @EventListener。

示例:加載緩存數(shù)據(jù)

假設(shè)我們要在應(yīng)用啟動時加載一些用戶數(shù)據(jù)到緩存中,以下是一個完整的示例:

示例代碼

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
public class CacheLoader implements CommandLineRunner {
    private Map<Integer, String> userCache = new HashMap<>();
    @Override
    public void run(String... args) throws Exception {
        // 模擬從數(shù)據(jù)庫加載用戶數(shù)據(jù)到緩存
        userCache.put(1, "Alice");
        userCache.put(2, "Bob");
        userCache.put(3, "Charlie");
        System.out.println("User cache loaded at startup: " + userCache);
    }
    public String getUserById(int userId) {
        return userCache.get(userId);
    }
}

測試類

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CacheLoaderApplication implements CommandLineRunner {
    @Autowired
    private CacheLoader cacheLoader;
    public static void main(String[] args) {
        SpringApplication.run(CacheLoaderApplication.class, args);
    }
    @Override
    public void run(String... args) throws Exception {
        // 測試緩存數(shù)據(jù)
        System.out.println("User with ID 1: " + cacheLoader.getUserById(1));
        System.out.println("User with ID 2: " + cacheLoader.getUserById(2));
        System.out.println("User with ID 3: " + cacheLoader.getUserById(3));
    }
}

通過以上方式,可以確保在SpringBoot項目啟動時執(zhí)行必要的初始化工作,如加載緩存數(shù)據(jù),從而提高應(yīng)用的性能和響應(yīng)速度。

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java 生成簽名證書的實現(xiàn)示例

    Java 生成簽名證書的實現(xiàn)示例

    本文主要介紹了Java 生成簽名證書的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-09-09
  • 小白也可以學(xué)會的Java NIO的Write事件

    小白也可以學(xué)會的Java NIO的Write事件

    剛開始對NIO的寫操作理解的不深,不知道為什么要注冊寫事件,何時注冊寫事件,為什么寫完之后要取消注冊寫事件,今天特地整理了本篇文章,需要的朋友可以參考下
    2021-06-06
  • 一篇文章帶你入門Java變量及整形

    一篇文章帶你入門Java變量及整形

    這篇文章主要介紹了簡單了解JAVA變量類型及代碼實例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2021-08-08
  • JDK安裝配置教程

    JDK安裝配置教程

    這篇文章主要為大家詳細(xì)介紹了JDK安裝配置教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Spring Boot命令行啟動添加參數(shù)的三種方式

    Spring Boot命令行啟動添加參數(shù)的三種方式

    在命令行中,常見的參數(shù)可以分為三類:選項參數(shù)、非選項參數(shù)和系統(tǒng)參數(shù),本文就來介紹一下Spring Boot命令行三種參數(shù)形式,感興趣的可以了解一下
    2023-09-09
  • 詳解Spring ApplicationContext加載過程

    詳解Spring ApplicationContext加載過程

    這篇文章主要介紹了Spring ApplicationContext加載過程的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下
    2021-03-03
  • Java中獲取List中最后一個元素的三種方法

    Java中獲取List中最后一個元素的三種方法

    在Java編程中我們經(jīng)常需要獲取一個List集合中的最后一個元素,這篇文章主要給大家介紹了關(guān)于Java中獲取List中最后一個元素的三種方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-12-12
  • 詳解Java中ThreadLocal類型及簡單用法

    詳解Java中ThreadLocal類型及簡單用法

    ThreadLocal實例通常是希望將狀態(tài)與線程關(guān)聯(lián)起來的類中的私有靜態(tài)字段,下面通過例子給大家詳細(xì)介紹Java中ThreadLocal類型及簡單用法,感興趣的朋友跟隨小編一起看看吧
    2021-10-10
  • 解決Android Studio安裝后運行出錯dose not...和Internal error...

    解決Android Studio安裝后運行出錯dose not...和Internal error...

    這篇文章主要介紹了解決Android Studio安裝后運行出錯dose not...和Internal error...的相關(guān)資料,需要的朋友可以參考下
    2017-03-03
  • Java開發(fā)之內(nèi)部類對象的創(chuàng)建及hook機(jī)制分析

    Java開發(fā)之內(nèi)部類對象的創(chuàng)建及hook機(jī)制分析

    這篇文章主要介紹了Java開發(fā)之內(nèi)部類對象的創(chuàng)建及hook機(jī)制,結(jié)合實例形式分析了java基于hook機(jī)制內(nèi)部類對象的創(chuàng)建與使用,需要的朋友可以參考下
    2018-01-01

最新評論

将乐县| 大连市| 南康市| 北辰区| 延长县| 大荔县| 泾阳县| 广宗县| 乾安县| 东乡族自治县| 望谟县| 威海市| 泗洪县| 金门县| 邳州市| 永嘉县| 玛曲县| 招远市| 黄大仙区| 正宁县| 平果县| 河西区| 兰坪| 芦溪县| 汶川县| 墨江| 乌拉特后旗| 海林市| 沧州市| 舟山市| 普宁市| 富蕴县| 利辛县| 瓦房店市| 龙游县| 牙克石市| 礼泉县| 建德市| 息烽县| 册亨县| 恩施市|