# 7.3.Profile配置

Spring Profiles 提供了一种分离应用程序配置部分并使其仅在某些环境中可用的方法。任何`@Component`，`@Configuration`或`@ConfigurationProperties`注解都可以在加载时使用`@Profile`标记进行限制，如下例所示：

```java
@Configuration(proxyBeanMethods = false)
@Profile("production")
public class ProductionConfiguration {

    // ...

}
```

如果bean是通过`@ConfigurationProperties`注册而不是通过`@EnableConfigurationProperties`自动扫描注册的，则需要在`@EnableConfigurationProperties`中有`@Configuration`注解的类上指定`@Profile`注解。在被`@ConfigurationProperties`扫描的情况下，`@Profile`可以在`@ConfigurationProperties`类本身上指定。 您可以使用`spring.profiles.active` `Environment`属性来指定哪些配置文件处于活动状态。您可以通过本章前面描述的任何方式指定属性。例如，您可以将它包含在您的`application.properties`中，如以下示例所示：

```properties
spring.profiles.active=dev,hsqldb
```

您还可以使用以下开关在命令行上指定它：`--spring.profiles.active=dev,hsqldb`.

如果没有配置文件处于活动状态，则启用默认配置文件。默认配置文件的名称是`default`，并且可以使用`Environment`的`spring.profiles.default`属性进行调整，如以下示例所示：

```properties
spring.profiles.default=none
```

`spring.profiles.active`和`spring.profiles.default`只能在非配置文件特定文档中使用。这意味着它们不能通过`spring.config.activate.on-profile`包含在[配置文件](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.files.activation-properties)或[特定配置文件](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.files.profile-specific)中.

例如第二个文档配置无效：

```properties
# this document is valid
spring.profiles.active=prod
#---
# this document is invalid
spring.config.activate.on-profile=prod
spring.profiles.active=metrics
```

#### 7.3.1.添加活动配置文件

`spring.profiles.active`属性遵循与其他属性相同的排序规则：`PropertySource`最高者获胜。这意味着您可以在`application.properties`中指定活动配置文件，然后使用命令行开关**替换它们。**

有时，将属性**添加**到活动配置文件而不是替换它们很有用。`spring.profiles.include`属性可用于在由`spring.profiles.active`属性激活的配置文件之上添加活动配置文件。`SpringApplication`入口还有一个用于设置附加配置文件的 Java API 。请参阅[SpringApplication](https://docs.spring.io/spring-boot/docs/2.7.3/api/org/springframework/boot/SpringApplication.html)中的`setAdditionalProfiles()`方法。

例如，当运行具有以下属性的应用程序时，即使使用 `--spring.profiles.active` 开关运行，也会激活公共和本地配置文件：

```properties
spring.profiles.include[0]=common
spring.profiles.include[1]=local
```

与`spring.profiles.active`类似，`spring.profiles.include`只能在非配置文件特定文档中使用。这意味着它不能通过`spring.config.activate.on-profile`包含在[配置文件](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.files.activation-properties)或[特定配置文件](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.files.profile-specific)中. 如果给定的配置文件处于活动状态，则在[下一节](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.profiles.groups)中描述的配置文件组也可用于添加活动配置文件。

#### 7.3.2. 配置文件组

有时，您在应用程序中定义和使用的配置文件过于细化，使用起来很麻烦。例如，您可能拥有用于独立启用数据库和消息传递功能的`proddb`和`prodmq`配置文件。

为了帮助解决这个问题，Spring Boot 允许您定义配置文件组。配置文件组允许您为相关的配置文件组定义逻辑名称。

例如，我们可以创建一个由我们的`proddb`和`prodmq`配置文件组成的`production`组。

```properties
spring.profiles.group.production[0]=proddb
spring.profiles.group.production[1]=prodmq
```

我们的应用程序现在可以启动，使用`--spring.profiles.active=production`参数激活`production`,`proddb`和`prodmq`配置文件。

#### 7.3.3. 以编程方式设置配置文件

您可以通过在应用程序运行之前调用`SpringApplication.setAdditionalProfiles(…)`来以编程方式设置活动配置文件。也可以使用 Spring 的`ConfigurableEnvironment`接口激活配置文件。

#### 7.3.4. 配置文件特定的配置文件

`application.properties`（或`application.yml`）和通过引用的文件的特定配置文件变体被视为文件并通过`@ConfigurationProperties`加载。有关详细信息，请参阅“[配置文件特定文件](https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.files.profile-specific)”。

###


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.shiker.tech/spring-boot-can-kao-wen-dang/7.-he-xin-te-xing/7.3.profile-pei-zhi.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
