两种SpringBoot加载YML配置文件的方法
2018-02-17 21:06
1001 查看
SpringBoot默认支持properties和YAML两种格式的配置文件。前者格式简单,但是只支持键值对。如果需要表达列表,最好使用YAML格式。SpringBoot支持自动加载约定名称的配置文件,例如
在网上搜到了这样一个方法。
application.yml。如果是自定义名称的配置文件,就要另找方法了。可惜的是,不像前者有
@PropertySource这样方便的加载方式,后者的加载必须借助编码逻辑来实现。
YamlPropertiesFactoryBean
SpringBoot文档中给出了YamlPropertiesFactoryBean和
YamlMapFactoryBean两个类实现YAML配置文件的加载。前者将文件加载为Properties,后者为Map。
在网上搜到了这样一个方法。
package com.example.demo.config; import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.core.io.ClassPathResource; @Configuration public class BootstrapConfig { @Bean public static PropertySourcesPlaceholderConfigurer properties() { PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer(); YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean(); yaml.setResources(new ClassPathResource("config/appprops.yml")); propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject()); return propertySourcesPlaceholderConfigurer; } }
YamlPropertySourceLoader
文档中还提到了YamlPropertySourceLoader,但是没有给出实现的代码。今天看着API摸索了一下,找到了方案。
package com.example.demo.config; import org.springframework.boot.env.YamlPropertySourceLoader; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.io.ClassPathResource; import java.io.IOException; @Configuration public class BootstrapConfig { @Bean public PropertySourcesPlaceholderConfigurer properties2() { PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); YamlPropertySourceLoader loader = new YamlPropertySourceLoader(); MutablePropertySources sources = new MutablePropertySources(); try { sources.addLast(loader.load("db", new ClassPathResource("config/appprops.yml"), null)); } catch (IOException e) { e.printStackTrace(); } configurer.setPropertySources(sources); return configurer; } }
简单总结
前者不抛异常,看着更舒服;而后者可以支持同时加载多个YAML文件。相关文章推荐
- spring boot启动时加载外部配置文件的方法
- springBoot使用Controller读取配置文件两种方式&读取自定义配置方法
- 详解Spring Boot加载properties和yml配置文件
- spring-boot读取props和yml配置文件的方法
- Hibernate加载配置文件的两种方法
- Spring加载配置文件的两种方式
- spring-boot 加载本地静态资源文件路径配置
- spring加载配置文件的两种方式
- 加载spring配置文件的常用三种方法
- hibernate加载配置文件的两种方法
- spring-boot 加载本地静态资源文件路径配置
- spring-boot 加载本地静态资源文件路径配置
- 加载spring配置文件的常用三种方法
- spring 配置hibernate映射文件的两种方法
- spring-boot 加载本地静态资源文件路径配置
- hibernate加载配置文件的两种方法
- spring 中加载xml配置文件的方法
- spring-boot 加载本地静态资源文件路径配置
- springboot配置文件加载不到的问题
- spring加载配置文件的 两种方式