一、profile的多檔案配置方式
1、profile檔案方式:提供多個組態檔,每個代表一種環境
如:
1.application-dev.properties/yml 開發環境
2.application-test.properties/yml 測驗環境
3.application-pro.properties/yml 生產環境
(1)application-dev.properties/yml代碼塊:
#開發環境
server:
port: 8082
name: 開發環境
(2)application-test.properties/yml代碼塊:
#測驗環境
server:
port: 8081
name: 測驗環境
(3)application-pro.properties/yml代碼塊:
#生產環境
server:
port: 8083
name: 生產環境
2、在組態檔中激活profile
(1)application.yaml代碼塊:
spring:
profiles:
active: dev
name: 默認環境
(2)測驗:
1.新建ProfileController JavaClass

2.添加測驗資料
@RestController
public class ProfileController {
@Value("${name}")
private String name;
@RequestMapping("/profile")
public String profile(){
return name;
}
}
3.測驗結果

二、profile的單檔案配置方式
1、用 — — —來劃分多個配置
代碼塊:
#Spring學習(四)激活組態檔
#
spring:
profiles:
active: dev
name: 默認環境
---
#開發環境
server:
port: 8877
name: 開發環境
spring:
config:
activate:
on-profile: dev2 #當前編號名稱
---
#測驗環境
server:
port: 8086
name: 測驗環境
spring:
config:
activate:
on-profile: test2 #當前編號名稱
---
#生產環境
server:
port: 8888
name: 生產環境
spring:
config:
activate:
on-profile: pro2 #當前編號名稱
2、然后一樣同上面多檔案一樣激活
2、運行結果
測驗active: dev2
結果:

三、profile的虛擬機引數配置方式
1、打開Edit Configurations

2、設定引數

3、結果:

四、profile的命令列引數引數配置方式
1、打開Edit Configurations

2、設定引數 --spring.profiles.active=xxx

3、結果

4、優先級結論
file:./config/:當前專案下的/config目錄下 >file:./ :當前專案的根目錄>
classpath:/config/:classpath的/config目錄>命令列引數>虛擬機引數>檔案配置

注意:當用Maven打包專案時,file:./config/:當前專案下的/config目錄下 、file:./ :當前專案的根目錄不會被打包進去
所以我們可以在打包好后解壓重新放一個yaml檔案進去,然后在宣告組態檔的埠號
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/458205.html
標籤:Java
上一篇:Java自增
