天天看點

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

如果我的文章對你有幫助,就點一下推薦吧.(*^__^*)