SpringBoot啟動(dòng)項(xiàng)目時(shí)實(shí)現(xiàn)加載緩存
在SpringBoot項(xiàng)目中,執(zhí)行啟動(dòng)時(shí)的初始化工作(如加載緩存)是一個(gè)常見(jiàn)的需求??梢酝ㄟ^(guò)多種方式實(shí)現(xiàn),包括使用@PostConstruct注解、實(shí)現(xiàn)ApplicationRunner或CommandLineRunner接口,以及監(jiān)聽(tīng)Spring的生命周期事件。下面詳細(xì)介紹這些方法,并給出相應(yīng)的代碼示例。
方法一:使用 @PostConstruct 注解
@PostConstruct注解可以用來(lái)標(biāo)記一個(gè)方法,在依賴注入完成后立即執(zhí)行。
這個(gè)方法在Spring容器初始化完畢后自動(dòng)調(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ù)
}
}
方法二:實(shí)現(xiàn) CommandLineRunner 接口
實(shí)現(xiàn)CommandLineRunner接口的run方法,SpringBoot啟動(dòng)時(shí)會(huì)調(diào)用這個(gè)方法,適合用來(lái)執(zhí)行啟動(dòng)時(shí)的初始化任務(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ù)
}
}
方法三:實(shí)現(xiàn) ApplicationRunner 接口
與CommandLineRunner類似,ApplicationRunner接口也是在SpringBoot啟動(dòng)完成后調(diào)用,但提供了更為豐富的ApplicationArguments對(duì)象,可以用于處理傳入的命令行參數(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)聽(tīng) Spring 的生命周期事件
通過(guò)實(shí)現(xiàn)ApplicationListener接口或使用@EventListener注解,監(jiān)聽(tīng) ApplicationReadyEvent事件,在SpringBoot啟動(dòng)完成后執(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ù)
}
}
選擇合適的方法
選擇哪種方法取決于具體的需求和使用場(chǎng)景:
- 如果初始化工作是一個(gè)Bean的屬性,則推薦使用@PostConstruct注解。
- 如果需要訪問(wèn)命令行參數(shù),推薦使用ApplicationRunner或CommandLineRunner。
- 如果需要監(jiān)聽(tīng)?wèi)?yīng)用上下文事件,推薦使用ApplicationListener或 @EventListener。
示例:加載緩存數(shù)據(jù)
假設(shè)我們要在應(yīng)用啟動(dòng)時(shí)加載一些用戶數(shù)據(jù)到緩存中,以下是一個(gè)完整的示例:
示例代碼
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ù)庫(kù)加載用戶數(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);
}
}
測(cè)試類
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 {
// 測(cè)試緩存數(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));
}
}
通過(guò)以上方式,可以確保在SpringBoot項(xiàng)目啟動(dòng)時(shí)執(zhí)行必要的初始化工作,如加載緩存數(shù)據(jù),從而提高應(yīng)用的性能和響應(yīng)速度。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 生成簽名證書(shū)的實(shí)現(xiàn)示例
本文主要介紹了Java 生成簽名證書(shū)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-09-09
小白也可以學(xué)會(huì)的Java NIO的Write事件
剛開(kāi)始對(duì)NIO的寫操作理解的不深,不知道為什么要注冊(cè)寫事件,何時(shí)注冊(cè)寫事件,為什么寫完之后要取消注冊(cè)寫事件,今天特地整理了本篇文章,需要的朋友可以參考下2021-06-06
Spring Boot命令行啟動(dòng)添加參數(shù)的三種方式
在命令行中,常見(jiàn)的參數(shù)可以分為三類:選項(xiàng)參數(shù)、非選項(xiàng)參數(shù)和系統(tǒng)參數(shù),本文就來(lái)介紹一下Spring Boot命令行三種參數(shù)形式,感興趣的可以了解一下2023-09-09
詳解Spring ApplicationContext加載過(guò)程
這篇文章主要介紹了Spring ApplicationContext加載過(guò)程的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用spring框架,感興趣的朋友可以了解下2021-03-03
詳解Java中ThreadLocal類型及簡(jiǎn)單用法
ThreadLocal實(shí)例通常是希望將狀態(tài)與線程關(guān)聯(lián)起來(lái)的類中的私有靜態(tài)字段,下面通過(guò)例子給大家詳細(xì)介紹Java中ThreadLocal類型及簡(jiǎn)單用法,感興趣的朋友跟隨小編一起看看吧2021-10-10
解決Android Studio安裝后運(yùn)行出錯(cuò)dose not...和Internal error...
這篇文章主要介紹了解決Android Studio安裝后運(yùn)行出錯(cuò)dose not...和Internal error...的相關(guān)資料,需要的朋友可以參考下2017-03-03
Java開(kāi)發(fā)之內(nèi)部類對(duì)象的創(chuàng)建及hook機(jī)制分析
這篇文章主要介紹了Java開(kāi)發(fā)之內(nèi)部類對(duì)象的創(chuàng)建及hook機(jī)制,結(jié)合實(shí)例形式分析了java基于hook機(jī)制內(nèi)部類對(duì)象的創(chuàng)建與使用,需要的朋友可以參考下2018-01-01

