Spring Boot加载yaml配置到Bean

Spring Boot可能遇到项目启动无法加载yaml配置的情况,可以使用如下配置注入PropertySourcesPlaceholderConfigurer

1
2
3
4
5
6
7
8
@Bean
public static PropertySourcesPlaceholderConfigurer properties() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
yaml.setResources(new ClassPathResource("xxx.yml"));
configurer.setProperties(yaml.getObject());
return configurer;
}