SpringBoot實(shí)現(xiàn)加載yml文件中字典數(shù)據(jù)
將字典數(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í)行流程源碼分析(最新推薦)
Spring?Boot?對(duì)于?Spring?Security?提供了自動(dòng)化配置方案,可以使用更少的配置來(lái)使用?Spring?Security,這篇文章主要介紹了SpringBoot整合Spring?Security過(guò)濾器鏈加載執(zhí)行流程源碼分析,需要的朋友可以參考下2023-02-02
SpringBoot?實(shí)現(xiàn)流控的操作方法
本文介紹了限流算法的基本概念和常見(jiàn)的限流算法,包括計(jì)數(shù)器算法、漏桶算法和令牌桶算法,還介紹了如何在Spring?Boot中使用Guava庫(kù)和自定義注解以及AOP實(shí)現(xiàn)接口限流功能,感興趣的朋友一起看看吧2024-12-12
基于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
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
關(guān)于RequestMapping注解的作用說(shuō)明
這篇文章主要介紹了關(guān)于RequestMapping注解的作用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。2022-01-01
使用ServletInputStream()輸入流讀取圖片方式
這篇文章主要介紹了使用ServletInputStream()輸入流讀取圖片方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
關(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

