欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot項(xiàng)目啟動(dòng)數(shù)據(jù)加載內(nèi)存的三種方法

 更新時(shí)間:2024年04月30日 09:13:44   作者:No8g攻城獅  
一般來說,SpringBoot工程環(huán)境配置放在properties文件中,啟動(dòng)的時(shí)候?qū)⒐こ讨械膒roperties/yaml文件的配置項(xiàng)加載到內(nèi)存中,本文給大家介紹了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)文章!

相關(guān)文章

  • Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決

    Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決

    這篇文章主要介紹了Spring多數(shù)據(jù)源導(dǎo)致配置失效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 基于Java中字符串內(nèi)存位置詳解

    基于Java中字符串內(nèi)存位置詳解

    下面小編就為大家?guī)硪黄贘ava中字符串內(nèi)存位置詳解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-08-08
  • MyBatis實(shí)現(xiàn)批量插入數(shù)據(jù),多重forEach循環(huán)

    MyBatis實(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()的使用及說明

    這篇文章主要介紹了關(guān)于StringUtils.isBlank()的使用及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Java數(shù)據(jù)庫(kù)連接池連接Oracle過程詳解

    Java數(shù)據(jù)庫(kù)連接池連接Oracle過程詳解

    這篇文章主要介紹了Java數(shù)據(jù)庫(kù)連接池連接Oracle過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java實(shí)現(xiàn)人機(jī)猜拳小游戲

    Java實(shí)現(xiàn)人機(jī)猜拳小游戲

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)人機(jī)猜拳小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • Spring常用注解 使用注解來構(gòu)造IoC容器的方法

    Spring常用注解 使用注解來構(gòu)造IoC容器的方法

    下面小編就為大家分享一篇Spring常用注解 使用注解來構(gòu)造IoC容器的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • Java類的加載時(shí)機(jī)與過程

    Java類的加載時(shí)機(jī)與過程

    這篇文章主要介紹了Java類的加載時(shí)機(jī)與過程,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2021-12-12
  • 詳解springboot整合ueditor踩過的坑

    詳解springboot整合ueditor踩過的坑

    這篇文章主要介紹了詳解springboot整合ueditor踩過的坑,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • spring boot的maven配置依賴詳解

    spring boot的maven配置依賴詳解

    本篇文章主要介紹了spring boot的maven配置依賴詳解,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-11-11

最新評(píng)論