配置檔案

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 Bean 是透過 @EnableConfigurationProperties 註冊而非自動掃描,則 @Profile 註解需要指定在帶有 @EnableConfigurationProperties 註解的 @Configuration 類上。如果 @ConfigurationProperties 是被掃描發現的,則 @Profile 可以指定在 @ConfigurationProperties 類本身上。

你可以使用 spring.profiles.active Environment 屬性來指定哪些 profile 是活動的。你可以透過本章前面描述的任何方式來指定該屬性。例如,你可以將其包含在 application.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 屬性進行調整,如下例所示:

  • 屬性檔案

  • YAML

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

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

例如,第二個文件配置是無效的:

  • 屬性檔案

  • 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"

新增活動 Profile

spring.profiles.active 屬性遵循與其他屬性相同的排序規則:優先順序最高的 PropertySource 獲勝。這意味著你可以在 application.properties 中指定活動的 profile,然後透過命令列開關來替換它們。

有時,擁有能夠新增而非替換活動 profile 的屬性很有用。spring.profiles.include 屬性可用於在由 spring.profiles.active 屬性啟用的 profile 之外新增額外的活動 profile。SpringApplication 入口點也提供了用於設定附加 profile 的 Java API。請參閱 SpringApplication 中的 setAdditionalProfiles() 方法。

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

  • 屬性檔案

  • 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。

  • 屬性檔案

  • 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 啟用的文件中。

程式設計式設定 Profile

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

Profile 特定的配置檔案

application.properties (或 application.yaml) 的 Profile 特定變體以及透過 @ConfigurationProperties 引用的檔案都被視為檔案並載入。詳見Profile 特定檔案