|
对于最新稳定版本,请使用 Spring Framework 7.0.6! |
上下文配置继承
@ContextConfiguration 支持布尔类型的 inheritLocations 和 inheritInitializers 属性,用于指示是否应继承超类中声明的资源位置、组件类以及上下文初始化器。这两个标志的默认值均为 true。这意味着测试类会继承其所有超类所声明的资源位置或组件类,以及上下文初始化器。具体而言,测试类的资源位置或组件类会被追加到其超类所声明的资源位置或带注解类列表之后。同样地,给定测试类的初始化器也会被添加到其测试超类所定义的初始化器集合中。因此,子类可以选择性地扩展资源位置、组件类或上下文初始化器。
如果 inheritLocations 注解中的 inheritInitializers 或 @ContextConfiguration 属性被设置为 false,则测试类中指定的资源位置(或组件类)和上下文初始化器将覆盖(shadow)并有效替换其父类中定义的配置。
自 Spring Framework 5.3 起,测试配置也可以从外部类继承。有关详细信息,请参阅 @Nested 测试类配置。 |
在下一个使用 XML 资源位置的示例中,ApplicationContext 的 ExtendedTest 将按顺序从 base-config.xml 和 extended-config.xml 加载。
因此,在 extended-config.xml 中定义的 Bean 可以覆盖(即替换)在 base-config.xml 中定义的 Bean。以下示例展示了如何让一个类继承另一个类,并同时使用自身的配置文件和其父类的配置文件:
-
Java
-
Kotlin
@ExtendWith(SpringExtension.class)
// ApplicationContext will be loaded from "/base-config.xml"
// in the root of the classpath
@ContextConfiguration("/base-config.xml") (1)
class BaseTest {
// class body...
}
// ApplicationContext will be loaded from "/base-config.xml" and
// "/extended-config.xml" in the root of the classpath
@ContextConfiguration("/extended-config.xml") (2)
class ExtendedTest extends BaseTest {
// class body...
}
| 1 | 在超类中定义的配置文件。 |
| 2 | 在子类中定义的配置文件。 |
@ExtendWith(SpringExtension::class)
// ApplicationContext will be loaded from "/base-config.xml"
// in the root of the classpath
@ContextConfiguration("/base-config.xml") (1)
open class BaseTest {
// class body...
}
// ApplicationContext will be loaded from "/base-config.xml" and
// "/extended-config.xml" in the root of the classpath
@ContextConfiguration("/extended-config.xml") (2)
class ExtendedTest : BaseTest() {
// class body...
}
| 1 | 在超类中定义的配置文件。 |
| 2 | 在子类中定义的配置文件。 |
同样地,在下一个使用组件类的示例中,ApplicationContext 的 ExtendedTest 将按顺序从 BaseConfig 和 ExtendedConfig 类中加载。因此,在 ExtendedConfig 中定义的 Bean 可以覆盖(即替换)在 BaseConfig 中定义的 Bean。以下示例展示了某个类如何继承另一个类,并同时使用自身的配置类和父类的配置类:
-
Java
-
Kotlin
// ApplicationContext will be loaded from BaseConfig
@SpringJUnitConfig(BaseConfig.class) (1)
class BaseTest {
// class body...
}
// ApplicationContext will be loaded from BaseConfig and ExtendedConfig
@SpringJUnitConfig(ExtendedConfig.class) (2)
class ExtendedTest extends BaseTest {
// class body...
}
| 1 | 在超类中定义的配置类。 |
| 2 | 在子类中定义的配置类。 |
// ApplicationContext will be loaded from BaseConfig
@SpringJUnitConfig(BaseConfig::class) (1)
open class BaseTest {
// class body...
}
// ApplicationContext will be loaded from BaseConfig and ExtendedConfig
@SpringJUnitConfig(ExtendedConfig::class) (2)
class ExtendedTest : BaseTest() {
// class body...
}
| 1 | 在超类中定义的配置类。 |
| 2 | 在子类中定义的配置类。 |
在下一个使用上下文初始化器的示例中,ApplicationContext 的 ExtendedTest 通过 BaseInitializer 和 ExtendedInitializer 进行初始化。但请注意,这些初始化器的调用顺序取决于它们是否实现了 Spring 的 Ordered 接口,或者是否使用了 Spring 的 @Order 注解或标准的 @Priority 注解。以下示例展示了某个类如何继承另一个类,并同时使用自身的初始化器和父类的初始化器:
-
Java
-
Kotlin
// ApplicationContext will be initialized by BaseInitializer
@SpringJUnitConfig(initializers = BaseInitializer.class) (1)
class BaseTest {
// class body...
}
// ApplicationContext will be initialized by BaseInitializer
// and ExtendedInitializer
@SpringJUnitConfig(initializers = ExtendedInitializer.class) (2)
class ExtendedTest extends BaseTest {
// class body...
}
| 1 | 在超类中定义的初始化器。 |
| 2 | 在子类中定义的初始化器。 |
// ApplicationContext will be initialized by BaseInitializer
@SpringJUnitConfig(initializers = [BaseInitializer::class]) (1)
open class BaseTest {
// class body...
}
// ApplicationContext will be initialized by BaseInitializer
// and ExtendedInitializer
@SpringJUnitConfig(initializers = [ExtendedInitializer::class]) (2)
class ExtendedTest : BaseTest() {
// class body...
}
| 1 | 在超类中定义的初始化器。 |
| 2 | 在子类中定义的初始化器。 |