Spring Boot 配置文件介紹
來源:程序員人生 發布時間:2017-03-01 08:50:56 閱讀次數:3400次
屬性文件使用Application/YAML
Application/YAML 屬性文件,按優先級按高到低排序,位置高的將覆蓋位置低的
1. 當前目錄下的1個/config子目錄
2. 當前目錄
3. 1個classpath下的/config包
4. classpath根路徑(root)
##自定義屬性
#32位隨機字符串
jerome.random.value=${random.value}
#int類型的隨機數字
#jerome.bignumber=${random.long}
#jerome.number.less.than.ten=${random.int(10)}
#jerome.number.in.range=${random.int[1024,65536]}
jerome.random.int=${random.int[1024,65536]}
#自定義名字
#jerome.name=www.jerome.com
#屬性占位符屬性
jerome.desc=${jerome.name} is a domain name
代碼使用
// 32位隨機字符串
@Value(value = "${jerome.random.value}")
private String randomValue;
// int類型的隨機數字
@Value(value = "${jerome.random.int}")
private int randomInt;
// 自定義名字
@Value(value = "${jerome.name}")
private String name;
// 屬性占位符屬性 ${jerome.name} is a domain name
@Value(value = "${jerome.desc}")
private String desc;
/**
* 獲得配置文件的數據
*
* @return Json
*/
@RequestMapping("testProp")
Map<String, Object> testProp() {
Map<String, Object> map = new HashMap<>();
map.put("title", "hello world");
map.put("randomValue", randomValue);
map.put("randomInt", randomInt);
map.put("name", name);
map.put("desc", desc);
return map;
}
返回結果
使用YAML代替Properties(如果Properties和YAML都存在,已Properties為準)
編寫注意:冒號后面要加空格,多級使用tab縮進。
#自定義配置
jerome:
# secret: ${random.value}
# number: ${random.int}
name: www.suzhida.com
desc: ${roncoo.name} is a domain name
#端口
server:
port: 80
#spring jsckson
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: Asia/Chongqing
多環境配置(開發環境、測試壞境、生產環境)
多環境配置的好處
1. 不同環境配置可以配置不同的參數
2. 便于部署,提高效力,減少出錯
Properties多環境配置
1. 主配置激活選項
spring.profiles.active=dev
2.添加其他配置文件
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈