Spring Boot 中的配置文件有兩種格式,properties 或者 yaml,通常狀況下,二者能夠任意使用,可是他們仍是有一些本質上的區別的,本文主要介紹yaml 配置,最後再來看看 yaml 和 properties 配置文件的區別java
1.項目根目錄下的config目錄中redis
2.項目根目錄下spring
3. Classpath中的config數組
4. classpathbash
四個位置中的 application.yaml 文件的優先級按照上面列出的順序依次下降。即若是有同一個屬性在四個文件中都出現了,優先級高的爲準。同時,開發者能夠本身指定yaml文件的名字,可是若是自定義以後,須要指定配置文件的位置app
Yaml也支持數組注入,例如ide
my:
servers:
- my1.example.com
- my2.example.com複製代碼
@ConfigurationProperties(prefix="my")
@Component
public class Config {
private List<String> servers = new ArrayList<String>();
public List<String> getServers() {
return this.servers;
}
}複製代碼
redis:
redisConfigs:
- host: 192.168.66.1
port: 1111
- host: 192.168.0.10
port: 1111
複製代碼
@Component
@ConfigurationProperties(prefix = "redis")
public class MyRedis {
private List<SingleRedisConfig> redisConfigs;
}複製代碼
本人創業團隊產品MadPecker,主要作BUG管理、測試管理、應用分發測試
網址:www.madpecker.com,有須要的朋友歡迎試用、體驗! ui
本文爲MadPecker技術人員編寫,轉載請標明出處this