配置檔案

Spring 配置檔案提供了一種將應用程式配置的不同部分分離開來,並使其僅在特定環境中可用的方式。任何 @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 屬性來指定哪些配置檔案是啟用的。你可以透過本章前面描述的任何方式來指定該屬性。例如,你可以將其包含在 application.properties 中,如下例所示

  • 屬性

  • YAML

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

你也可以透過命令列使用以下開關來指定:--spring.profiles.active=dev,hsqldb

如果沒有配置檔案被啟用,則會啟用一個預設配置檔案。預設配置檔案的名稱是 default,並且可以透過 spring.profiles.default Environment 屬性進行調整,如下例所示

  • 屬性

  • YAML

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

spring.profiles.activespring.profiles.default 只能用於非特定配置檔案的文件。這意味著它們不能包含在特定配置檔案的檔案或由 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"

spring.profiles.active 屬性遵循與其他屬性相同的排序規則。最高的 PropertySource 獲勝。這意味著你可以在 application.properties 中指定活動的配置檔案,然後使用命令列開關替換它們。

有關屬性源考慮順序的更多詳細資訊,請參閱“外部化配置”

預設情況下,Spring Boot 中的配置檔名稱可以包含字母、數字或允許的字元(-_.+@)。此外,它們只能以字母或數字開頭和結尾。

此限制有助於防止常見的解析問題。但是,如果你更喜歡更靈活的配置檔名,可以在 application.propertiesapplication.yaml 檔案中將 spring.profiles.validate 設定為 false

  • 屬性

  • YAML

spring.profiles.validate=false
spring:
  profiles:
    validate: false

新增活躍配置檔案

有時,擁有新增到活動配置檔案而非替換它們的屬性很有用。spring.profiles.include 屬性可用於在由 spring.profiles.active 屬性啟用的配置檔案之上新增活動配置檔案。SpringApplication 入口點還提供了一個 Java API,用於設定額外的配置檔案。請參閱 SpringApplication 中的 setAdditionalProfiles() 方法。

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

  • 屬性

  • YAML

spring.profiles.include[0]=common
spring.profiles.include[1]=local
spring:
  profiles:
    include:
      - "common"
      - "local"
包含的配置檔案在任何 spring.profiles.active 配置檔案之前新增。
spring.profiles.include 屬性是針對每個屬性源進行處理的,因此列表的常見複雜型別合併規則不適用。
spring.profiles.active 類似,spring.profiles.include 只能用於非特定配置檔案的文件。這意味著它不能包含在特定配置檔案的檔案或由 spring.config.activate.on-profile 啟用的文件中。

配置檔案組(在下一節中描述)也可以用於在給定配置檔案處於活動狀態時新增活動配置檔案。

配置檔案組

有時,你在應用程式中定義和使用的配置檔案過於細化,變得難以使用。例如,你可能有 proddbprodmq 配置檔案,用於獨立啟用資料庫和訊息傳遞功能。

為了解決這個問題,Spring Boot 允許你定義配置檔案組。配置檔案組允許你為相關配置檔案組定義一個邏輯名稱。

例如,我們可以建立一個由 proddbprodmq 配置檔案組成的 production 組。

  • 屬性

  • YAML

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

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

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

以程式設計方式設定配置檔案

你可以在應用程式執行之前呼叫 SpringApplication.setAdditionalProfiles(…​) 以程式設計方式設定活動配置檔案。也可以使用 Spring 的 ConfigurableEnvironment 介面來啟用配置檔案。

特定於配置檔案的配置檔案

application.properties(或 application.yaml)和透過 @ConfigurationProperties 引用的檔案的特定於配置檔案的變體都被視為檔案並載入。有關詳細資訊,請參閱特定於配置檔案的檔案

© . This site is unofficial and not affiliated with VMware.