SpringBoot加载自定义yml文件
自定义配置文件(跟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文件的更多相关文章
- springboot加载application.yml文件null
话不多说,直接上代码 本人项目为maven项目 以下是项目结构 pom.xml文件 <?xml version="1.0" encoding="UTF-8" ...
- spring boot 加载自定义log4j 文件路径
spring boot 使用log4j 打印时,需首先去除自带 Logger ,然后加入log4j 依赖 <dependencies> <!-- https://mvnreposit ...
- laravel 自动加载 自定义的文件/辅助函数
需求 在 laravel 中自定义了一些 辅助函数,想要laravel框架自动加载这些函数 实现 将自定义的辅助函数放在helpers.php文件中,如下: 在compsoer.json 的 auto ...
- java web项目启动时自动加载自定义properties文件
首先创建一个类 public class ContextInitListener implements ServletContextListener 使得该类成为一个监听器.用于监听整个容器生命周期的 ...
- SpringBoot系列——加载自定义配置文件
前言 SpringBoot启动时默认加载bootstrap.properties或bootstrap.yml(这两个优先级最高).application.properties或application. ...
- SpringBoot启动如何加载application.yml配置文件
一.前言 在spring时代配置文件的加载都是通过web.xml配置加载的(Servlet3.0之前),可能配置方式有所不同,但是大多数都是通过指定路径的文件名的形式去告诉spring该加载哪个文件: ...
- Springboot默认加载application.yml原理以及扩展
Springboot默认加载application.yml原理以及扩展 SpringApplication.run(...)默认会加载classpath下的application.yml或applic ...
- Eclipse下SpringBoot没有自动加载application.properties文件
Eclipse内创建SpringBoot项目,在java/main/resources文件夹下面创建application.properties配置文件,SpringApplication.run后发 ...
- Springboot 加载配置文件源码分析
Springboot 加载配置文件源码分析 本文的分析是基于springboot 2.2.0.RELEASE. 本篇文章的相关源码位置:https://github.com/wbo112/blogde ...
随机推荐
- 项目(二) esp32-cam 网页图像人脸
https://randomnerdtutorials.com/esp32-cam-video-streaming-face-recognition-arduino-ide/ ESP32-CAM Pi ...
- SecureCRT 日记保存带时间戳
%h:%m:%s:%t--- result:
- 异常0xc000041d的抛出过程
为了说明这个过程,我们必须写一个示例程序,如下: #include "stdafx.h" #include <tchar.h> #include <stdio.h ...
- A1130 | 中缀表达式、查找根节点
代码: #include <stdio.h> #include <memory.h> #include <math.h> #include <string&g ...
- [洛谷P3092]【[USACO13NOV]没有找零No Change】
状压\(DP\) + 二分 考虑构成:\(k<=16\)所以根据\(k\)构造状压\(dp\),将所有硬币的使用情况进行状态压缩 考虑状态:数组\(dp[i]\)表示用\(i\)状态下的硬币可以 ...
- windows cmd编辑文本
echo创建一个空的txt文件:echo.>1.txt这里>表示输出到...echo.表示输出一个空行(即换行)>命令可以扩展为>>表示的意思为附加到...例子:1.tx ...
- 小数据池 is和== 再谈编码
昨日回顾 上节课内容回顾 1. 字典 {key:value, key:value.....} 成对的保存数据 字典没有索引. 不能切片, 字典的key必须是可哈希的.不可变的 1. 增加: dic[新 ...
- Spring事务经典案例-银行转账
1.entity实体类 2.dao层 3.dao实现类 4.service层 5.serviceimpl层 6.大配置.xml <?xml version="1.0" enc ...
- Codeforces 828F Best Edge Weight - 随机堆 - 树差分 - Kruskal - 倍增算法
You are given a connected weighted graph with n vertices and m edges. The graph doesn't contain loop ...
- JPA的查询方法总结
一.使用where条件上一篇我们使用JPA进行了数据源的访问,默认JPA已经实现了好几个接口可以调用.但是,在实际的业务中,查询语句不可避免地需要使用where.order by等语句. 我们用商品数 ...