Description
Rossen Stoyanchev opened SPR-9011 and commented
Status Quo
Starting with Spring 3.1 applications can specify contextInitializerClasses
via context-param
and init-param
in web.xml
.
Goals
For comprehensive testing it should be possible to re-use ApplicationContextInitializer
instances in tests as well.
This could be done at the @ContextConfiguration
level by allowing an array of ACI types to be specified, and the TCF would allow each to visit the ApplicationContext
at the right time.
Deliverables
[x] Introduce a new initializers
attribute in @ContextConfiguration
.
```
Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[] initializers() default {};
1. [x] Introduce a new `inheritInitializers` attribute in `@ContextConfiguration`.
- ```
boolean inheritInitializers() default true;
-
Allow a context to be loaded solely via a custom
ApplicationContextInitializer
(i.e., without locations or classes) -
Initializers must be included in
MergedContextConfiguration
for determining the context cache key. -
Invoke initializers within existing
SmartContextLoader
implementations.-
for example, in
AbstractGenericContextLoader.loadContext(...)
methods
[x] per the contract defined in the Javadoc for
ApplicationContextInitializer
:ApplicationContextInitializer
processors are encouraged to detect whether Spring'sOrdered
interface has been implemented or if the@Order
annotation is present and to sort instances accordingly if so prior to invocation.```
-
Collections.sort(initializerInstances, new AnnotationAwareOrderComparator());
1. [x] Document in Javadoc
1. [x] Document in the reference manual
----
#### Pseudocode Examples
```java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
locations = "/app-config.xml",
initializers = CustomInitializer.class)
public class ApplicationContextInitializerTests {}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
locations = "/app-config.xml",
initializers = {PropertySourceInitializer.class, ProfileInitializer.class})
public class ApplicationContextInitializerTests {}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
classes = BaseConfig.class,
initializers = BaseInitializer.class)
public class BaseTest {}
@ContextConfiguration(
classes = ExtendedConfig.class,
initializers = ExtendedInitializer.class)
public class ExtendedTest extends BaseTest {}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(
classes = BaseConfig.class,
initializers = BaseInitializer.class)
public class BaseTest {}
@ContextConfiguration(
classes = ExtendedConfig.class,
initializers = ExtendedInitializer.class,
inheritInitializers = false)
public class ExtendedTest extends BaseTest {}
// In the following example, an exception would not be thrown even
// if no default XML file or @Configuration class is detected.
// In other words, the initializer would be responsible for
// providing XML configuration files or annotated configuration
// classes to the provided context.
@ContextConfiguration(initializers = EntireAppInitializer.class)
public class InitializerWithoutConfigFilesOrClassesTest extends BaseTest {}
Affects: 3.1 GA
Referenced from: commits 1f93777, 58daeea
11 votes, 15 watchers