springboot怎么通过@PropertySource加载自定义yml文件
发表于:2025-11-19 作者:千家信息网编辑
千家信息网最后更新 2025年11月19日,小编给大家分享一下springboot怎么通过@PropertySource加载自定义yml文件,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我
千家信息网最后更新 2025年11月19日springboot怎么通过@PropertySource加载自定义yml文件
小编给大家分享一下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 ;以上是"springboot怎么通过@PropertySource加载自定义yml文件"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!
文件
内容
篇文章
注解
源码
处理
支持
不怎么
内存
大部分
更多
知识
系统
行业
资讯
资讯频道
频道
参考
存储
学习
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
关于网络安全建设方案
软件开发行业就业前景报告
深圳培训软件开发机构
上海大宗商品交易软件开发
高淳区智能化软件开发排名靠前
企业软件开发之类文章
网络安全第1条
黄浦区立体化软件开发代理品牌
肇庆微信软件开发公司
新奥尔良方法数据库
软件开发创新创业计划书模板
部队计算网络技术
网络安全教育重要性
jpress 数据库表
建筑安全风险数据库免费
生僻字取名软件开发
浙江h5多媒体软件开发
机算计网络安全的发展趋势
数据库外键的取值
如何查看服务器版本
数据库采样周期ms
华为软件开发概念
关于如何维护网络安全背景图
高级网络安全待遇
肇庆微信软件开发公司
药物不良反应报告数据库
软件开发面试准备思维导图
数据库怎么增加文件
预防网络安全问题提两件建议
云服务器最大