Spring Boot 入門系列(二十五)讀取配置文件的幾種方式詳解!


在項目開發中經常會用到配置文件,之前介紹過Spring Boot 資源文件屬性配置的方法,但是很多朋友反饋說介紹的不夠詳細全面。所以, 今天完整的分享Spring Boot讀取配置文件的幾種方式!

Spring Boot 支持多種格式的配置文件格式,目前最常用的配置文件格式是 properties和 yml。所以,這里默認是用.properties文件,其實,yml格式文件的用法也基本類似。Spring Boot 最常用的幾種讀取配置文件的方法:分別是@Value注解,@ConfigurationProperties注解和Environment接口。這三種注解可以配合着@PropertySource來使用。

一、使用@Value注解

使用@Value注解,默認讀取的是application.properties。如果是自定義的配置文件,則需要用 @PropertySource 來指定具體要讀取的配置文件。

1、application.properties 配置文件增加如下配置

# 自定義配置
com.weiz.costum.name=weiz-value
com.weiz.costum.website=www.weiz.com
com.weiz.costum.language=java

 

2、讀取配置

 @Value("${com.weiz.costum.name}")
 private String name;
 @Value("${com.weiz.costum.website}")
 private String website;
 @Value("${com.weiz.costum.language}")
 private String language;

 @RequestMapping("/getvalue")
 public String getValue() {
     System.out.println(name);
     System.out.println(website);
     System.out.println(language);
     return "getvalue";
 }

代碼說明:

1、@Value 為讀取配置的注解。需要配置完整的key路徑。

2、@Value 默認讀取application.properties 文件,如果需要自定義配置文件,需要通過@PropertySource 指定。

上面的代碼,可以把@Value 的相關代碼封裝到單獨的類中,在該類增加@Component注解,然后讀取配置文件。然后在調用的類中注入該類即可。

 

二、使用Environment讀取文件

Environment的使用非常方便,只要在使用的類中注入Environment,就能很方便就讀取到相應的配置。

    @Autowired
    private Environment env;

    @RequestMapping("/getenv")
    public String getEnvironment() {
        System.out.println(env.getProperty("com.weiz.resource.name"));
        System.out.println(env.getProperty("com.weiz.resource.website"));
        System.out.println(env.getProperty("com.weiz.resource.language"));
        return "hello";
    }

代碼說明:

   1、使用Environment無需指定配置文件,獲取的是系統加載的全部配置文件中的配置。

   2、注意配置文件的編碼格式。

 

三、使用@ConfigurationProperties注解

在實際項目中,當項目需要注入的變量值很多時,上述所述的@value 和 Environment 兩種方法會比較繁瑣,這時候我們通常使用基於類型安全的配置方式,將properties屬性和一個Bean關聯在一起,即使用注解@ConfigurationProperties讀取配置文件數據。 

1、增加自定義配置文件

在src\main\resources下新建website.properties配置文件:

com.weiz.resource.name=weiz
com.weiz.resource.website=www.weiz.com
com.weiz.resource.language=java

 

2、增加自定義配置對象類

首先創建WebSiteProperties 自定義配置對象類。然后,使用@ConfigurationProperties 注解將配置文件屬性注入到自定義配置對象類中

package com.weiz.pojo;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ConfigurationProperties(prefix = "com.weiz.resource")
@PropertySource(value = "classpath:website.properties")
public class WebSiteProperties {
    private String name;
    private String website;
    private String language;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getWebsite() {
        return website;
    }

    public void setWebsite(String website) {
        this.website = website;
    }

    public String getLanguage() {
        return language;
    }

    public void setLanguage(String language) {
        this.language = language;
    }
}

代碼說明:

  1、@ConfigurationProperties(prefix = "com.weiz.resource") 綁定屬性,其中prefix表示所綁定的屬性的前綴。

  2、@PropertySource(value = "classpath:website.properties") 指定讀取的配置文件及其路徑。

通過上面的WebSiteProperties類,即可讀取全部對應的配置項。

 

3、使用配置

    @Autowired
    private WebSiteProperties properties;

    @RequestMapping("/getpro")
    public String getProperties() {
        System.out.println(properties.getName());
        System.out.println(properties.getWebsite());
        System.out.println(properties.getLanguage());
        return "hello";
    }

上面的代碼可以看到,使用非常簡單,只需將之前定義的WebSiteProperties 配置類注入即可。

 

四、經驗與坑

在實際項目中,會碰到很多讀取配置文件的業務場景,需要注意各種坑,否則會讓你很惆悵。

  1、yml 文件注意空格和格式縮進。

  2、properties文件默認使用的是iso8859-1。容易出現亂碼問題,如果有中文,如要指定編碼格式。

  3、系統中 yml文件的加載順序高於properties,但是讀取配置信息的時候會讀取后加載。

  4、@PropertySource注解默認只會加載 properties文件,yml 文件這不需要此注解。

  5、@PropertySource注解可以與任何一種方式聯合使用。

  6、簡單值推薦使用@Value,復雜對象推薦使用@ConfigurationProperties。

 

最后

以上,就把Spring Boot如何資源文件屬性配置介紹完了。

這個系列課程的完整源碼,也會提供給大家。大家關注我的微信公眾號(架構師精進),回復:springboot源碼 ,獲取這個系列課程的完整源碼。

 


免責聲明!

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



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