1、設定檔
1)application.properties(核心設定檔)
test.a= a_propertiestest.b = b_${test.a}
2)application.yml(核心設定檔)
test: a: a.yml b: b_${test.a} c: c.yml d: d_${test.c}
3)other.properties(普通設定檔)
other.config= this is common config!
4)類中讀取屬性值
@Value("${test.a}")private String a;
@Value("${test.b}")private String b;@Value("${test.c}")private String c;@Value("${test.d}")private String d;private static String other;static { ResourceBundle oBundle = ResourceBundle.getBundle("other"); other = oBundle.getString("other.config");}@GetMapping("/test")public String testConfig() { return a + " ; " + b + " ; " + c + " ; " + d + " ; " + other;}
註:application.properties與application.yml都會自動載入,相同配置優先使用application.properties中設定的值 2、切換設定檔 1)預設配置:application.yml
config: description: This is default config!# 設定檔切換#spring:# profiles:# active: test
2)測試環境配置:application-test.yml
config: description: This is test config!
3)生產環境配置:application-prod.yml
config: description: This is produce config!
3、設定context-path和連接埠號碼
#設定context-path和連接埠號碼,預設為"/"和8080server: context-path: /v port: 18080