Spring Boot 設定檔詳解:Properties和YAML

來源:互聯網
上載者:User

標籤:一個   file   寫法   格式   冒號   環境變數   random   domain   ring   

 

 

 

 

一.設定檔的生效順序,會對值進行覆蓋: 
1. @TestPropertySource 註解 
2. 命令列參數 
3. Java系統屬性(System.getProperties()) 
4. 作業系統環境變數 
5. 只有在random.*裡包含的屬性會產生一個RandomValuePropertySource 
6. 在打包的jar外的應用程式設定檔(application.properties,包含YAML和profile變數) 
7. 在打包的jar內的應用程式設定檔(application.properties,包含YAML和profile變數) 
8. 在@Configuration類上的@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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.