配置文件

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 特定文件