2 changed files with 27 additions and 28 deletions
@ -1,33 +1,32 @@ |
|||||||
[[testcontext-ctx-management-mixed-config]] |
[[testcontext-ctx-management-mixed-config]] |
||||||
= Mixing XML, Groovy Scripts, and Component Classes |
= Mixing Component Classes, XML, and Groovy Scripts |
||||||
|
|
||||||
It may sometimes be desirable to mix XML configuration files, Groovy scripts, and |
It may sometimes be desirable to mix component classes (typically `@Configuration` |
||||||
component classes (typically `@Configuration` classes) to configure an |
classes), XML configuration files, or Groovy scripts to configure an `ApplicationContext` |
||||||
`ApplicationContext` for your tests. For example, if you use XML configuration in |
for your tests. For example, if you use XML configuration in production for legacy |
||||||
production, you may decide that you want to use `@Configuration` classes to configure |
reasons, you may decide that you want to use `@Configuration` classes to configure |
||||||
specific Spring-managed components for your tests, or vice versa. |
specific Spring-managed components for your tests, or vice versa. |
||||||
|
|
||||||
Furthermore, some third-party frameworks (such as Spring Boot) provide first-class |
Furthermore, some third-party frameworks (such as Spring Boot) provide first-class |
||||||
support for loading an `ApplicationContext` from different types of resources |
support for loading an `ApplicationContext` from different types of resources |
||||||
simultaneously (for example, XML configuration files, Groovy scripts, and |
simultaneously (for example, `@Configuration` classes, XML configuration files, and |
||||||
`@Configuration` classes). The Spring Framework, historically, has not supported this for |
Groovy scripts). The Spring Framework, historically, has not supported this for standard |
||||||
standard deployments. Consequently, most of the `SmartContextLoader` implementations that |
deployments. Consequently, most of the `SmartContextLoader` implementations that the |
||||||
the Spring Framework delivers in the `spring-test` module support only one resource type |
Spring Framework delivers in the `spring-test` module support only one resource type for |
||||||
for each test context. However, this does not mean that you cannot use both. One |
each test context. However, this does not mean that you cannot use a mixture of resource |
||||||
exception to the general rule is that the `GenericGroovyXmlContextLoader` and |
types. One exception to the general rule is that the `GenericGroovyXmlContextLoader` and |
||||||
`GenericGroovyXmlWebContextLoader` support both XML configuration files and Groovy |
`GenericGroovyXmlWebContextLoader` support both XML configuration files and Groovy |
||||||
scripts simultaneously. Furthermore, third-party frameworks may choose to support the |
scripts simultaneously. Furthermore, third-party frameworks may choose to support the |
||||||
declaration of both `locations` and `classes` through `@ContextConfiguration`, and, with |
declaration of both `classes` and `locations` through `@ContextConfiguration`, and, with |
||||||
the standard testing support in the TestContext framework, you have the following options. |
the standard testing support in the TestContext framework, you have the following options. |
||||||
|
|
||||||
If you want to use resource locations (for example, XML or Groovy) and `@Configuration` |
If you want to use `@Configuration` classes and resource locations (for example, XML or |
||||||
classes to configure your tests, you must pick one as the entry point, and that one must |
Groovy) 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 |
import or include the other. For example, in a `@Configuration` class, you can use |
||||||
`@Configuration` classes by using component scanning or defining them as normal Spring |
`@ImportResource` to import XML configuration files or Groovy scripts; whereas, in XML or |
||||||
beans, whereas, in a `@Configuration` class, you can use `@ImportResource` to import XML |
Groovy scripts, you can include `@Configuration` classes by using component scanning or |
||||||
configuration files or Groovy scripts. Note that this behavior is semantically equivalent |
defining them as normal Spring beans. Note that this behavior is semantically equivalent |
||||||
to how you configure your application in production: In production configuration, you |
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` |
define either a set of `@Configuration` classes or a set of XML or Groovy resource |
||||||
classes from which your production `ApplicationContext` is loaded, but you still have the |
locations from which your production `ApplicationContext` is loaded, but you still have |
||||||
freedom to include or import the other type of configuration. |
the freedom to import or include the other type of configuration. |
||||||
|
|
||||||
|
|||||||
Loading…
Reference in new issue