1. 程式人生 > >Spring Boot 配置文件詳解:Properties和YAML

Spring Boot 配置文件詳解:Properties和YAML

列表 config 其他 操作系統 des num mat 變量 onf

一.配置文件的生效順序,會對值進行覆蓋:
1. @TestPropertySource 註解
2. 命令行參數
3. Java系統屬性(System.getProperties())
4. 操作系統環境變量
5. 只有在random.*裏包含的屬性會產生一個RandomValuePropertySource
6. 在打包的jar外的應用程序配置文件(application.properties,包含YAML和profile變量)
7. 在打包的jar內的應用程序配置文件(application.properties,包含YAML和profile變量)
8. [email protected]

/* */@PropertySource註解
9. 默認屬性(使用SpringApplication.setDefaultProperties指定)

二.配置隨機值
roncoo.secret=${random.value}
roncoo.number=${random.int}
roncoo.bignumber=${random.long}
roncoo.number.less.than.ten=${random.int(10)}
roncoo.number.in.range=${random.int[1024,65536]}

讀取使用註解:@Value(value = "${roncoo.secret}")

註:出現黃點提示,是要提示配置元數據,可以不配置


三.屬性占位符
當application.properties裏的值被使用時,它們會被存在的Environment過濾,所以你能夠引用先前定義的值(比如,系統屬性)。
roncoo.name=www.roncoo.com
roncoo.desc=${roncoo.name} is a domain name

四.Application屬性文件,按優先級排序,位置高的將覆蓋位置低的
1. 當前目錄下的一個/config子目錄
2. 當前目錄
3. 一個classpath下的/config包
4. classpath根路徑(root)

這個列表是按優先級排序的(列表中位置高的將覆蓋位置低的)

五. 配置應用端口和其他配置的介紹
#端口配置:
server.port=8090
#時間格式化
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
#時區設置
spring.jackson.time-zone=Asia/Chongqing

六. 使用YAML代替Properties
註意寫法:冒號後要加個空格

Spring Boot 配置文件詳解:Properties和YAML