spring 讀取yaml配置文件


 

從Spring框架4.1.0增加了對YAML的支持,Spring框架4.1.0 maven POM具有Snakeyaml依賴性  。

您可以在Spring Boot應用中使用兩種方式加載YAML:

1、使用Java配置類

復制代碼
 @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;
  }
復制代碼

2、使用XML Bean配置

復制代碼
<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"/>
復制代碼

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM