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

Spring Boot 支援多種格式的配置檔案格式,目前最常用的配置檔案格式是 properties和 yml。所以,這裡預設是用.properties檔案,其實,yml格式檔案的用法也基本類似。Spring Boot 最常用的幾種讀取配置檔案的方法:分別是@Value註解,@ConfigurationProperties註解和Environment介面。這三種註解可以配合著@PropertySource來使用。

一、使用@Value註解

使用@Value註解,預設讀取的是application.properties。如果是自定義的配置檔案,則需要用 @PropertySource 來指定具體要讀取的配置檔案。

1、application.properties 配置檔案增加如下配置

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

2、讀取配置

  1. @Value("${com.weiz.costum.name}")
  2. private String name;
  3. @Value("${com.weiz.costum.website}")
  4. private String website;
  5. @Value("${com.weiz.costum.language}")
  6. private String language;
  7.  
  8. @RequestMapping("/getvalue")
  9. public String getValue() {
  10. System.out.println(name);
  11. System.out.println(website);
  12. System.out.println(language);
  13. return "getvalue";
  14. }

程式碼說明:

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

2、@Value 預設讀取application.properties 檔案,如果需要自定義配置檔案,需要通過@PropertySource 指定。

上面的程式碼,可以把@Value 的相關程式碼封裝到單獨的類中,在該類增加@Component註解,然後讀取配置檔案。然後在呼叫的類中注入該類即可。

二、使用Environment讀取檔案

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

  1. @Autowired
  2. private Environment env;
  3.  
  4. @RequestMapping("/getenv")
  5. public String getEnvironment() {
  6. System.out.println(env.getProperty("com.weiz.resource.name"));
  7. System.out.println(env.getProperty("com.weiz.resource.website"));
  8. System.out.println(env.getProperty("com.weiz.resource.language"));
  9. return "hello";
  10. }

程式碼說明:

  1、使用Environment無需指定配置檔案,獲取的是系統載入的全部配置檔案中的配置。

   2、注意配置檔案的編碼格式。

三、使用@ConfigurationProperties註解

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

1、增加自定義配置檔案

在src\main\resources下新建website.properties配置檔案:

  1. com.weiz.resource.name=weiz
  2. com.weiz.resource.website=www.weiz.com
  3. com.weiz.resource.language=java

2、增加自定義配置物件類

首先建立WebSiteProperties 自定義配置物件類。然後,使用@ConfigurationProperties 註解將配置檔案屬性注入到自定義配置物件類中

  1. package com.weiz.pojo;
  2.  
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.context.annotation.PropertySource;
  6.  
  7. @Configuration
  8. @ConfigurationProperties(prefix = "com.weiz.resource")
  9. @PropertySource(value = "classpath:website.properties")
  10. public class WebSiteProperties {
  11. private String name;
  12. private String website;
  13. private String language;
  14.  
  15. public String getName() {
  16. return name;
  17. }
  18.  
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22.  
  23. public String getWebsite() {
  24. return website;
  25. }
  26.  
  27. public void setWebsite(String website) {
  28. this.website = website;
  29. }
  30.  
  31. public String getLanguage() {
  32. return language;
  33. }
  34.  
  35. public void setLanguage(String language) {
  36. this.language = language;
  37. }
  38. }

程式碼說明:

  1、@ConfigurationProperties(prefix = "com.weiz.resource") 繫結屬性,其中prefix表示所繫結的屬性的字首。

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

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

3、使用配置

  1. @Autowired
  2. private WebSiteProperties properties;
  3.  
  4. @RequestMapping("/getpro")
  5. public String getProperties() {
  6. System.out.println(properties.getName());
  7. System.out.println(properties.getWebsite());
  8. System.out.println(properties.getLanguage());
  9. return "hello";
  10. }

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

四、經驗與坑

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

  1、yml 檔案注意空格和格式縮排。

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

  3、系統中 yml檔案的載入順序高於properties,但是讀取配置資訊的時候會讀取後加載。

  4、@PropertySource註解預設只會載入 properties檔案,yml 檔案這不需要此註解。

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

  6、簡單值推薦使用@Value,複雜物件推薦使用@ConfigurationProperties。

最後

以上,就把Spring Boot如何資原始檔屬性配置介紹完了。

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