自定义配置文件(跟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. vue 实战

    vue 实战 Vue命令行工具vue-cli https://www.cnblogs.com/xiaohuochai/p/7277771.html https://github.com/ymblog/ ...

  2. hihocoder#1046: K个串

    [传送门] 这种区间内相同数字只能被统计一次/只有区间内数字都不相同才对答案有贡献的题都可以用扫描线扫右端点,表示当前区间右端点为$r$.然后当前线段树/树状数组维护区间左端点为$[1,r)$时对应的 ...

  3. Linux/Windows 配置config 使用ssh连接

    Linux 产看本地是否有ssh 公私钥 1 cd ~/.ssh 2 ls -a 有的话继续(没有 ssh-keygen 生成) 将公钥内容复制到要连接的服务器用户下 方法一 ssh-copy-id ...

  4. presto-gateway lyft 团队开源的prestodb 的负载均衡、代理、网关工具

    presto-gateway 是 lyft 团队开源 的prestodb 的工具,很方便,我们可以用来方便的管理presto 多集群 通过yaml 进行配置管理,可以方便的管理不同的集群 lyft 参 ...

  5. java自动化配置工具 - autoconfig 简介

    对于java程序员来说各种各样的配置文件是司空见惯的,比如spring的bean配置,struts的action配置等等.有些配置会随着运行环境的变化而各不相同,最典型的就是jdbc驱动的配置,在开发 ...

  6. linux 去掉 ^M 的方法

    在linux上经常遇到这种问题,从网上下载文件到 linux 上后,就多了很多 ^M这种东西,如何集体删除这种东西呢! 用 vim 打开文件 进行如下设置  将文件格式转化为unix :set ff= ...

  7. oracle 使用length()函数需要注意的坑!

      1.情景展示 筛选出指定字段字符长度既不等于18也不等于15的数据. 2.原因分析 第一步:按字符串度进行分组统计: 第二步:筛选数据. 你会发现,只将length=17统计了出来,长度不存在的数 ...

  8. eclipse修改代码没用、debug无法进断点、

    设置eclipse自动编译代码 处理tomcat不进断点问题 添加需要debug的项目 以上操作还是不行的话   可以把tomcat删除重新添加试试看

  9. 【转】gdb typeid 详解

        在揭开typeid神秘面纱之前,我们先来了解一下RTTI(Run-Time Type Identification,运行时类型识别),它使程序能够获取由基指针或引用所指向的对象的实际派生类型, ...

  10. Java8之list<entity>获取实体的某一字段

    示例 List<String> titles = titleList.stream().map(e -> e.get(ConstantUtil.TITLE)).collect(Col ...