天天看點

Spring Boot(二):Spring Boot中的配置參數

Spring Boot 配置參數

Spring Boot 幫助我們完成了許許多多的自動化配置 如果我們需要根據自己的需求修改配置 也是可以的

可以使用.properties 和 .yml 格式配置 這裡隻列出幾個常用的配置項 具體大家可以去官網查詢

配置檔案

Key值 預設值 描述
server.address 伺服器的網絡位址
server.port 8080 端口
server.error.path /error 當系統出錯時需要跳轉的位址
server.error.whitelabel.enabled true 是否使用預設的錯誤頁面
server.max-http-header-size 8KB HTTP請求頭容量限制
server.servlet.application-display-name application 應用名稱
server.servlet.session.cookie.name Session名稱
server.servlet.session.cookie.path Session路徑
server.servlet.session.timeout 30m Session逾時時間
spring.config.additional-location 需要額外添加配置檔案的位址
spring.config.location 預設配置檔案的位址
spring.config.name 配置檔案名稱

PS:這裡并沒有列出 例如資料庫配置 日志 等相關的配置 會在以後的文章中講到相關内容時添加

在我們實際項目的開發中 我們一般分為本地 開發 測試 仿真 生産等環境(根據不同公司 會有不同的階段) 這些環境往往都會有一下差異例如資料庫的連結位址 檔案的上傳位址 等等 我們不可能為了根據釋出的環境 經常去該配置檔案 是以Spring也給我們提供了 根據不同環境 啟用不同的配置檔案的功能
首先在resources下面建立幾個自己需要的配置檔案 有幾個配置檔案 我們配置幾個 檔案以application-環境命名
Spring Boot(二):Spring Boot中的配置參數

然後再 application.properties 中添加 spring.profeiles.active = prod

啟動檢視日志

Spring Boot(二):Spring Boot中的配置參數
java -jar xxxxx.jar --spring.profiles.active=dev 
           
java -jar xxxxx.jar --spring.profiles.active=dev,local 
           
JAVA_OPTS="-Dspring.profiles.active=prod"