SpringBoot項目啟動數(shù)據(jù)加載內(nèi)存的三種方法
一、前言
一般來說,SpringBoot工程環(huán)境配置放在properties文件中,啟動的時候?qū)⒐こ讨械膒roperties/yaml文件的配置項加載到內(nèi)存中。但這種方式改配置項的時候,需要重新編譯部署,考慮到這種因素,今天介紹將配置項存到數(shù)據(jù)庫表中,在工程啟動時把配置項加載到內(nèi)存中。
SpringBoot提供了兩個接口: CommandLineRunner 和 ApplicationRunner 。實現(xiàn)其中接口,就可以在工程啟動時將數(shù)據(jù)庫中的數(shù)據(jù)加載到內(nèi)存。使用的場景有:加載配置項到內(nèi)存中;啟動時將字典或白名單數(shù)據(jù)加載到內(nèi)存(或緩存到Redis中)。
二、加載方式
第一種:使用@PostConstruct注解(properties/yaml文件)。
第二種:使用@Order注解和CommandLineRunner接口。
第三種:使用@Order注解和ApplicationRunner接口。
注意事項
第二種和第三種,二者的官方j(luò)avadoc一樣,區(qū)別在于接收的參數(shù)不一樣。CommandLineRunner的參數(shù)是最原始的參數(shù),沒有做任何處理。ApplicationRunner的參數(shù)是ApplicationArguments,是對原始參數(shù)做了進一步的封裝。
三、代碼示例
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)用的時機是在容器初始化完成之后,立即調(diào)用。
2、CommandLineRunner和ApplicationRunner使用上沒有區(qū)別,唯一區(qū)別是CommandLineRunner接受字符串數(shù)組參數(shù),需要自行解析出健和值,ApplicationRunner的參數(shù)是ApplicationArguments,是對原始參數(shù)做了進一步的封裝。
3、兩個接口都可以使用 @Order 參數(shù),支持工程啟動后根據(jù)order 聲明的權(quán)重值來決定調(diào)用的順序(數(shù)字越小,優(yōu)先級越高)。
以上就是SpringBoot項目啟動數(shù)據(jù)加載內(nèi)存中的三種方法的詳細內(nèi)容,更多關(guān)于SpringBoot數(shù)據(jù)加載內(nèi)存的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決
這篇文章主要介紹了Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01MyBatis實現(xiàn)批量插入數(shù)據(jù),多重forEach循環(huán)
這篇文章主要介紹了MyBatis實現(xiàn)批量插入數(shù)據(jù),多重forEach循環(huán)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02關(guān)于StringUtils.isBlank()的使用及說明
這篇文章主要介紹了關(guān)于StringUtils.isBlank()的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05Java數(shù)據(jù)庫連接池連接Oracle過程詳解
這篇文章主要介紹了Java數(shù)據(jù)庫連接池連接Oracle過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09Spring常用注解 使用注解來構(gòu)造IoC容器的方法
下面小編就為大家分享一篇Spring常用注解 使用注解來構(gòu)造IoC容器的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01