天天看点

spring 读取yaml配置文件

从Spring框架4.1.0增加了对YAML的支持,Spring框架4.1.0 maven POM具有Snakeyaml依赖性  。

您可以在Spring Boot应用中使用两种方式加载YAML:

1、使用Java配置类

spring 读取yaml配置文件

 @Bean
  public static PropertySourcesPlaceholderConfigurer properties() {
      PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
      YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
      yaml.setResources(new ClassPathResource("appConfig.yml");
      propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());
      return propertySourcesPlaceholderConfigurer;
  }      
spring 读取yaml配置文件

2、使用XML Bean配置

spring 读取yaml配置文件
<context:annotation-config/>
 
<bean id="yamlProperties" class="org.springframework.beans.factory.config.YamlPropertiesFactoryBean">
    <property name="resources" value="classpath:appConfig.yml"/>
</bean>
 
<context:property-placeholder properties-ref="yamlProperties"/>      
spring 读取yaml配置文件

转自:https://www.cnblogs.com/EasonJim/p/7583837.html

如果我的文章对你有帮助,就点一下推荐吧.(*^__^*)