Spring/SpringBoot中使用yaml/yml配置文件

需求

在使用Spring/SpringBoot的過程中,我們總會(huì)使用到各種自定義配置,不可能把所有的配置都寫到application.yml中,此時(shí)我們可以將某一部分自定義配置單獨(dú)用一個(gè)文件提取出來,Spring也提供了相應(yīng)的解決方案。

例子

  • test.yml
huaiku:
  email: test@163.com
  address: 中華人民共和國
  • 使用
public class YmlConfigTestBean {
    private String email;
    private String address;
    
    public YmlConfigTestBean(String email,String address) {
        this.address = address;
        this.email = email;
    }
    
    public void print() {
        System.out.println(String.format("Email:%s,Address:%s", this.email,this.address));
    }
}
  • 配置
@Configuration
public class ApplicationConfigurations {
    
    @Bean public PropertyPlaceholderConfigurer yamlPropertyPlaceholderBean() {
        // 解析 yaml
        YamlPropertiesFactoryBean yamlProperty = new YamlPropertiesFactoryBean();
        yamlProperty.setResources(new ClassPathResource("test.yml"));
        // 配置 PropertyPlaceholder
        PropertyPlaceholderConfigurer yamlPropertyPlaceholder = new PropertyPlaceholderConfigurer();
        yamlPropertyPlaceholder.setProperties(yamlProperty.getObject());
        yamlPropertyPlaceholder.setFileEncoding("UTF-8");
        
        return yamlPropertyPlaceholder;
    }
    
    @Bean public YmlConfigTestBean ymlConfigTestBean(@Value("${huaiku.email}")String email,@Value("${huaiku.address}")String address) {
        return new YmlConfigTestBean(email,address);
    }
}
  • 測試
@SpringBootApplication
public class SampleApplication {
    private static final Logger logger = LoggerFactory.getLogger(SampleApplication.class);
    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }

    @Bean public CommandLineRunner test(final YmlConfigTestBean bean) {
        return (args)-> {
            logger.info("打印....");
            bean.print();
        };
    }
}
  • 結(jié)果
2018-12-06 17:06:32,312 INFO :-- [main .. ] o.s.SampleApplication 打印.... 
Email:test@163.com,Address:中華人民共和國
  • 結(jié)語

此處提供的是基于Java注解的配置,在XML配置中把@Bean 注冊(cè)的類改成XML注冊(cè)的類便可。

  • 補(bǔ)充

在項(xiàng)目中使用yml配置需要添加相關(guān)依賴yaml和yml依賴有所不同

  1. yml
<dependency>
      <groupId>com.fasterxml.jackson.dataformat</groupId>
      <artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
  1. yaml
<dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
</dependency>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,869評(píng)論 18 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 46,941評(píng)論 6 342
  • 1.1 spring IoC容器和beans的簡介 Spring 框架的最核心基礎(chǔ)的功能是IoC(控制反轉(zhuǎn))容器,...
    simoscode閱讀 6,749評(píng)論 2 22
  • 內(nèi)容過長,core部分分開發(fā)布,core章節(jié)第二部分點(diǎn)擊:Spring Framework 官方文檔中文版—Cor...
    kopshome閱讀 33,780評(píng)論 7 24
  • 本文是我自己在秋招復(fù)習(xí)時(shí)的讀書筆記,整理的知識(shí)點(diǎn),也是為了防止忘記,尊重勞動(dòng)成果,轉(zhuǎn)載注明出處哦!如果你也喜歡,那...
    波波波先森閱讀 12,319評(píng)論 6 86