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 Bean 是通过 @EnableConfigurationProperties 注册的,而不是通过自动扫描注册的,则需要在带有 @EnableConfigurationProperties 注解的 @Configuration 类上指定 @Profile 注解。而在 @ConfigurationProperties 被扫描的情况下,@Profile 可以直接指定在 @ConfigurationProperties 类本身上。
你可以使用 spring.profiles.active Environment 属性来指定哪些 profiles 处于激活状态。你可以通过本章前面描述的任意一种方式来设置该属性。例如,你可以将其包含在你的 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.active 和 spring.profiles.default 只能在非 profile-specific 的文档中使用。这意味着它们不能包含在 profile-specific 文件 中,也不能包含在通过 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"
spring.profiles.active 属性遵循与其他属性相同的排序规则。优先级最高的 PropertySource 获胜。这意味着你可以在 application.properties 中指定激活的 profile,然后通过命令行开关来替换它们。
有关属性源的优先顺序的更多详情,请参见 “外部化配置”。
默认情况下,Spring Boot 中的 profile 名称可以包含字母、数字或允许的字符(-、_、.、+、@)。此外,它们只能以字母或数字开头和结尾。
此限制有助于防止常见的解析问题。然而,如果你希望使用更灵活的 profile 名称,可以在 application.properties 或 application.yaml 文件中将 spring.profiles.validate 设置为 false:
- Properties
- YAML
spring.profiles.validate=false
spring:
profiles:
validate: false
添加 Active Profiles
有时,拥有能够添加到激活的 profile 而非替换它们的属性会很有用。spring.profiles.include 属性可用于在 spring.profiles.active 属性激活的 profile 之上添加额外的激活 profile。SpringApplication 入口点也提供了 Java API 来设置附加的 profile。参见 SpringApplication 中的 setAdditionalProfiles() 方法。
例如,当运行具有以下属性的应用程序时,即使使用 --spring.profiles.active 开关运行,也会激活 common 和 local 配置文件:
- Properties
- YAML
spring.profiles.include[0]=common
spring.profiles.include[1]=local
spring:
profiles:
include:
- "common"
- "local"
包含的 profiles 会在任何 spring.profiles.active 指定的 profiles 之前被添加。
spring.profiles.include 属性会对每个属性源进行处理,因此列表的常规 复杂类型合并规则 不适用。
与 spring.profiles.active 类似,spring.profiles.include 只能用于非特定于 profile 的文档中。这意味着它不能包含在 特定于 profile 的文件 中,也不能包含在通过 spring.config.activate.on-profile 激活的文档 中。
下一节 中描述的 Profile 组也可以用于在给定 profile 处于激活状态时添加 active profiles。
Profile Groups
有时,你在应用程序中定义和使用的配置文件过于细粒度,从而变得难以使用。例如,你可能有 proddb 和 prodmq 两个配置文件,分别用于独立启用数据库和消息功能。
为此,Spring Boot 允许你定义 profile 组。profile 组可以为一组相关的 profile 定义一个逻辑名称。
例如,我们可以创建一个 production 组,该组包含我们的 proddb 和 prodmq 配置文件。
- Properties
- YAML
spring.profiles.group.production[0]=proddb
spring.profiles.group.production[1]=prodmq
spring:
profiles:
group:
production:
- "proddb"
- "prodmq"
现在,我们的应用程序可以通过使用 --spring.profiles.active=production 来启动,从而一次性激活 production、proddb 和 prodmq 这三个 profile。
与 spring.profiles.active 和 spring.profiles.include 类似,spring.profiles.group 只能用于非特定于 profile 的文档中。这意味着它不能包含在 特定于 profile 的文件 中,也不能包含在通过 spring.config.activate.on-profile 激活的文档 中。
以编程方式设置 Profiles
你可以在应用程序运行之前,通过调用 SpringApplication.setAdditionalProfiles(…) 以编程方式设置激活的 profiles。也可以通过使用 Spring 的 ConfigurableEnvironment 接口来激活 profiles。
Profile-specific Configuration Files
特定于 profile 的 application.properties(或 application.yaml)以及通过 @ConfigurationProperties 引用的文件,都会被视为文件并被加载。详情请参见 Profile Specific Files。