|
对于最新稳定版本,请使用 Spring Framework 7.0.6! |
@ActiveProfiles
@ActiveProfiles 是一个类级别的注解,用于声明在为集成测试加载 ApplicationContext 时应激活哪些 Bean 定义配置文件。
以下示例表示应激活 dev 配置文件:
-
Java
-
Kotlin
@ContextConfiguration
@ActiveProfiles("dev") (1)
class DeveloperTests {
// class body...
}
| 1 | 指示应激活 dev 配置文件。 |
@ContextConfiguration
@ActiveProfiles("dev") (1)
class DeveloperTests {
// class body...
}
| 1 | 指示应激活 dev 配置文件。 |
以下示例表明 dev 和 integration 这两个配置文件都应处于激活状态:
-
Java
-
Kotlin
@ContextConfiguration
@ActiveProfiles({"dev", "integration"}) (1)
class DeveloperIntegrationTests {
// class body...
}
| 1 | 指示应激活 dev 和 integration 配置文件。 |
@ContextConfiguration
@ActiveProfiles(["dev", "integration"]) (1)
class DeveloperIntegrationTests {
// class body...
}
| 1 | 指示应激活 dev 和 integration 配置文件。 |
@ActiveProfiles 默认支持继承由超类和外部类声明的活动 Bean 定义配置文件。您还可以通过实现自定义的
ActiveProfilesResolver
并通过 @ActiveProfiles 的 resolver 属性进行注册,以编程方式解析活动 Bean 定义配置文件。 |
请参阅 使用环境配置文件的上下文配置、
@Nested 测试类配置,以及
@ActiveProfiles javadoc 以获取示例和更多详细信息。