|
对于最新的稳定版本,请使用 Spring Framework 7.0.6! |
上下文配置继承
@ContextConfiguration 支持布尔值属性 inheritLocations 和 inheritInitializers,用于声明是否应继承超类声明的资源位置或组件类及上下文初始化器。这两个标志的默认值均为 true。这意味着测试类会继承所有超类声明的资源位置、组件类以及上下文初始化器。
具体来说,测试类的资源位置或组件类会被追加到超类声明的资源位置列表或注解类列表中。同样地,给定测试类的初始化器会被添加到测试超类定义的初始化器集合中。因此,子类可以选择扩展资源位置、组件类或上下文初始化器。
如果inheritLocations或inheritInitializers属性在@ContextConfiguration中被设置为false,则测试类的资源位置或组件类以及上下文初始化器,将分别覆盖并有效替换父类定义的配置。
从 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 | 配置文件定义于子类中。 |
同样地,在下一个使用组件类的示例中,ExtendedTest的ApplicationContext将按顺序从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 | 子类中定义的初始化器。 |