Spring Boot 詳解配置文件
1. 前言
Spring Boot 可以在零配置的情況下使用,但是不代表 Spring Boot 完全不需要配置文件。
舉一個(gè)簡(jiǎn)單的例子, Spring Boot 開(kāi)發(fā)的 Web 項(xiàng)目默認(rèn)的啟動(dòng)端口是 8080 。如果我們想更換啟動(dòng)端口,通過(guò)配置文件去修改是比較好的。如果放到代碼中,修改一個(gè)端口都要重新編譯下程序,豈不煩哉?
配置文件不是必須的,但是如果想實(shí)現(xiàn)一些個(gè)性化的功能,還是需要用到配置文件的。本篇就講下 Spring Boot 中使用配置文件的常用場(chǎng)景。
2. 構(gòu)建演示 Web 項(xiàng)目
為了便于演示,我們先構(gòu)建一個(gè) Web 項(xiàng)目。
2.1 使用 Spring Initializr 構(gòu)建一個(gè) Spring Boot 應(yīng)用
Spring Boot 版本選擇 2.2.5 , Group 為 com.imooc
, Artifact 為 spring-boot-profile
,生成項(xiàng)目后導(dǎo)入 Eclipse 開(kāi)發(fā)環(huán)境。
2.2 修改 pom.xml
引入 Web 項(xiàng)目依賴,同時(shí)開(kāi)啟熱部署便于修改測(cè)試。
實(shí)例:
<!-- 熱部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.3 編寫(xiě)控制器用于測(cè)試
實(shí)例:
@RestController // 標(biāo)注為控制器,且返回值序列化為json
public class HelloController {
@GetMapping("/hello") // 響應(yīng)get請(qǐng)求,匹配的請(qǐng)求路徑為/hello
public Map hello() {
Map<String, String> map = new HashMap<String, String>();
map.put("test", "content for test");
return map;
}
}
2.4 啟動(dòng)項(xiàng)目
訪問(wèn) http://127.0.0.1:8080/hello
,效果如下:
3. 修改項(xiàng)目啟動(dòng)配置
我們運(yùn)行啟動(dòng)類,啟動(dòng) spring-boot-profile
應(yīng)用,控制臺(tái)會(huì)發(fā)現(xiàn)如下提示:
Tomcat started on port(s): 8080 (http) with context path ''
可以看出, Spring Boot 應(yīng)用默認(rèn)啟動(dòng)端口是 8080 ,默認(rèn)項(xiàng)目路徑是空。
我們可以通過(guò)修改 resources/application.properties
來(lái)自定義項(xiàng)目啟動(dòng)配置:
實(shí)例:
# 啟動(dòng)端口
server.port=8000
# 項(xiàng)目路徑
server.servlet.context-path=/spring-boot-profile
再次啟動(dòng)應(yīng)用,控制臺(tái)提示變?yōu)椋?/p>
Tomcat started on port(s): 8000 (http) with context path '/spring-boot-profile'
此時(shí)項(xiàng)目對(duì)應(yīng)的訪問(wèn)路徑為: http://127.0.0.1:8000/spring-boot-profile
, 使用瀏覽器訪問(wèn)效果如下:
4. 配置文件格式
Spring Boot 支持兩種格式的配置文件,即 .properties
文件和 .yml
配置文件。
上面的配置使用 .yml
則為:
實(shí)例:
server:
port: 8000
servlet:
context-path: /spring-boot-profile
.properties
配置使用頓號(hào)分割語(yǔ)義,而 .yml
配置使用縮進(jìn)分割語(yǔ)義。這兩種配置文件沒(méi)有本質(zhì)區(qū)別,只是格式不同。
5. 自定義配置項(xiàng)
我們還可以在配置文件中使用自定義配置,例如我們開(kāi)發(fā)了一個(gè)微信公眾號(hào)后臺(tái)應(yīng)用,需要在程序中配置公眾號(hào)的 appid 和 secret 。
配置文件如下:
實(shí)例:
# 公眾號(hào)appid
wxmp.appid=111
# 公眾號(hào)secret
wxmp.secret=222
我們定義一個(gè)組件,通過(guò) @Value
注解注入配置項(xiàng)的值。
實(shí)例:
/**
* 微信公眾號(hào)參數(shù)
*/
@Component//注冊(cè)為組件
public class WxMpParam {
@Value("${wxmp.appid}")//注入wxmp.appid配置項(xiàng)
private String appid;
@Value("${wxmp.secret}")//注入wxmp.secret配置項(xiàng)
private String secret;
//省略get set方法
}
通過(guò)控制器測(cè)試配置項(xiàng)是否注入成功。
實(shí)例:
@RestController
public class HelloController {
@Autowired
private WxMpParam wxMpParam;
@GetMapping("/hello")
public Map hello() {
Map<String, String> map = new HashMap<String, String>();
map.put("appid",wxMpParam.getAppid());
map.put("secret",wxMpParam.getSecret());
return map;
}
}
此時(shí)我們?cè)L問(wèn) http://127.0.0.1:8000/spring-boot-profile/hello
,瀏覽器顯示如下,說(shuō)明我們的配置注入成功。
6. 配置項(xiàng)自動(dòng)注入對(duì)象
如果參數(shù)很多,一一指定對(duì)象屬性和配置項(xiàng)的關(guān)聯(lián)非常麻煩??梢酝ㄟ^(guò)設(shè)定對(duì)象與配置項(xiàng)的對(duì)應(yīng)關(guān)系,來(lái)實(shí)現(xiàn)配置項(xiàng)的自動(dòng)注入。
實(shí)例:
@Component // 注冊(cè)為組件
@EnableConfigurationProperties // 啟用配置自動(dòng)注入功能
@ConfigurationProperties(prefix = "wxmp") // 指定類對(duì)應(yīng)的配置項(xiàng)前綴
public class WxMpParam {
private String appid;// 對(duì)應(yīng)到wxmp.appid
private String secret; // 對(duì)應(yīng)到wxmp.secret
//省略 get set
}
在上面的代碼中,通過(guò) prefix = "wxmp"
指定了關(guān)聯(lián)配置的前綴,屬性 appid 即關(guān)聯(lián)到前綴 + 屬性名為 wxmp.appid 的配置項(xiàng)。同理,屬性 secret 關(guān)聯(lián)到 wxmp.secret 配置項(xiàng)。
7. 在配置文件中使用隨機(jī)數(shù)
配置文件中使用隨機(jī)數(shù)也是比較常見(jiàn)的場(chǎng)景,尤其啟動(dòng)多個(gè)客戶端時(shí),希望指定一個(gè)啟動(dòng)端口的范圍,例如 10 - 20 ,可配置如下:
實(shí)例:
# 配置端口為1-20間的隨機(jī)數(shù)
server.port=${random.int[10,20]}
這樣我可以連續(xù)啟動(dòng)四個(gè)客戶端,啟動(dòng)端口分別是 12 、 13 、 17 、 19 ,可見(jiàn)是隨機(jī)的,而且在我指定的范圍內(nèi)波動(dòng)。
8. 自定義配置文件
有時(shí)候參數(shù)太多,都放到一個(gè)配置文件中太亂了,我們會(huì)希望將配置分到不同文件中,然后每個(gè)文件保存不同配置。
例如上面微信公眾號(hào)配置,我們單獨(dú)建立一個(gè) wxmp.properties
文件,內(nèi)容如下:
實(shí)例:
# wxmp.properties配置文件
# 公眾號(hào)的appid
wxmp.appid=111
# 公眾號(hào)的secret
wxmp.secret=222
WxMpParam 代碼如下:
實(shí)例:
/**
* 微信公眾號(hào)參數(shù)
*/
@Component // 注冊(cè)為組件
@PropertySource(value = "classpath:wxmp.properties", encoding = "utf-8") // 指定配置文件及編碼
public class WxMpParam {
@Value("${wxmp.appid}")
private String appid;
@Value("${wxmp.secret}")
private String secret;
}
9. 配置項(xiàng)引用
Spring Boot 配置項(xiàng)是可以引用其他配置項(xiàng)的值的,這個(gè)稍微提一下,例如:
實(shí)例:
# wxmp.properties
# 公眾號(hào)的appid
wxmp.appid=111
# 公眾號(hào)的secret,值為111222
wxmp.secret=${wxmp.appid}222
10. 小結(jié)
對(duì)一個(gè) Spring Boot 應(yīng)用而言。
-
如果配置項(xiàng)比較少,直接全部寫(xiě)在
application.properties
。 -
如果配置項(xiàng)很多,可以劃分為若干配置文件。
-
如果很多自定義配置擁有相同的前綴,可以指定前綴,讓配置項(xiàng)自動(dòng)注入對(duì)象中。
-
Spring Boot 提供了多變的配置文件使用機(jī)制,我們根據(jù)具體場(chǎng)景靈活使用即可。