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

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開頭的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 開頭的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事件發(fā)布和監(jiān)聽詳解

    SpringBoot事件發(fā)布和監(jiān)聽詳解

    今天去官網(wǎng)查看spring boot資料時(shí),在特性中看見(jiàn)了系統(tǒng)的事件及監(jiān)聽章節(jié),所以下面這篇文章主要給大家介紹了關(guān)于SpringBoot事件發(fā)布和監(jiān)聽的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-11-11
  • mybatis-plus使用@EnumValue處理枚舉類型的示例代碼

    mybatis-plus使用@EnumValue處理枚舉類型的示例代碼

    這篇文章主要介紹了mybatis-plus使用@EnumValue處理枚舉類型的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • 新聞列表的分頁(yè)查詢java代碼實(shí)現(xiàn)

    新聞列表的分頁(yè)查詢java代碼實(shí)現(xiàn)

    這篇文章主要為大家詳細(xì)介紹了新聞列表的分頁(yè)查詢java代碼實(shí)現(xiàn),感興趣的小伙伴們可以參考一下
    2016-08-08
  • pom文件中${project.basedir}的使用

    pom文件中${project.basedir}的使用

    這篇文章主要介紹了pom文件中${project.basedir}的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • 一文帶你掌握J(rèn)ava8中Lambda表達(dá)式 函數(shù)式接口及方法構(gòu)造器數(shù)組的引用

    一文帶你掌握J(rèn)ava8中Lambda表達(dá)式 函數(shù)式接口及方法構(gòu)造器數(shù)組的引用

    Java 8 (又稱為 jdk 1.8) 是 Java 語(yǔ)言開發(fā)的一個(gè)主要版本。 Oracle 公司于 2014 年 3 月 18 日發(fā)布 Java 8 ,它支持函數(shù)式編程,新的 JavaScript 引擎,新的日期 API,新的Stream API 等
    2021-10-10
  • java編程Reference核心原理示例源碼分析

    java編程Reference核心原理示例源碼分析

    這篇文章主要為大家介紹了java編程Reference的核心原理以及示例源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-01-01
  • IntellJ IDEA神器使用技巧(小結(jié))

    IntellJ IDEA神器使用技巧(小結(jié))

    這篇文章主要介紹了IntellJ IDEA神器使用技巧(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • mybatis update更新字段的使用操作

    mybatis update更新字段的使用操作

    這篇文章主要介紹了mybatis update更新字段的使用操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • java注解的全面分析

    java注解的全面分析

    這篇文章主要介紹了java注解的全面分析的相關(guān)資料,Java提供的一種原程序中的元素關(guān)聯(lián)任何信息和任何元數(shù)據(jù)的途徑和方法,需要的朋友可以參考下
    2017-08-08
  • IDEA生成項(xiàng)目maven-tree依賴目錄樹結(jié)構(gòu)方式

    IDEA生成項(xiàng)目maven-tree依賴目錄樹結(jié)構(gòu)方式

    這篇文章主要介紹了IDEA生成項(xiàng)目maven-tree依賴目錄樹結(jié)構(gòu)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12

最新評(píng)論