SpringBoot學習筆記(十五)--Profile多環境支持

Profile 是 Spring 對不同環境提供不同配置功能的支持,可以通過激活、指定參數等方式快速切換環境。

配置

方式一:多 Profile 文件

編寫主配置文件的時候,文件名可以是 application-{profile}.properties 或 application-{profile}.yml

有多個配置文件時,默認使用 application.properties 的配置。

示例

配置三個配置文件

  • application-dev.properties
  • application-prod.properties
  • application.properties

application-dev.properties

server.port=8082

application-prod.properties

server.port=80

application.properties

server.port=8081

方式二:使用 yml 的多文檔塊

示例

server:
  port: 8081
spring:
  profiles:
    active: prod
---
server:
  port: 8082
spring:
  profiles: dev
---
server:
  port: 8083
spring:
  profiles: prod

激活

方式一:在配置文件中指定 spring.profiles.active=prod

server.port=8081
spring.profiles.active=prod

方式二:在 yml 文件中的多文檔塊指定

spring:
  profiles:
    active: prod

方式三:運行時傳入 Program arguments

--spring.profiles.active=dev

傳入 Program arguments 後,在 properties 或 yml 中配置的 spring.profiles.active 就失效了。

傳入方式一:配置 IDEA

在這裏插入圖片描述

在這裏插入圖片描述

傳入方式二:在打包後運行時傳入

在這裏插入圖片描述

方式四:運行時傳入 VM options

-Dspring.profiles.active=dev

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章