springboot 加载自定义yml文件

本文探讨了SpringBoot加载自定义yml文件的问题,包括ConfigurationProperties注解不再支持locations属性,PropertySource不支持yml加载,以及如何通过YamlPropertiesFactoryBean、YamlMapFactoryBean和PropertySourcePlaceholderConfigurer来加载yml。遇到yml中'---'分隔导致的属性获取问题,提出了两种解决方案:分开到不同文件或调整属性位置。同时提供了相关参考资料。
摘要由CSDN通过智能技术生成

1.  ConfigurationProperties注解的locations属性在1.5.X以后没有了,不能指定locations来加载yml文件

 

2. PropertySource注解不支持yml文件加载,详细见官方文档: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-yaml-shortcomings

 

3. Spring Framework有两个类加载YAML文件,YamlPropertiesFactoryBean和YamlMapFactoryBean

 

4. 可以通过PropertySourcePlaceholderConfigurer来加载yml文件,暴露yml文件到spring environment

 

// 加载YML格式自定义配置文件
	@Bean
	public static PropertySourcesPlaceholderConfigurer properties() {
		PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
		YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
		yaml.setResources(new FileSystemResource("config.yml"));//File引入
//		yaml.setResources(new ClassPathResource("youryml.yml"));//class引入
		configurer.setProperties(yaml.getObjec
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值