Mixing XML, Groovy Scripts, and Component Classes
有时候可能需要混合使用 XML 配置文件、Groovy 脚本和组件类(通常为 @Configuration
类)来为您的测试配置 ApplicationContext
。例如,如果您在生产中使用 XML 配置,您可能会决定您想使用 @Configuration
类为您的测试配置特定于 Spring 管理的组件,反之亦然。
It may sometimes be desirable to mix XML configuration files, Groovy scripts, and
component classes (typically @Configuration
classes) to configure an
ApplicationContext
for your tests. For example, if you use XML configuration in
production, you may decide that you want to use @Configuration
classes to configure
specific Spring-managed components for your tests, or vice versa.
此外,一些第三方框架(例如 Spring Boot)为同时从不同类型的资源中加载 ApplicationContext
提供了一流的支持(例如 XML 配置文件、Groovy 脚本和 @Configuration
类)。历史上,Spring Framework 并不支持标准部署的此功能。因此,Spring Framework 在 spring-test
模块中提供的 çoğu SmartContextLoader
实现只支持每个测试上下文的单个资源类型。但是,这并不意味着您不能同时使用两者。一般规则的一个例外是 GenericGroovyXmlContextLoader
和 GenericGroovyXmlWebContextLoader
同时支持 XML 配置文件和 Groovy 脚本。此外,第三方框架可以选择通过 @ContextConfiguration
支持声明 locations
和 classes
,并且通过 TestContext 框架中的标准测试支持,您拥有以下选项。
Furthermore, some third-party frameworks (such as Spring Boot) provide first-class
support for loading an ApplicationContext
from different types of resources
simultaneously (for example, XML configuration files, Groovy scripts, and
@Configuration
classes). The Spring Framework, historically, has not supported this for
standard deployments. Consequently, most of the SmartContextLoader
implementations that
the Spring Framework delivers in the spring-test
module support only one resource type
for each test context. However, this does not mean that you cannot use both. One
exception to the general rule is that the GenericGroovyXmlContextLoader
and
GenericGroovyXmlWebContextLoader
support both XML configuration files and Groovy
scripts simultaneously. Furthermore, third-party frameworks may choose to support the
declaration of both locations
and classes
through @ContextConfiguration
, and, with
the standard testing support in the TestContext framework, you have the following options.
如果您想使用资源位置(例如 XML 或 Groovy)和 @Configuration
类来配置您的测试,您必须选择一个作为入口点,并且该入口点必须包含或导入另一个。例如,在 XML 或 Groovy 脚本中,您可以通过使用组件扫描或将其定义为普通 Spring bean 来包含 @Configuration
类,而在一个 @Configuration
类中,您可以使用 @ImportResource
来导入 XML 配置文件或 Groovy 脚本。请注意,此行为在语义上等同于您在生产中配置应用程序的方式:在生产配置中,您定义一组 XML 或 Groovy 资源位置或一组用于加载生产 ApplicationContext
的 @Configuration
类,但您仍然有自由包含或导入其他类型的配置。
If you want to use resource locations (for example, XML or Groovy) and @Configuration
classes to configure your tests, you must pick one as the entry point, and that one must
include or import the other. For example, in XML or Groovy scripts, you can include
@Configuration
classes by using component scanning or defining them as normal Spring
beans, whereas, in a @Configuration
class, you can use @ImportResource
to import XML
configuration files or Groovy scripts. Note that this behavior is semantically equivalent
to how you configure your application in production: In production configuration, you
define either a set of XML or Groovy resource locations or a set of @Configuration
classes from which your production ApplicationContext
is loaded, but you still have the
freedom to include or import the other type of configuration.