您好,欢迎访问一九零五行业门户网

SpringBoot怎么实现加载yml文件中字典数据

将字典数据,配置在 yml 文件中,通过加载yml将数据加载到 map中
spring boot 中 yml 配置、引用其它 yml 中的配置。# 在配置文件目录(如:resources)下新建application-xxx
必须以application开头的yml文件, 多个文件用 , 号分隔,不能换行
项目结构文件
application.yml
server: port: 8088 application: name: vipsoft env demospring: 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 processororg.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,触发该方法的执行,进行环境变量的加载 */ @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;@restcontrollerpublic 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;@springboottestpublic 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怎么实现加载yml文件中字典数据的详细内容。
其它类似信息

推荐信息