在專案開發中經常會用到配置檔案,之前介紹過Spring Boot 資原始檔屬性配置的方法,但是很多朋友反饋說介紹的不夠詳細全面。所以, 今天完整的分享Spring Boot讀取配置檔案的幾種方式!
Spring Boot 支援多種格式的配置檔案格式,目前最常用的配置檔案格式是 properties和 yml。所以,這裡預設是用。properties檔案,其實,yml格式檔案的用法也基本類似。Spring Boot 最常用的幾種讀取配置檔案的方法:分別是@Value註解,@ConfigurationProperties註解和Environment 等三種方式。
一、使用@Value註解
使用@Value註解,預設讀取的是application。properties。如果是自定義的配置檔案,則需要用 @PropertySource 來指定具體要讀取的配置檔案。
1、application。properties 配置檔案增加如下配置
# 自定義配置com。weiz。costum。name=weiz-valuecom。weiz。costum。website=www。weiz。comcom。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=weizcom。weiz。resource。website=www。weiz。comcom。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原始碼
,獲取這個系列課程的完整原始碼。