spring 讀取yaml配置文件

 

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

您能夠在Spring Boot應用中使用兩種方式加載YAML:spring

一、使用Java配置類app

複製代碼
 @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;
  }
複製代碼

二、使用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.htmlmaven

相關文章
相關標籤/搜索