SpringBoot項(xiàng)目啟動數(shù)據(jù)加載內(nèi)存的三種方法
一、前言
一般來說,SpringBoot工程環(huán)境配置放在properties文件中,啟動的時(shí)候?qū)⒐こ讨械膒roperties/yaml文件的配置項(xiàng)加載到內(nèi)存中。但這種方式改配置項(xiàng)的時(shí)候,需要重新編譯部署,考慮到這種因素,今天介紹將配置項(xiàng)存到數(shù)據(jù)庫表中,在工程啟動時(shí)把配置項(xiàng)加載到內(nèi)存中。
SpringBoot提供了兩個接口: CommandLineRunner 和 ApplicationRunner 。實(shí)現(xiàn)其中接口,就可以在工程啟動時(shí)將數(shù)據(jù)庫中的數(shù)據(jù)加載到內(nèi)存。使用的場景有:加載配置項(xiàng)到內(nèi)存中;啟動時(shí)將字典或白名單數(shù)據(jù)加載到內(nèi)存(或緩存到Redis中)。
二、加載方式
第一種:使用@PostConstruct注解(properties/yaml文件)。
第二種:使用@Order注解和CommandLineRunner接口。
第三種:使用@Order注解和ApplicationRunner接口。
注意事項(xiàng)
第二種和第三種,二者的官方j(luò)avadoc一樣,區(qū)別在于接收的參數(shù)不一樣。CommandLineRunner的參數(shù)是最原始的參數(shù),沒有做任何處理。ApplicationRunner的參數(shù)是ApplicationArguments,是對原始參數(shù)做了進(jìn)一步的封裝。
三、代碼示例
3.1 使用@PostConstruct注解
package com.example.demo.config;
import com.example.demo.service.ICodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class InitData1 {
public static Map<Integer, String> codeMap = new HashMap<Integer, String>();
@Autowired
private ICodeService codeService;
@PostConstruct
public void init() {
System.out.println("示例1:加載codeMap中......");
// 查詢數(shù)據(jù)庫數(shù)據(jù)
List<String> codeList = codeService.listAll();
for (int i = 0; i < codeList.size(); i++) {
codeMap.put(i, codeList.get(i));
}
}
@PreDestroy
public void destroy() {
System.out.println("系統(tǒng)啟動成功,codeMap加載完成!");
}
}3.2 CommandLineRunner接口
package com.example.demo.config;
import com.example.demo.service.ICodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
@Order(1) // 初始化加載優(yōu)先級,數(shù)字越小優(yōu)先級越高
public class InitData2 implements CommandLineRunner {
public static Map<Integer, String> codeMap = new HashMap<Integer, String>();
@Autowired
private ICodeService codeService;
@Override
public void run(String... args) throws Exception {
System.out.println("示例2:加載codeMap中......");
// 查詢數(shù)據(jù)庫數(shù)據(jù)
List<String> codeList = codeService.listAll();
for (int i = 0; i < codeList.size(); i++) {
codeMap.put(i, codeList.get(i));
}
}
}3.3 ApplicationRunner接口
package com.example.demo.config;
import com.example.demo.service.ICodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
@Order(1) // 初始化加載優(yōu)先級,數(shù)字越小優(yōu)先級越高
public class InitData3 implements ApplicationRunner {
public static Map<Integer, String> codeMap = new HashMap<Integer, String>();
@Autowired
private ICodeService codeService;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println("示例3:加載codeMap中......");
// 查詢數(shù)據(jù)庫數(shù)據(jù)
List<String> codeList = codeService.listAll();
for (int i = 0; i < codeList.size(); i++) {
codeMap.put(i, codeList.get(i));
}
}
}四、總結(jié)
1、CommandLineRunner和ApplicationRunner調(diào)用的時(shí)機(jī)是在容器初始化完成之后,立即調(diào)用。
2、CommandLineRunner和ApplicationRunner使用上沒有區(qū)別,唯一區(qū)別是CommandLineRunner接受字符串?dāng)?shù)組參數(shù),需要自行解析出健和值,ApplicationRunner的參數(shù)是ApplicationArguments,是對原始參數(shù)做了進(jìn)一步的封裝。
3、兩個接口都可以使用 @Order 參數(shù),支持工程啟動后根據(jù)order 聲明的權(quán)重值來決定調(diào)用的順序(數(shù)字越小,優(yōu)先級越高)。
以上就是SpringBoot項(xiàng)目啟動數(shù)據(jù)加載內(nèi)存中的三種方法的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot數(shù)據(jù)加載內(nèi)存的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
mybatis-plus自帶QueryWrapper自定義sql實(shí)現(xiàn)復(fù)雜查詢實(shí)例詳解
MyBatis-Plus是一個MyBatis(opens new window)的增強(qiáng)工具,在 MyBatis的基礎(chǔ)上只做增強(qiáng)不做改變,MyBatis可以無損升級為MyBatis-Plus,這篇文章主要給大家介紹了關(guān)于mybatis-plus自帶QueryWrapper自定義sql實(shí)現(xiàn)復(fù)雜查詢的相關(guān)資料,需要的朋友可以參考下2022-10-10
Java實(shí)戰(zhàn)花店商城系統(tǒng)的實(shí)現(xiàn)流程
只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+Maven+mybatis+Vue+Mysql實(shí)現(xiàn)一個花店商城系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平2022-01-01
Java利用docx4j+Freemarker生成word文檔
這篇文章主要為大家詳細(xì)介紹了Java如何利用docx4j+Freemarker生成word文檔,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-04-04
SpringBoot實(shí)現(xiàn)動態(tài)加載外部Jar流程詳解
這篇文章主要介紹了SpringBoot動態(tài)加載外部Jar的流程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-05-05

