最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot實(shí)現(xiàn)加載yml文件中字典數(shù)據(jù)

 更新時(shí)間:2023年04月19日 14:27:03   作者:VipSoft  
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何實(shí)現(xiàn)加載yml文件中字典數(shù)據(jù),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下

將字典數(shù)據(jù),配置在 yml 文件中,通過(guò)加載yml將數(shù)據(jù)加載到 Map中

Spring Boot 中 yml 配置、引用其它 yml 中的配置。# 在配置文件目錄(如:resources)下新建application-xxx

必須以application開(kāi)頭的yml文件, 多個(gè)文件用 "," 號(hào)分隔,不能換行

項(xiàng)目結(jié)構(gòu)文件

application.yml

server:
  port: 8088
  application:
    name: VipSoft Env Demo


spring:
  profiles:
    include:
      dic      # 在配置文件目錄(如:resources)下新建application-xxx 開(kāi)頭的yml文件, 多個(gè)文件用 "," 號(hào)分隔,不能換行

#性別字典
user-gender:
  0: 未知
  1: 男
  2: 女

application-dic.yml

將字典獨(dú)立到單獨(dú)的yml文件中

#支付方式
pay-type:
  1: 微信支付
  2: 貨到付款

在 resources 目錄下,創(chuàng)建META-INF目錄,創(chuàng)建 spring.factories文件,

Spring Factories是一種類似于Java SPI的機(jī)制,它在META-INF/spring.factories文件中配置接口的實(shí)現(xiàn)類名稱,然后在程序中讀取這些配置文件并實(shí)例化。

內(nèi)容如下:

# Environment Post Processor
org.springframework.boot.env.EnvironmentPostProcessor=com.vipsoft.web.utils.ConfigUtil

ConfigUtil

package com.vipsoft.web.utils;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.util.Assert;

public class ConfigUtil implements EnvironmentPostProcessor {

    private static Binder binder;

    private static ConfigurableEnvironment environment;

    public static String getString(String key) {
        Assert.notNull(environment, "environment 還未初始化!");
        return environment.getProperty(key, String.class, "");
    }

    public static <T> T bindProperties(String prefix, Class<T> clazz) {
        Assert.notNull(prefix, "prefix 不能為空");
        Assert.notNull(clazz, "class 不能為空");
        BindResult<T> result = ConfigUtil.binder.bind(prefix, clazz);
        return result.isBound() ? result.get() : null;
    }

    /**
    * 通過(guò) META-INF/spring.factories,觸發(fā)該方法的執(zhí)行,進(jìn)行環(huán)境變量的加載
    */
    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        for (PropertySource<?> propertySource : environment.getPropertySources()) {
            if (propertySource.getName().equals("refreshArgs")) {
                return;
            }
        }
        ConfigUtil.environment = environment;
        ConfigUtil.binder = Binder.get(environment);
    }
}

DictVo

package com.vipsoft.web.vo;


public class DictVO implements java.io.Serializable {
    private static final long serialVersionUID = 379963436836338904L;
    /**
     * 字典類型
     */
    private String type;
    /**
     * 字典編碼
     */
    private String code;
    /**
     * 字典值
     */
    private String value;

    public DictVO(String code, String value) {
        this.code = code;
        this.value = value;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

DefaultController

package com.vipsoft.web.controller;

import com.vipsoft.web.utils.ConfigUtil;
import com.vipsoft.web.vo.DictVO;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;


@RestController
public class DefaultController {
    @GetMapping(value = "/")
    public String login() {
        return "VipSoft Demo !!!";
    }

    @GetMapping("/list/{type}")
    public List<DictVO> listDic(@PathVariable("type") String type) {
        LinkedHashMap dict = ConfigUtil.bindProperties(type.replaceAll("_", "-"), LinkedHashMap.class);
        List<DictVO> list = new ArrayList<>();
        if (dict == null || dict.isEmpty()) {
            return list;
        }
        dict.forEach((key, value) -> list.add(new DictVO(key.toString(), value.toString())));
        return list;
    }
}

運(yùn)行效果

單元測(cè)試

package com.vipsoft.web;

import com.vipsoft.web.controller.DefaultController;
import com.vipsoft.web.utils.ConfigUtil;
import com.vipsoft.web.vo.DictVO;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class DicTest {
    @Autowired
    DefaultController defaultController;

    @Test
    public void DicListTest() throws Exception {
        List<DictVO> pay_type = defaultController.listDic("pay-type");
        pay_type.forEach(p -> System.out.println(p.getCode() + " => " + p.getValue()));


        List<DictVO> user_gender = defaultController.listDic("user-gender");
        user_gender.forEach(p -> System.out.println(p.getCode() + " => " + p.getValue()));
    }


    @Test
    public void getString() throws Exception {
        String includeYml = ConfigUtil.getString("spring.profiles.include");
        System.out.println("application 引用了配置文件 =》 " + includeYml);
    }
}

以上就是SpringBoot實(shí)現(xiàn)加載yml文件中字典數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot加載yml文件字典數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • SpringBoot整合Spring?Security過(guò)濾器鏈加載執(zhí)行流程源碼分析(最新推薦)

    SpringBoot整合Spring?Security過(guò)濾器鏈加載執(zhí)行流程源碼分析(最新推薦)

    Spring?Boot?對(duì)于?Spring?Security?提供了自動(dòng)化配置方案,可以使用更少的配置來(lái)使用?Spring?Security,這篇文章主要介紹了SpringBoot整合Spring?Security過(guò)濾器鏈加載執(zhí)行流程源碼分析,需要的朋友可以參考下
    2023-02-02
  • SpringBoot?實(shí)現(xiàn)流控的操作方法

    SpringBoot?實(shí)現(xiàn)流控的操作方法

    本文介紹了限流算法的基本概念和常見(jiàn)的限流算法,包括計(jì)數(shù)器算法、漏桶算法和令牌桶算法,還介紹了如何在Spring?Boot中使用Guava庫(kù)和自定義注解以及AOP實(shí)現(xiàn)接口限流功能,感興趣的朋友一起看看吧
    2024-12-12
  • BaseDao封裝增刪改查的代碼詳解

    BaseDao封裝增刪改查的代碼詳解

    本篇文章主要介紹對(duì)數(shù)據(jù)庫(kù)中表中的數(shù)據(jù)進(jìn)行增改刪查詢,封裝一個(gè)工具類(BaseDao)的詳細(xì)使用以及部分理論知識(shí),并通過(guò)代碼示例給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-03-03
  • 基于java SSM springboot實(shí)現(xiàn)抗疫物質(zhì)信息管理系統(tǒng)

    基于java SSM springboot實(shí)現(xiàn)抗疫物質(zhì)信息管理系統(tǒng)

    這篇文章主要介紹了基于JAVA SSM springboot實(shí)現(xiàn)的抗疫物質(zhì)信息管理系統(tǒng),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-08
  • SpringMVC 單文件上傳與多文件上傳實(shí)例

    SpringMVC 單文件上傳與多文件上傳實(shí)例

    這篇文章主要介紹了SpringMVC 單文件上傳與多文件上傳實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • Java如何將若干時(shí)間區(qū)間進(jìn)行合并的方法步驟

    Java如何將若干時(shí)間區(qū)間進(jìn)行合并的方法步驟

    這篇文章主要介紹了Java如何將若干時(shí)間區(qū)間進(jìn)行合并的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Hadoop多Job并行處理的實(shí)例詳解

    Hadoop多Job并行處理的實(shí)例詳解

    這篇文章主要介紹了Hadoop多Job并行處理的實(shí)例詳解的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下
    2017-10-10
  • 關(guān)于RequestMapping注解的作用說(shuō)明

    關(guān)于RequestMapping注解的作用說(shuō)明

    這篇文章主要介紹了關(guān)于RequestMapping注解的作用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
    2022-01-01
  • 使用ServletInputStream()輸入流讀取圖片方式

    使用ServletInputStream()輸入流讀取圖片方式

    這篇文章主要介紹了使用ServletInputStream()輸入流讀取圖片方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 關(guān)于JSON.toJSONString()和Gson.toJson()方法的比較

    關(guān)于JSON.toJSONString()和Gson.toJson()方法的比較

    本文介紹了兩種將Java對(duì)象轉(zhuǎn)換為JSON字符串的方法:阿里的`JSON.toJSONString()`和谷歌的`Gson.toJson()`,通過(guò)一個(gè)示例,展示了當(dāng)使用繼承關(guān)系且子類覆蓋父類字段時(shí),`Gson`會(huì)報(bào)錯(cuò),而`JSON`可以正常運(yùn)行,作者建議在處理JSON相關(guān)操作時(shí)使用阿里的`JSON`類
    2024-11-11

最新評(píng)論

三门县| 黄浦区| 府谷县| 彩票| 图们市| 临桂县| 麟游县| 南昌市| 专栏| 武宣县| 上思县| 苏尼特左旗| 琼结县| 江源县| 株洲县| 勃利县| 灵璧县| 班戈县| 齐齐哈尔市| 丰台区| 云龙县| 花莲市| 南开区| 漯河市| 抚顺县| 建平县| 辉南县| 杭锦旗| 宝兴县| 洛川县| 项城市| 双桥区| 宁安市| 万全县| 西和县| 临沧市| 革吉县| 封丘县| 米脂县| 赣榆县| 浮山县|