詳解Spring Boot配置檔案之多環境配置

2021-09-07 16:27:28 字數 1812 閱讀 6963

一. 多環境配置的好處:

1.不同環境配置可以配置不同的引數~

2.便於部署,提高效率,減少出錯~

二. properties多環境配置

1. 配置啟用選項

spring.profiles.active=dev
2.新增其他配置檔案

#啟用哪乙個環境的配置檔案

spring.profiles.active=dev

#公共配置

spring.jackson.date-format=yyyy-mm-dd hh:mm:ss:

執行的時候還可以制定啟用的環境

三.yaml多環境配置

1.配置啟用選項

spring:

profiles:

active: dev

2.在配置檔案新增三個英文狀態下的短橫線即可區分

---

spring:

profiles: dev

#啟用哪乙個環境的配置檔案

spring:

profiles:

active: prd

#公共配置

spring:

jackson:

date-format: yyyy-mm-dd hh:mm:ss

---spring:

profiles: dev

server:

port: 8081

---spring:

profiles: test

server:

port: 8082

---spring:

profiles: prd

server:

port: 8083

這種情況是報錯的:duplicate key: spring

正確的配置:

#啟用哪乙個環境的配置檔案

#公共配置

spring:

profiles:

active: prd

jackson:

date-format: yyyy-mm-dd hh:mm:ss

---spring:

profiles: dev

server:

port: 8081

---spring:

profiles: test

server:

port: 8082

---spring:

profiles: prd

server:

port: 8083

四.兩種配置方式的比較1. properties配置多環境,需要新增多個配置檔案,yaml只需要乙個配件檔案

yml檔案本身具有區分不同環境的能力

2.書寫格式的差異,yaml相對比較簡潔,優雅

3. yaml的缺點:不能通過@propertysource註解載入。如果需要使用@propertysource註解的方式載入值,那就要使用properties檔案。

講真,還是喜歡properties的配置形式,yml形式的優雅?沒啥感覺~哈哈~

springBoot配置redis詳解

引入依賴 org.springframework.bootgroupid spring boot starter data redisartifactid dependency org.apache.commonsgroupid commons pool2artifactid dependency ...

Spring Boot讀取配置檔案與配置檔案優先順序

spring boot讀取配置檔案 package com.ivan.config.controller import org.springframework.beans.factory.annotation.autowired import org.springframework.core.env...

Spring Boot配置檔案詳解

spring boot的tomcat預設埠號配置 server.port 8080 contextpathtomcat上下文設定 server.servlet.context path myspringboot 配置日誌檔案存放路徑 logging.file users puhanjie downl...