SpringBoot項(xiàng)目啟動(dòng)數(shù)據(jù)加載內(nèi)存的三種方法
一、前言
一般來說,SpringBoot工程環(huán)境配置放在properties文件中,啟動(dòng)的時(shí)候?qū)⒐こ讨械膒roperties/yaml文件的配置項(xiàng)加載到內(nèi)存中。但這種方式改配置項(xiàng)的時(shí)候,需要重新編譯部署,考慮到這種因素,今天介紹將配置項(xiàng)存到數(shù)據(jù)庫(kù)表中,在工程啟動(dòng)時(shí)把配置項(xiàng)加載到內(nèi)存中。
SpringBoot提供了兩個(gè)接口: CommandLineRunner 和 ApplicationRunner 。實(shí)現(xiàn)其中接口,就可以在工程啟動(dòng)時(shí)將數(shù)據(jù)庫(kù)中的數(shù)據(jù)加載到內(nèi)存。使用的場(chǎng)景有:加載配置項(xiàng)到內(nèi)存中;啟動(dòng)時(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,是對(duì)原始參數(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ù)庫(kù)數(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)啟動(dò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)先級(jí),數(shù)字越小優(yōu)先級(jí)越高 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ù)庫(kù)數(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)先級(jí),數(shù)字越小優(yōu)先級(jí)越高 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ù)庫(kù)數(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,是對(duì)原始參數(shù)做了進(jìn)一步的封裝。
3、兩個(gè)接口都可以使用 @Order 參數(shù),支持工程啟動(dòng)后根據(jù)order 聲明的權(quán)重值來決定調(diào)用的順序(數(shù)字越小,優(yōu)先級(jí)越高)。
以上就是SpringBoot項(xiàng)目啟動(dòng)數(shù)據(jù)加載內(nèi)存中的三種方法的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot數(shù)據(jù)加載內(nèi)存的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot項(xiàng)目啟動(dòng)時(shí)提示程序包不存在和找不到符號(hào)的處理方法
- springboot項(xiàng)目啟動(dòng)類錯(cuò)誤(找不到或無法加載主類 com.**Application)
- 解決springboot項(xiàng)目啟動(dòng)報(bào)錯(cuò)Error creating bean with name dataSourceScriptDatabaseInitializer問題
- 解決springboot項(xiàng)目啟動(dòng)失敗Could not initialize class com.fasterxml.jackson.databind.ObjectMapper問題
相關(guān)文章
Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決
這篇文章主要介紹了Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01MyBatis實(shí)現(xiàn)批量插入數(shù)據(jù),多重forEach循環(huán)
這篇文章主要介紹了MyBatis實(shí)現(xiàn)批量插入數(shù)據(jù),多重forEach循環(huán)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02關(guān)于StringUtils.isBlank()的使用及說明
這篇文章主要介紹了關(guān)于StringUtils.isBlank()的使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05Java數(shù)據(jù)庫(kù)連接池連接Oracle過程詳解
這篇文章主要介紹了Java數(shù)據(jù)庫(kù)連接池連接Oracle過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Spring常用注解 使用注解來構(gòu)造IoC容器的方法
下面小編就為大家分享一篇Spring常用注解 使用注解來構(gòu)造IoC容器的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01