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 {

	// ...

}
如果通过 @EnableConfigurationProperties 注册 @ConfigurationProperties bean(而不是自动扫描),则需要在带有 @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
# 此文档有效
spring:
  profiles:
    active: "prod"
---
# 此文档无效
spring:
  config:
    activate:
      on-profile: "prod"
  profiles:
    active: "metrics"

spring.profiles.active 属性遵循与其他属性相同的排序规则。 最高的 PropertySource 优先。 这意味着你可以在 application.properties 中指定激活 profile,然后通过命令行参数 替换 它们。

详见 “外部化配置”,了解属性源的优先级顺序。

添加激活的 Profile

有时,希望属性能够*追加*到激活的 profile,而不是替换。 spring.profiles.include 属性可用于在 spring.profiles.active 属性激活的基础上追加激活 profile。 SpringApplication 启动入口也有 Java API 可用于设置附加 profile,详见 SpringApplicationsetAdditionalProfiles() 方法。

例如,应用有如下属性时,即使通过 --spring.profiles.active 启动,common 和 local profile 也会被激活:

  • Properties

  • YAML

spring.profiles.include[0]=common
spring.profiles.include[1]=local
spring:
  profiles:
    include:
      - "common"
      - "local"
包含的 profile 会在所有 spring.profiles.active profile 之前添加。
spring.profiles.include 属性会针对每个属性源处理,因此列表的 复杂类型合并规则不适用。
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 定义一个逻辑名称。

例如,我们可以创建一个包含 proddbprodmq profile 的 production 分组。

  • 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 激活的文档 中。

以编程方式设置 Profile

你可以在应用运行前调用 SpringApplication.setAdditionalProfiles(…​) 以编程方式设置激活的 profile。 也可以通过 Spring 的 ConfigurableEnvironment 接口激活 profile。

Profile 特定的配置文件

application.properties(或 application.yaml)及通过 @ConfigurationProperties 引用的文件都支持 profile 特定变体,并会被加载。 详见 特定于配置文件的文件