大家好,欢迎来到IT知识分享网。
(PS:头条这个排版真是心塞,这么大一个公司,居然不支持代码排版,后续不想再写了)
在 Spring Boot 2 中,非 applicaiton.yml 的配置文件是默认不会解析的。如果对于简单的配置存储,比如主机或者一些环境变量,可以借助 PropertySource 注解实现,通过 @Value 进行读取。但是对于复杂的配置文件,比如要解析成 Map 字典存储,properties 配置文件就不能很好的体现。此时,就需要自行解析 yml 配置文件。
1、定义配置文件
logistics.yml,内容如下:
# 物流公司配置, 注意需要指定前缀, Spring Boot 2 才能将其进行 Map 方式装配到对象中 all-logistics: --logistics: ----aae: ----# 映射yms标识, 不对应时默认为 -1 ------id: -1 ------biz-code: aae ------biz-name: AAE全球专递 ----annengwuliu: ------id: -1 ------biz-code: annengwuliu ------biz-name: 安能物流
显然是一个 Map
类型,后续的操作都通过 Map 来进行。
2、自动配置
package com.github.zhgxun.learn.common.config; import com.github.zhgxun.learn.common.util.YamlUtil; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Component; import java.util.Map; @Data @Component @ConfigurationProperties(prefix = "all-logistics") @PropertySource(name = "logistics.yml", value = {"classpath:logistics.yml"}, factory = YamlUtil.class) public class LogisticsConfig { ----private Map
logistics; } @Data class Logistics { ----private int id; ----private String bizCode; ----private String bizName; }
在 Spring 中,配置都通过自动配置来完成。此时需要注意 @PropertySource(name = “logistics.yml”, value = {“classpath:logistics.yml”}, factory = YamlUtil.class) 这句,默认的配置解析工厂是 PropertySourceFactory,只解析 properties 后缀的配置文件。需要我们自己实现一个 yml 格式的配置解析文件。
3、解析工具
package com.github.zhgxun.learn.common.util; import org.springframework.boot.env.YamlPropertySourceLoader; import org.springframework.core.env.PropertySource; import org.springframework.core.io.support.DefaultPropertySourceFactory; import org.springframework.core.io.support.EncodedResource; import org.springframework.util.Assert; import java.io.IOException; import java.util.List; / * 自定义yml配置文件解析器 * 目前仅处理单个yml文件 */ public class YamlUtil extends DefaultPropertySourceFactory { ----@Override ----public PropertySource
createPropertySource(String name, EncodedResource resource) throws IOException { --------Assert.notNull(name, "Custom profile.yml file name is null"); --------Assert.hasLength(name, "Custom profile.yml file name is empty"); --------List
> propertySources = new YamlPropertySourceLoader().load(name, resource.getResource()); --------return propertySources.get(0); ----} }
继承 DefaultPropertySourceFactory 工厂,重写属性资源,实例化为 YamlPropertySourceLoader 即可导入资源。需要注意的是,该导入返回一个列表,即是支持多个资源导入。此处我们目前仅需要导入一个资源即可,如果涉及到多个资源导入,需要自行对其进行进一步的处理。
4、单元测试
package com.github.zhgxun.learn.config; import com.github.zhgxun.learn.common.config.LogisticsConfig; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class LogisticsTest { ----@Autowired ----private LogisticsConfig config; ----@Test ----public void test() { --------System.out.println(config.getLogistics()); ----} }
5、输出
{aae=Logistics(id=-1, bizCode=aae, bizName=AAE全球专递), annengwuliu=Logistics(id=-1, bizCode=annengwuliu, bizName=安能物流)}
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/90369.html