Profiles
Spring 配置提供了分离应用程序配置部分的方法,并使其仅在特定环境中可用。任何 @Component
、@Configuration
或 @ConfigurationProperties
都可以用 @Profile
标记,以限制其何时加载,如下例所示:
Spring Profiles provide a way to segregate parts of your application configuration and make it be available only in certain environments.
Any @Component
, @Configuration
or @ConfigurationProperties
can be marked with @Profile
to limit when it is loaded, as shown in the following example:
include-code::ProductionConfiguration[]
如果 |
If |
你可以使用 configprop:spring.profiles.active[] Environment
属性来指定哪些配置处于活动状态。你可以使用本章前面描述的任何方式指定属性。例如,你可以将其包含在 application.properties
中,如下例所示:
You can use a configprop:spring.profiles.active[] Environment
property to specify which profiles are active.
You can specify the property in any of the ways described earlier in this chapter.
For example, you could include it in your application.properties
, as shown in the following example:
spring: profiles: active: "dev,hsqldb"
你还可以使用以下开关在命令行中指定它:--spring.profiles.active=dev,hsqldb
。
You could also specify it on the command line by using the following switch: --spring.profiles.active=dev,hsqldb
.
如果没有任何配置处于活动状态,则会启用默认配置。默认配置的名称是 default
,可以使用 configprop:spring.profiles.default[] Environment
属性对其进行调整,如下例所示:
If no profile is active, a default profile is enabled.
The name of the default profile is default
and it can be tuned using the configprop:spring.profiles.default[] Environment
property, as shown in the following example:
spring: profiles: default: "none"
spring.profiles.active
和 spring.profiles.default
只可用于非特定配置文件的文档中。这意味着它们不能由 spring.config.activate.on-profile
包含在 profile specific files 或 documents activated 中。
spring.profiles.active
and spring.profiles.default
can only be used in non-profile specific documents.
This means they cannot be included in profile specific files or documents activated by spring.config.activate.on-profile
.
例如,第二个文档配置无效:
For example, the second document configuration is invalid:
# this document is valid spring: profiles: active: "prod" --- # this document is invalid spring: config: activate: on-profile: "prod" profiles: active: "metrics"
Adding Active Profiles
configprop:spring.profiles.active[] 属性遵循与其他属性相同的排序规则:最高的 PropertySource
获胜。这意味着你可以在 application.properties
中指定活动配置,然后使用命令行开关 replace 它们。
The configprop:spring.profiles.active[] property follows the same ordering rules as other properties: The highest PropertySource
wins.
This means that you can specify active profiles in application.properties
and then replace them by using the command line switch.
有时,使用可以 add 活动配置文件而不是替换它们的属性很有用。spring.profiles.include
属性可用于在 configprop:spring.profiles.active[] 属性激活的配置之上添加活动配置。SpringApplication
入口点还有一个用于设置附加配置的 Java API。请参阅 setAdditionalProfiles()
中的 SpringApplication 方法。
Sometimes, it is useful to have properties that add to the active profiles rather than replace them.
The spring.profiles.include
property can be used to add active profiles on top of those activated by the configprop:spring.profiles.active[] property.
The SpringApplication
entry point also has a Java API for setting additional profiles.
See the setAdditionalProfiles()
method in SpringApplication.
例如,当运行具有以下属性的应用程序时,即使使用 --spring.profiles.active
开关运行它,也会激活公共配置和本地配置:
For example, when an application with the following properties is run, the common and local profiles will be activated even when it runs using the --spring.profiles.active
switch:
spring: profiles: include: - "common" - "local"
与 spring.profiles.active
类似,spring.profiles.include
只可用于非特定配置文件的文档中。这意味着它不能由 spring.config.activate.on-profile
包含在 profile specific files 或 documents activated 中。
Similar to spring.profiles.active
, spring.profiles.include
can only be used in non-profile specific documents.
This means it cannot be included in profile specific files or documents activated by spring.config.activate.on-profile
.
配置文件组(在 next section 中描述)也可以在给定的配置文件处于活动状态时用于添加活动配置文件。
Profile groups, which are described in the next section can also be used to add active profiles if a given profile is active.
Profile Groups
有时,你在应用程序中定义和使用的配置文件过于细致,使用起来会很麻烦。例如,你可能有 proddb
和 prodmq
配置文件,用于分别启用数据库和消息传递功能。
Occasionally the profiles that you define and use in your application are too fine-grained and become cumbersome to use.
For example, you might have proddb
and prodmq
profiles that you use to enable database and messaging features independently.
为了解决此问题,Spring Boot 允许你定义配置文件组。配置文件组允许你为相关的配置文件组定义一个逻辑名称。
To help with this, Spring Boot lets you define profile groups. A profile group allows you to define a logical name for a related group of profiles.
例如,我们可以创建一个 production
组,其中包含我们的 proddb
和 prodmq
配置文件。
For example, we can create a production
group that consists of our proddb
and prodmq
profiles.
spring: profiles: group: production: - "proddb" - "prodmq"
现在,我们的应用程序可以通过使用 --spring.profiles.active=production
一次激活 production
、proddb
和 prodmq
配置文件来启动。
Our application can now be started using --spring.profiles.active=production
to activate the production
, proddb
and prodmq
profiles in one hit.
Programmatically Setting Profiles
你可以在应用程序运行之前通过调用 SpringApplication.setAdditionalProfiles(…)
以编程方式设置活动配置文件。也可以使用 Spring 的 ConfigurableEnvironment
接口来激活配置文件。
You can programmatically set active profiles by calling SpringApplication.setAdditionalProfiles(…)
before your application runs.
It is also possible to activate profiles by using Spring’s ConfigurableEnvironment
interface.
Profile-specific Configuration Files
application.properties
(或`application.yaml`)的特定配置文件变体和通过`@ConfigurationProperties`引用的文件被认为是文件并被加载。有关详细信息,请参阅“Profile Specific Files”。
Profile-specific variants of both application.properties
(or application.yaml
) and files referenced through @ConfigurationProperties
are considered as files and loaded.
See "Profile Specific Files" for details.