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 ...
随机推荐
- Oracle_20200416
PLSQL 新建普通用户 1.使用system登录 2.File-->NEW-->SQL WINDOW 3.执行语句 --创建用户 --create user 用户名 identified ...
- Java定时器Timer和TimerTask
方式一:设定指定任务task在指定时间time执行 schedule(TimerTask task, Date date) public static void main(String[] arg ...
- zsh以及oh-my-zsh的安装配置
Oh My Zsh是一款社区驱动的命令行工具,正如它的主页上说的,Oh My Zsh 是一种生活方式.它基于zsh命令行,提供了主题配置,插件机制,已经内置的便捷操作.给我们一种全新的方式使用命令行. ...
- Docker部署【项目管理和问题跟踪工具-Redmine】
创建网络 docker network create redmine-network 启动Mysql数据库 docker run -d --name mysql --network redmine-n ...
- jQuery.extend 函数详解(转)
地址:http://www.jb51.net/article/29591.htm JQuery的extend扩展方法: Jquery的扩展方法extend是我们在写插件的过程中常用的方法,该方法有一些 ...
- Unity检测鼠标是否与UI交互
在Unity项目中,假设在鼠标按键时会触发游戏内的操作,但是在鼠标与UI进行交互时我们希望停止游戏中的操作,这是需要使用EventSystem中的方法来检测鼠标是否正在与UI交互 private bo ...
- MySql分库分表以及相关问题
为什么要分库分表? MySql是存在瓶颈的,数据量就是他最大的瓶颈,如果一张表或者一个数据库里面的数据量过大都会导致一些意料之外的问题,譬如查询过慢,难以维护等问题,这时候就要想出一个完美的解决办法. ...
- Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test) on
解决错误 Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test (default-test ...
- Mysql开放读取权限
对用户授权 mysql>grant rights on database.* to user@host identified by "pass"; 例1: 增加一个用户tes ...
- 给jui(dwz)的toolbar添加漂亮的图标
前面两篇把菜单树和navTab的图标都换了.今天来添加toolbar的图标. 因为JUI(DWZ)自带的toolbar图标就三四个,根本不够用.于是只能是进行自定义添加 这是系统自带的图标,也就4个. ...