SpringBoot實現(xiàn)加載yml文件中字典數(shù)據(jù)
將字典數(shù)據(jù),配置在 yml 文件中,通過加載yml將數(shù)據(jù)加載到 Map中
Spring Boot 中 yml 配置、引用其它 yml 中的配置。# 在配置文件目錄(如:resources)下新建application-xxx
必須以application開頭的yml文件, 多個文件用 "," 號分隔,不能換行
項目結(jié)構(gòu)文件

application.yml
server:
port: 8088
application:
name: VipSoft Env Demo
spring:
profiles:
include:
dic # 在配置文件目錄(如:resources)下新建application-xxx 開頭的yml文件, 多個文件用 "," 號分隔,不能換行
#性別字典
user-gender:
0: 未知
1: 男
2: 女
application-dic.yml
將字典獨立到單獨的yml文件中
#支付方式 pay-type: 1: 微信支付 2: 貨到付款
在 resources 目錄下,創(chuàng)建META-INF目錄,創(chuàng)建 spring.factories文件,
Spring Factories是一種類似于Java SPI的機(jī)制,它在META-INF/spring.factories文件中配置接口的實現(xiàn)類名稱,然后在程序中讀取這些配置文件并實例化。
內(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;
}
/**
* 通過 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;
}
}運行效果

單元測試
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實現(xiàn)加載yml文件中字典數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot加載yml文件字典數(shù)據(jù)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
mybatis-plus使用@EnumValue處理枚舉類型的示例代碼
這篇文章主要介紹了mybatis-plus使用@EnumValue處理枚舉類型的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
一文帶你掌握J(rèn)ava8中Lambda表達(dá)式 函數(shù)式接口及方法構(gòu)造器數(shù)組的引用
Java 8 (又稱為 jdk 1.8) 是 Java 語言開發(fā)的一個主要版本。 Oracle 公司于 2014 年 3 月 18 日發(fā)布 Java 8 ,它支持函數(shù)式編程,新的 JavaScript 引擎,新的日期 API,新的Stream API 等2021-10-10
IDEA生成項目maven-tree依賴目錄樹結(jié)構(gòu)方式
這篇文章主要介紹了IDEA生成項目maven-tree依賴目錄樹結(jié)構(gòu)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12

