在項目開發中經常會用到配置檔案,之前介紹過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源碼 ,擷取這個系列課程的完整源碼。
推薦閱讀:
SpringBoot從入門到精通(二十四)3分鐘搞定Spring Boot 多環境配置! SpringBoot從入門到精通(二十三)Mybatis系列之——實作Mybatis多資料源配置 SpringBoot從入門到精通(二十二)使用Swagger2優雅建構 RESTful API文檔 SpringBoot從入門到精通(二十一)如何優雅的設計 RESTful API 接口版本号,實作 API 版本控制! SpringBoot從入門到精通(二十)快速建構RESTful Web API 服務 SpringBoot從入門到精通(十九)使用注解實作動态Sql、參數傳遞 SpringBoot從入門到精通(十八)Mybatis系列之——使用注解的方式實作背景管理功能 SpringBoot從入門到精通(十七)MyBatis系列之——建立自定義mapper 實作多表關聯查詢! SpringBoot從小白到精通(十六)使用pagehelper實作分頁查詢功能 SpringBoot從小白到精通(十五)實作開發環境熱部署 SpringBoot從小白到精通(十四)使用JdbcTemplate操作資料庫,配置多資料源! SpringBoot從小白到精通(十三)如何實作事務儲存 SpringBoot從小白到精通(十二)logback日志配置 SpringBoot從小白到精通(十一)統一異常處理 SpringBoot從小白到精通(十)使用Interceptor攔截器,一學就會! SpringBoot從小白到精通(九)使用@Async實作異步執行任務 SpringBoot從小白到精通(八)熟悉@EnableScheduling,一秒搞定定時任務 SpringBoot從小白到精通(七)使用Redis實作高速緩存架構 SpringBoot從小白到精通(六)使用Mybatis實作增删改查【附詳細步驟】 SpringBoot從小白到精通(五)Thymeleaf的文法及常用标簽 SpringBoot從小白到精通(四)Thymeleaf頁面模闆引擎 SpringBoot從小白到精通(三)系統配置及自定義配置 SpringBoot從小白到精通(二)如何傳回統一的資料格式 SpringBoot從小白到精通(一)如何快速建立SpringBoot項目