(PS:头条这个排版真是心塞,这么大一个公司,竟然不支撑代码排版,后续不想再写了)
在 Spring Boot 2 中,非 applicaiton.yml 的配置文件是默许不会解析的。如果对于简单的配置存储,比如主机或者一些环境变量,可以借助 PropertySource 注解实现,通过 @Value 进行读取。然而对于繁杂的配置文件,比如要解析成 Map 字典存储,properties 配置文件就不能很好的体现。此时,就需要自行解析 yml 配置文件。文章源自微观生活(93wg.com)微观生活-https://93wg.com/14550.html
1、定义配置文件文章源自微观生活(93wg.com)微观生活-https://93wg.com/14550.html
logistics.yml,内容如下:文章源自微观生活(93wg.com)微观生活-https://93wg.com/14550.html
2、自动配置package com.github.zhgxun.learn.co妹妹on.config;import com.github.zhgxun.learn.co妹妹on.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<String, Logistics> logistics;}@Dataclass Logistics {----private int id;----private String bizCode;----private String bizName;}文章源自微观生活(93wg.com)微观生活-https://93wg.com/14550.html
在 Spring 中,配置都通过自动配置来完成。此时需要注意 @PropertySource(name = \"logistics.yml\", value = {\"classpath:logistics.yml\"}, factory = YamlUtil.class) 这句,默许的配置解析工厂是 PropertySourceFactory,只解析 properties 后缀的配置文件。需要咱们自己实现一个 yml 格式的配置解析文件。文章源自微观生活(93wg.com)微观生活-https://93wg.com/14550.html
3、解析工具package com.github.zhgxun.learn.co妹妹on.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<PropertySource<?>> propertySources = new YamlPropertySourceLoader().load(name, resource.getResource());--------return propertySources.get(0);----}}文章源自微观生活(93wg.com)微观生活-https://93wg.com/14550.html
继承 DefaultPropertySourceFactory 工厂,重写属性资源,实例化为 YamlPropertySourceLoader 便可导入资源。需要注意的是,该导入返回一个列表,即是支撑多个资源导入。此处咱们目前仅需要导入一个资源便可,如果触及到多个资源导入,需要自行对其进行进一步的处理。文章源自微观生活(93wg.com)微观生活-https://93wg.com/14550.html
4、单元测试package com.github.zhgxun.learn.config;import com.github.zhgxun.learn.co妹妹on.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)@SpringBootTestpublic 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=安能物流)}文章源自微观生活(93wg.com)微观生活-https://93wg.com/14550.html
以上就是微观生活(93wg.com)关于“Spring Boot 2 自定义yml配置文件解析”的详细内容,希望对大家有所帮助!文章源自微观生活(93wg.com)微观生活-https://93wg.com/14550.html 文章源自微观生活(93wg.com)微观生活-https://93wg.com/14550.html
评论