Spring Boot 揭祕與實戰(四) 配置檔案篇 - 有哪些很棒的特性

樑桂釗發表於2016-12-21

Spring 框架本身提供了多種的方式來管理配置屬性檔案。Spring 3.1 之前可以使用 PropertyPlaceholderConfigurer。Spring 3.1 引入了新的環境(Environment)和概要資訊(Profile)API,是一種更加靈活的處理不同環境和配置檔案的方式。不過 Spring 這些配置管理方式的問題在於選擇太多,讓開發人員無所適從。Spring Boot 提供了一種統一的方式來管理應用的配置。
原文地址:Spring Boot 揭祕與實戰(四) 配置檔案篇 - 有哪些很棒的特性
部落格地址:blog.720ui.com/

使用屬性檔案

自定義屬性

Spring Boot 提供的 SpringApplication 類會搜尋並載入 application.properties 檔案來獲取配置屬性值。

建立 application.properties 檔案。

author.realname=樑桂釗
author.nickname=LiangGzone複製程式碼

不需要其他配置,我們只需要通過 @Value("${屬性名}") 註解來載入對應的配置屬性,現在,通過單元測試用例來驗證吧。

@Value("${author.realname}")
private String realname;

@Value("${author.nickname}")
private String nickname;

@Test
public void test1() throws Exception {
    System.out.println("real_name : " + realname);
    System.out.println("nick_name : " + nickname);
}複製程式碼

引數引用

此外,我們來可以通過引用引數來使用。

author.product=Spring Boot 揭祕與實戰
author.project=springboot-action
author.intro=${author.product} | ${author.project} | 作者:${author.realname}複製程式碼

那麼,問題來了, author.intro 通過引數引用得到的結果是什麼呢?現在,通過單元測試用例來進行測試。

@Value("${author.intro}")
private String intro;

@Test
public void test2() throws Exception {
    System.out.println("intro : " + intro);
}複製程式碼

隨機數屬性

Spring Boot 的屬性配置檔案中 ${random} 可以用來生成各種不同型別的隨機值,從而簡化了程式碼生成的麻煩,例如 生成 int 值、long 值或者 string 字串。

# 32位隨機字串
rand.str = ${random.value}
# 隨機int型別
rand.intid = ${random.int}
# 隨機long型別
rand.longid = ${random.long}
# 100以內的隨機int型別
rand.number = ${random.int(100)}
# 0-100範圍內的隨機int型別
rand.range = ${random.int[0,100]}複製程式碼

附上,單元測試用例。

@Value("${rand.str}")
private String randStr;
@Value("${rand.intid}")
private int randIntid;
@Value("${rand.longid}")
private long randLongid;
@Value("${rand.number}")
private int randNumber;
@Value("${rand.range}")
private String randRange;

@Test
public void test3() throws Exception {
    System.out.println("rand.str : " + randStr);
    System.out.println("rand.intid : " + randIntid);
    System.out.println("rand.longid : " + randLongid);
    System.out.println("rand.number : " + randNumber);
    System.out.println("rand.range : " + randRange);
}複製程式碼

application-{profile}.properties引數載入

一個非常典型的場景,多環境配置。Spring Boot 也給我們提供了非常簡化的配置。

現在,我們根據環境建立4個配置檔案。

#開發環境
application-development.properties

#測試環境
application-test.properties

#預生產環境
application-preproduction.properties

#生產環境
application-product.properties複製程式碼

執行命令,通過 active 載入測試環境的配置。

java -jar  ***.jar  --spring.profiles.active=test複製程式碼

YAML檔案

相對於屬性檔案,YAML 檔案是一個更好的配置檔案格式。Spring Boot 提供的 SpringApplication 類也提供了對 YAML 配置檔案的支援。
建立application.yml 檔案

author:
 email: lianggzone@163.com
 blog: http://blog.720ui.com複製程式碼

那麼,我們再來測試一下,是否正常使用哈。

@Value("${author.email}")
private String email;
@Value("${author.blog}")
private String blog;

@Test
public void test4() throws Exception {
    System.out.println("email : " + email);
    System.out.println("blog : " + blog);
}複製程式碼

原始碼

相關示例完整程式碼: springboot-action

(完)

更多精彩文章,盡在「服務端思維」微信公眾號!

Spring Boot 揭祕與實戰(四) 配置檔案篇 - 有哪些很棒的特性

相關文章