自定义配置文件(跟SpringBoot的application.yml同一目录下):

nlu-parse-rule:
title: "NLU响应结果解析规则"
desc: "解析NLU的识别文本(JSON)构建响应URL,注意当前yaml配置中key不用到下划线"
rules:
- busiDesc: "能耗业务1"
busiCode: "nh1"
firstMarch:
- $.store.bicycle.color|red|=
- $.appkey|7kfo5mdq5xnf6ofkfh76xda7lbccqgi7gzfhakyq|=
secondMatch:
- $.store.bicycle.color|NULL|<>
- $.store.bicycle.color|NULL|<>
returnValue: # url中占位符的真实替换值
- key1|$.store.bicycle.color
- key2|$.store.bicycle.color
- key3|$.store.bicycle.color

映射为对象,代码如下:

 @Component
@PropertySource(value= {"classpath:rules.yml"})
@ConfigurationProperties(prefix = "nlu-parse-rule")
@Data
public class NluRuleProperties {
private String title;
private String desc;
private List<NluRuleConfig> rules;
}

调试发现竟然不识别,

@PropertySource 不支持yml文件的对象转换,原因如下,看源码:他的默认构造工厂是PropertySourceFactory

 @Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {
String name() default ""; String[] value(); boolean ignoreResourceNotFound() default false; String encoding() default ""; Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}

而PropertySourceFactory默认就一个实现:实现properties配置文件的加载解析

public class DefaultPropertySourceFactory implements PropertySourceFactory {
public DefaultPropertySourceFactory() {
} public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
return name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource);
}
}

因此,我们只要实现一个yml文件的工厂类即可,参考:https://mdeinum.github.io/2018-07-04-PropertySource-with-yaml-files/

代码实现:

 public class YamlPropertySourceFactory implements PropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
Properties propertiesFromYaml = loadYamlIntoProperties(resource);
String sourceName = name != null ? name : resource.getResource().getFilename();
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
} private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException {
try {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
} catch (IllegalStateException e) {
// for ignoreResourceNotFound
Throwable cause = e.getCause();
if (cause instanceof FileNotFoundException)
throw (FileNotFoundException) e.getCause();
throw e;
}
}
}

搞定!测试通过。

SpringBoot加载自定义yml文件的更多相关文章

  1. springboot加载application.yml文件null

    话不多说,直接上代码 本人项目为maven项目 以下是项目结构 pom.xml文件 <?xml version="1.0" encoding="UTF-8" ...

  2. spring boot 加载自定义log4j 文件路径

    spring boot 使用log4j 打印时,需首先去除自带 Logger ,然后加入log4j 依赖 <dependencies> <!-- https://mvnreposit ...

  3. laravel 自动加载 自定义的文件/辅助函数

    需求 在 laravel 中自定义了一些 辅助函数,想要laravel框架自动加载这些函数 实现 将自定义的辅助函数放在helpers.php文件中,如下: 在compsoer.json 的 auto ...

  4. java web项目启动时自动加载自定义properties文件

    首先创建一个类 public class ContextInitListener implements ServletContextListener 使得该类成为一个监听器.用于监听整个容器生命周期的 ...

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

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

  6. SpringBoot启动如何加载application.yml配置文件

    一.前言 在spring时代配置文件的加载都是通过web.xml配置加载的(Servlet3.0之前),可能配置方式有所不同,但是大多数都是通过指定路径的文件名的形式去告诉spring该加载哪个文件: ...

  7. Springboot默认加载application.yml原理以及扩展

    Springboot默认加载application.yml原理以及扩展 SpringApplication.run(...)默认会加载classpath下的application.yml或applic ...

  8. Eclipse下SpringBoot没有自动加载application.properties文件

    Eclipse内创建SpringBoot项目,在java/main/resources文件夹下面创建application.properties配置文件,SpringApplication.run后发 ...

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

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

随机推荐

  1. uiwebview 离线缓存 图片

    uiwebview 离线缓存图片

  2. 我的首次AK记

    哈哈哈!本蒟蒻终于在今天中午机房测试中AK全场了(其实没啥技术含量,只有5个水题,对,全都很水)

  3. MongoDB Shell db.runCommand

    db.runCommand()示例 我们使用MongoDB Shell登录到mongos,添加Shard节点 [root@100 shard]# /usr/local/mongoDB/bin/mong ...

  4. 大文件断点续传插件webupload插件

    java两台服务器之间,大文件上传(续传),采用了Socket通信机制以及JavaIO流两个技术点,具体思路如下: 实现思路: 1.服:利用ServerSocket搭建服务器,开启相应端口,进行长连接 ...

  5. 腾讯蓝鲸cmdb部署

    蓝鲸配置平台 (CMDB)http://172.16.6.10:8088 环境(单机测试): Centos6 16G 200G 依赖环境: Java 1.8.0_92 python 2.7 ZooKe ...

  6. 将图片文件转成BASE64格式

    html5Reader (file, item) { const reader = new FileReader() reader.onload = (e) => { this.$set(ite ...

  7. jdk8可重复key的Map: IdentityHashMap

    编写一个多条件过滤功能时,想使用map作为过滤条件的容器,由于存在同一健匹配多个值的情况,所以就发现了jdk8的新的map:IdentityHashMap.使用它完美解决了我的问题. 对比Identi ...

  8. Java中的锁原理、锁优化、CAS、AQS详解!

    阅读本文大概需要 2.8 分钟. 来源:jianshu.com/p/e674ee68fd3f 一.为什么要用锁? 锁-是为了解决并发操作引起的脏读.数据不一致的问题. 二.锁实现的基本原理 2.1.v ...

  9. Research Guide: Pruning Techniques for Neural Networks

    Research Guide: Pruning Techniques for Neural Networks 2019-11-15 20:16:54 Original: https://heartbe ...

  10. 在linux上安装运行安卓系统

    一. 环境 Ubuntu 二. 安装QEMU $sudo apt-get install qemu qemu-kvm libvirt-bin 三. 创建虚拟硬盘文件,将安卓安装在此虚拟硬盘上 $qem ...