springboot如何通过@PropertySource加载自定义yml文件
作者:mobile18600007978 发布时间:2022-08-06 19:42:56
标签:springboot,@PropertySource,yml
@PropertySource加载自定义yml文件
使用@PropertySource默认加载的是.xml或者 .properties文件,因为在注解源码默认使用的是DefaultPropertySourceFactory实现处理文件内容,spring使用ResourcePropertySource从Resource构建Properties传给Spring。
系统的应用,比如加载自定义的文件,将配置文件内容存储在内存,如下:
那么加载一个自定义的.yml文件,就需要自定义实现ResourcePropertySource来处理yml文件的类
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;
}
}
}
@PropertySource注解对于yml的支持
@PropertySource只对properties文件可以进行加载,但对于yml或者yaml不能支持。
追寻源码。
public class DefaultPropertySourceFactory implements PropertySourceFactory {
public DefaultPropertySourceFactory() {
}
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
return name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource);
}
}
我们只需要继承DefaultPropertySourceFactory类并修改就可以了。
public class YamlConfigFactory extends DefaultPropertySourceFactory {
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
String sourceName = name != null ? name : resource.getResource().getFilename();
if (!resource.getResource().exists()) {
return new PropertiesPropertySource(sourceName, new Properties());
} else if (sourceName.endsWith(".yml") || sourceName.endsWith(".yaml")) {
Properties propertiesFromYaml = loadYml(resource);
return new PropertiesPropertySource(sourceName, propertiesFromYaml);
} else {
return super.createPropertySource(name, resource);
}
}
private Properties loadYml(EncodedResource resource) throws IOException {
YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(resource.getResource());
factory.afterPropertiesSet();
return factory.getObject();
}
}
@PropertySource(value = {"classpath:dog.yml"},factory = YamlConfigFactory.class)
@Component
@ConfigurationProperties(prefix = "dog")
public class Dog {
private String name ;
private String age ;
来源:https://blog.csdn.net/mobile18611667978/article/details/104439706


猜你喜欢
- 步骤:1、创建一个项目,该项目主要用来设计用户控件。2、创建一个用户控件窗体,用来设计用户控件。3、向用户控件窗体中添加一个按钮(butto
- 这几天恰好和朋友谈起了递归,忽然发现不少朋友对于“尾递归”的概念比较模糊,网上搜索一番也没有发现讲解地完整详细的资料,于是写了这么一篇文章,
- 这两天学习了使用Path绘制贝塞尔曲线相关,然后自己动手做了一个类似QQ未读消息可拖拽的小气泡,效果图如下:最终效果图接下来一步一步的实现整
- 本文主要介绍了Java实现雪花算法(snowflake),分享给大家,具体如下:简单描述最高位是符号位,始终为0,不可用。41位的时间序列,
- 本文实例讲述了Java内置观察者模式。分享给大家供大家参考,具体如下:之前也简单地写过观察者模式(又称为发布-订阅模式)小例子,现在项目中也
- 前言前面介绍了APP顶部导航栏AppBar,今天来介绍下Flutter实现APP底部导航栏。我们以仿写微信的底部导航栏来举例说明。要实现类似
- 前言今天我们来讨论一下,程序中的错误处理。在任何一个稳定的程序中,都会有大量的代码在处理错误,有一些业务错误,我们可以通过主动检查判断来规避
- 添加记录后获取主键ID,这是一个很常见的需求,特别是在一次前端调用中需要插入多个表的场景。除了添加单条记录时获取主键值,有时候可能需要获取批
- 前言JDK 1.5 之前 synchronized 的性能是比较低的,但在 JDK 1.5 中,官方推出一个重量级功能 Lock,一举改变了
- 代码很简单,功能也很简单,这里就不多废话了#include<stdio.h>int main(){ char ku[16]={&
- 本文是利用SharpPcap实现网络包的捕获的小例子,实现了端口监控,数据包捕获等功能,主要用于学习分享。什么是SharpPcap?Shar
- 通常 Java 执行 Windows 或者 Linux 的命令时,都是使用 Runtime.getRuntime.exec(com
- 本文实例讲述了C#实现基于XML配置MenuStrip菜单的方法。分享给大家供大家参考。具体如下:1.关于本程序的说明用XML配置MenuS
- SpringBoot 配置SwaggerUI 访问404的小坑。在学习SpringBoot构建Restful API的时候遇到了一个小坑,配
- 现在Java的大部分项目都是基于Maven, 在Maven项目中使用Selenium2. 非常简单。 首先你需要配置好
- 所谓游戏,本质就是提供更逼真的、能模拟某种环境的用户界面,并根据某种规则来响应用户操作。为了提供更逼真的用户界面,需要借助于图形、图像处理。
- 1、Java序列化与反序列化是什么?Java序列化是指把Java对象转换为字节序列的过程,而Java反序列化是指把字节序列恢复为Java对象
- 本文实例讲述了Android API开发之SMS短信服务处理和获取联系人的方法。分享给大家供大家参考,具体如下:Android API支持开
- 本文实例讲述了Android开发之开关按钮控件ToggleButton简单用法。分享给大家供大家参考,具体如下:先来看看运行效果:具体代码如
- 这篇文章主要介绍了mybatis insert返回主键代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,