Java SpringBoot 加载 yml 配置文件中字典项
将字典数据,配置在 yml 文件中,通过加载yml将数据加载到 Map中
Spring Boot 中 yml 配置、引用其它 yml 中的配置。# 在配置文件目录(如:resources)下新建application-xxx
必须以application开头的yml文件, 多个文件用 "," 号分隔,不能换行
项目结构文件
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
目录下,创建META-INF
目录,创建 spring.factories
文件,
Spring Factories是一种类似于Java SPI的机制,它在META-INF/spring.factories文件中配置接口的实现类名称,然后在程序中读取这些配置文件并实例化。
内容如下:
# 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;
}
@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;
}
}
运行效果
Java SpringBoot 加载 yml 配置文件中字典项的更多相关文章
- SpringBoot加载子模块配置文件的方法
这两天开始学习SpringBoot框架,按照官方的文档,很轻易地就把单模块的项目启动了,但在使用maven搭建多模块的时候遇到了子模块配置文件没有加载的问题 项目架构是这样的 zero |-ws |- ...
- springboot加载外部配置文件
网上搜集和整理如下(自己已验证过) 1. war包在tomcat中加载外部配置文件 war包运行在独立tomcat下时,如何加载war包外部配置application.properties,以达到每次 ...
- SpringBoot启动加载yml配置文件出现编码格式错误
Caused by: org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input l ...
- java EE加载peoperties配置文件
//加载配置文件 InputStream in = JedisUtils.class.getClassLoader().getResourceAsStream("redis.properti ...
- SpringBoot系列——加载自定义配置文件
前言 SpringBoot启动时默认加载bootstrap.properties或bootstrap.yml(这两个优先级最高).application.properties或application. ...
- Springboot 加载配置文件源码分析
Springboot 加载配置文件源码分析 本文的分析是基于springboot 2.2.0.RELEASE. 本篇文章的相关源码位置:https://github.com/wbo112/blogde ...
- spring加载jar包中多个配置文件(转)
转自:http://evan0625.iteye.com/blog/1598366 在使用spring加载jar包中的配置文件时,不支持通配符,需要一个一个引入,如下所示: Java代码 <co ...
- SpringBoot加载配置文件(@PropertySource@importSource@Value)
情景描述 最近新搭建了一个项目,从Spring迁到了Springboot,为了兼容Spring加载配置文件的风格,所以还想把PropertyPlaceholderConfigurer放在.xml文件里 ...
- Spring中加载xml配置文件的六种方式
Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog 因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...
- java动态编译类文件并加载到内存中
如果你想在动态编译并加载了class后,能够用hibernate的数据访问接口以面向对象的方式来操作该class类,请参考这篇博文-http://www.cnblogs.com/anai/p/4270 ...
随机推荐
- mybatis-config.xml头信息
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 P ...
- ROM,RAM,内存
ROM是用来存放最基本的程序的,不是系统程序(windows),而是主板自带的最基本的程序, 无法被删除,更改.只能读取. 操作系统是放在硬盘里的,在开机时会在内存中加载,所以windows7比win ...
- jmeter--负载测试
负载测试 1. jmeter插件处理 2. 下载负载测试计划所需要插件 3. 负载测试计划 4. 波浪形的测试计划--测试服务器的稳定性 一般用于测试稳定的场景测试(有规律的活动/场景/接口请求等等, ...
- Linux调用python文件的同时传参
创建python文件内容如下: 计算两个数相加并打印,需要传递两个参数 vi sum.py import sys def calc(a,b): c=a+b return c a1=int(float( ...
- Leecode 1.两数之和(Java 哈希表)
想法: 1.哈希表hashmap 第一种方法:将数组中元素及其下标right都加入hashmap中,对于每个元素n下标left,在map中查找是否有target-n的元素,若有,则返回其下标right ...
- GDB使用详解
来源:GDB使用详解 - 知乎 (zhihu.com) 1. 概述 GDB 全称"GNU symbolic debugger",从名称上不难看出,它诞生于 GNU 计划(同时诞 ...
- ffmpeg+nginx+hls(低延迟)
先看一下我参考的原文实现: 实现方案 https://zhuanlan.zhihu.com/p/87225094 流媒体服务器环境搭建 https://blog.csdn.net/impingo/ar ...
- 错误:为仓库 'appstream' 下载元数据失败 : Cannot prepare internal mirrorlist: No URLs in mirrorlist
sudo sed -i -e "s|mirrorlist=|#mirrorlist=|g" /etc/yum.repos.d/CentOS-* sudo sed -i -e &qu ...
- .Net中跨域问题的解决方案
开发中前端与后端完全分离并分开发布,遇到跨域问题,一通百度之后,解决方案如下: 把下面的代码放在web.config文件中的 System.WebServer 节点下 <httpProtocol ...
- 快速构造Python爬虫请求,有这个网站就够了!
引言 大家好,我是蜡笔小曦. 我们在通过程序向某个网页发起请求时,实际上是模拟浏览器进行http(超文本传输协议)请求,这就要求我们需要按照固定的格式进行代码构造. 一般请求数据分为三部分:请求行.请 ...