Profiles

Spring Profiles 提供一種區隔應用程式組態部分的方式,並使其僅在特定環境中可用。任何 @Component@Configuration@ConfigurationProperties 都可以使用 @Profile 標記來限制其載入時機,如下列範例所示

  • Java

  • Kotlin

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration(proxyBeanMethods = false)
@Profile("production")
public class ProductionConfiguration {

	// ...

}
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Profile

@Configuration(proxyBeanMethods = false)
@Profile("production")
class ProductionConfiguration {

	// ...

}
如果 @ConfigurationProperties beans 是透過 @EnableConfigurationProperties 而非自動掃描註冊,則需要在具有 @EnableConfigurationProperties 註解的 @Configuration 類別上指定 @Profile 註解。在掃描 @ConfigurationProperties 的情況下,可以在 @ConfigurationProperties 類別本身上指定 @Profile

您可以使用 spring.profiles.active Environment 屬性來指定哪些 profile 處於活動狀態。您可以使用本章前面描述的任何方式指定該屬性。例如,您可以將其包含在您的 application.properties 中,如下列範例所示

  • Properties

  • YAML

spring.profiles.active=dev,hsqldb
spring:
  profiles:
    active: "dev,hsqldb"

您也可以使用以下開關在命令列上指定它:--spring.profiles.active=dev,hsqldb

如果沒有 profile 處於活動狀態,則會啟用預設 profile。預設 profile 的名稱為 default,可以使用 spring.profiles.default Environment 屬性進行調整,如下列範例所示

  • Properties

  • YAML

spring.profiles.default=none
spring:
  profiles:
    default: "none"

spring.profiles.activespring.profiles.default 只能在非特定 profile 的文件中使用。這表示它們不能包含在特定 profile 的檔案或由 spring.config.activate.on-profile 啟用的文件中。

例如,第二個文件組態是無效的

  • Properties

  • YAML

spring.profiles.active=prod
#---
spring.config.activate.on-profile=prod
spring.profiles.active=metrics
# this document is valid
spring:
  profiles:
    active: "prod"
---
# this document is invalid
spring:
  config:
    activate:
      on-profile: "prod"
  profiles:
    active: "metrics"

新增活動 Profiles

spring.profiles.active 屬性遵循與其他屬性相同的排序規則:最高的 PropertySource 獲勝。這表示您可以在 application.properties 中指定活動 profile,然後透過使用命令列開關來取代它們。

有時,擁有新增到活動 profile 而不是取代它們的屬性會很有用。 spring.profiles.include 屬性可用於在 spring.profiles.active 屬性啟用的 profile 之上新增活動 profile。 SpringApplication 進入點也有一個 Java API 用於設定其他 profile。請參閱 SpringApplication 中的 setAdditionalProfiles() 方法。

例如,當執行具有以下屬性的應用程式時,即使它使用 --spring.profiles.active 開關執行,也會啟用 common 和 local profile

  • Properties

  • YAML

spring.profiles.include[0]=common
spring.profiles.include[1]=local
spring:
  profiles:
    include:
      - "common"
      - "local"
spring.profiles.active 類似,spring.profiles.include 只能在非特定 profile 的文件中使用。這表示它不能包含在特定 profile 的檔案或由 spring.config.activate.on-profile 啟用的文件中。

如果在給定的 profile 處於活動狀態時,也可以使用下一節中描述的 Profile 群組來新增活動 profile。

Profile 群組

有時,您在應用程式中定義和使用的 profile 過於細緻,使用起來會很麻煩。例如,您可能擁有 proddbprodmq profile,您使用它們來獨立啟用資料庫和訊息傳遞功能。

為了協助解決此問題,Spring Boot 允許您定義 profile 群組。 Profile 群組允許您為相關的 profile 群組定義邏輯名稱。

例如,我們可以建立一個 production 群組,其中包含我們的 proddbprodmq profile。

  • Properties

  • YAML

spring.profiles.group.production[0]=proddb
spring.profiles.group.production[1]=prodmq
spring:
  profiles:
    group:
      production:
      - "proddb"
      - "prodmq"

現在可以使用 --spring.profiles.active=production 啟動我們的應用程式,一次啟用 productionproddbprodmq profile。

spring.profiles.activespring.profiles.include 類似,spring.profiles.group 只能在非特定 profile 的文件中使用。這表示它不能包含在特定 profile 的檔案或由 spring.config.activate.on-profile 啟用的文件中。

程式化設定 Profiles

您可以在應用程式執行之前呼叫 SpringApplication.setAdditionalProfiles(…​) 以程式化方式設定活動 profile。也可以使用 Spring 的 ConfigurableEnvironment 介面來啟用 profile。

特定 Profile 的組態檔

application.properties(或 application.yaml)和透過 @ConfigurationProperties 引用的檔案的特定 profile 變體被視為檔案並載入。請參閱 特定 Profile 檔案 以取得詳細資訊。