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

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

使用屬性文件

自定義屬性

Spring Boot 提供的 SpringApplication 類會搜索並加載 application.properties 文件來獲取配置屬性值。java

建立 application.properties 文件。git

author.realname=梁桂釗
author.nickname=LiangGzone複製代碼

不須要其餘配置,咱們只須要經過 @Value("${屬性名}") 註解來加載對應的配置屬性,如今,經過單元測試用例來驗證吧。github

@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);
}複製代碼

參數引用

此外,咱們來能夠經過引用參數來使用。spring

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

那麼,問題來了, author.intro 經過參數引用獲得的結果是什麼呢?如今,經過單元測試用例來進行測試。springboot

@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]}複製代碼

附上,單元測試用例。app

@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個配置文件。dom

#開發環境
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

(完)

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

相關文章
相關標籤/搜索