将字典数据,配置在 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 配置文件中字典项的更多相关文章

  1. SpringBoot加载子模块配置文件的方法

    这两天开始学习SpringBoot框架,按照官方的文档,很轻易地就把单模块的项目启动了,但在使用maven搭建多模块的时候遇到了子模块配置文件没有加载的问题 项目架构是这样的 zero |-ws |- ...

  2. springboot加载外部配置文件

    网上搜集和整理如下(自己已验证过) 1. war包在tomcat中加载外部配置文件 war包运行在独立tomcat下时,如何加载war包外部配置application.properties,以达到每次 ...

  3. SpringBoot启动加载yml配置文件出现编码格式错误

    Caused by: org.yaml.snakeyaml.error.YAMLException: java.nio.charset.MalformedInputException: Input l ...

  4. java EE加载peoperties配置文件

    //加载配置文件 InputStream in = JedisUtils.class.getClassLoader().getResourceAsStream("redis.properti ...

  5. SpringBoot系列——加载自定义配置文件

    前言 SpringBoot启动时默认加载bootstrap.properties或bootstrap.yml(这两个优先级最高).application.properties或application. ...

  6. Springboot 加载配置文件源码分析

    Springboot 加载配置文件源码分析 本文的分析是基于springboot 2.2.0.RELEASE. 本篇文章的相关源码位置:https://github.com/wbo112/blogde ...

  7. spring加载jar包中多个配置文件(转)

    转自:http://evan0625.iteye.com/blog/1598366 在使用spring加载jar包中的配置文件时,不支持通配符,需要一个一个引入,如下所示: Java代码 <co ...

  8. SpringBoot加载配置文件(@PropertySource@importSource@Value)

    情景描述 最近新搭建了一个项目,从Spring迁到了Springboot,为了兼容Spring加载配置文件的风格,所以还想把PropertyPlaceholderConfigurer放在.xml文件里 ...

  9. Spring中加载xml配置文件的六种方式

    Spring中加载xml配置文件的六种方式 博客分类: Spring&EJB XMLSpringWebBeanBlog  因为目前正在从事一个项目,项目中一个需求就是所有的功能都是插件的形式装 ...

  10. java动态编译类文件并加载到内存中

    如果你想在动态编译并加载了class后,能够用hibernate的数据访问接口以面向对象的方式来操作该class类,请参考这篇博文-http://www.cnblogs.com/anai/p/4270 ...

随机推荐

  1. python开头

    python识别的正则模式 coding[:=]\s*([-\w.]+)#coding:utf8   ???不要用 #coding=utf-8#coding:utf-8# -*- coding: ut ...

  2. PASS模型需求分析和原型设计

    班级网址 https://edu.cnblogs.com/campus/zjcsxy/SE2020 作业要求 https://edu.cnblogs.com/campus/zjcsxy/SE2020/ ...

  3. 记录一次HAWQ手工启动

    一.环境变量初始化 使用hawq的命令必须先进行环境变量的初始化 命令不能以 root 用户执行,应该以 gpadmin 用户执行 source /usr/local/apache-hawq/gree ...

  4. ROS突发限速参考资料

    MT-Rate-Limit - Datarate limitation for clients. Format is: rx-rate[/tx-rate] [rx-burst-rate[/tx-bur ...

  5. SpringCloud之旅

    现在大部分公司的项目架构都选择了微服务,我们公司也不例外,那么什么是微服务呢?今天就来开启SpringCloud之旅! SpringCloud是基于SpringBoot的一整套的微服务架构.他提供了微 ...

  6. NX二次开发 批量导出X_T(包括部件里的装配体)

    VS2019 NX1946 UF_initialize(); string filepath = strDir + "\\";//拼接路径 //获取图层过滤 PropertyLis ...

  7. IDEA 文件夹下无法新建java class文件问题处理:

    主要问题 1 未设置为源文件夹. 2 包名中包含关键字,也无法创建. 解决方式 1 文件夹右键---Mark Directory as----Test Sources Root 即可. 2 重命名

  8. userdel: user zhangsan is currently used by process 1057

    我个人推测是在root用户下su 切换到xiaoming用户,然后在xiaoming用户下又切换回root,但是xiaoming用户还被某个进程占用着,所以进程不死,用户del不掉. 所以我们在命令行 ...

  9. git Permission denied, please try again.

    博主在github上下载tiny face的的源代码的时候,遇到git clone命令为:git clone --recursive git@github.com:peiyunh/tiny.git 而 ...

  10. simpleini库的介绍和使用(面向业务编程-格式处理)

    simpleini库的介绍和使用(面向业务编程-格式处理) 介绍 simpleini是一个跨平台的ini格式处理库,提供了一些简单的API来读取和写入ini风格的配置文件.它支持ASCII.MBCS和 ...