ContextConfiguration (Spring Framework 3.2.1.RELEASE API)
Date: 2013-01-24

org.springframework.test.context

## Annotation Type ContextConfiguration

-     @Documented
      @Inherited
      @Retention(value=RUNTIME)
      @Target(value=TYPE)
      public @interface ContextConfiguration

  `@ContextConfiguration` defines class-level metadata that is used to determine how to load and configure an `ApplicationContext` for integration tests.

  ### Supported Resource Types

  Prior to Spring 3.1, only path-based resource locations were supported. As of Spring 3.1, context loaders may choose to support either path-based or class-based resources (but not both). Consequently `@ContextConfiguration` can be used to declare either path-based resource locations (via the `locations()` or `value()` attribute) or annotated classes (via the `classes()` attribute).

  ### Annotated Classes

  The term annotated class can refer to any of the following.

  - A class annotated with `@Configuration`
  - A component (i.e., a class annotated with `@Component`, `@Service`, `@Repository`, etc.)
  - A JSR-330 compliant class that is annotated with `javax.inject` annotations
  - Any other class that contains `@Bean`-methods
  Consult the Javadoc for `@Configuration` and `@Bean` for further information regarding the configuration and semantics of annotated classes.
  Since:
    2.5
  Author:
    Sam Brannen
  See Also:
    `ContextLoader`, `SmartContextLoader`, `ContextConfigurationAttributes`, `MergedContextConfiguration`, `ActiveProfiles`, `ApplicationContext`

- - ### Optional Element Summary

    Optional Elements
    Modifier and Type | Optional Element and Description
    --- | ---
    `Class<?>[]` | `classes` The annotated classes to use for loading an `ApplicationContext`.
    `boolean` | `inheritInitializers` Whether or not context initializers from test superclasses should be inherited.
    `boolean` | `inheritLocations` Whether or not `resource locations` or annotated classes from test superclasses should be inherited.
    `Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[]` | `initializers` The application context initializer classes to use for initializing a `ConfigurableApplicationContext`.
    `Class<? extends ContextLoader>` | `loader` The type of `SmartContextLoader` (or `ContextLoader`) to use for loading an `ApplicationContext`.
    `String[]` | `locations` The resource locations to use for loading an `ApplicationContext`.
    `String[]` | `value` Alias for `locations()`.

- - ### Element Detail

    - #### value

          public abstract String[] value

      Alias for `locations()`.

      This attribute may not be used in conjunction with `locations()` or `classes()`, but it may be used instead of `locations()`.

      Since:
        3.0
      See Also:
        `inheritLocations()`
      Default:
        {}

    - #### locations

          public abstract String[] locations

      The resource locations to use for loading an `ApplicationContext`.

      Check out the Javadoc for `AbstractContextLoader.modifyLocations()` for details on how a location String will be interpreted at runtime, in particular in case of a relative path. Also, check out the documentation on `AbstractContextLoader.generateDefaultLocations()` for details on the default locations that are going to be used if none are specified.

      Note that the above-mentioned default rules only apply for a standard `AbstractContextLoader` subclass such as `GenericXmlContextLoader` which is the effective default implementation used at runtime if `locations` are configured. See the documentation for `loader()` for further details regarding default loaders.

      This attribute may not be used in conjunction with `value()` or `classes()`, but it may be used instead of `value()`.

      Since:
        2.5
      See Also:
        `inheritLocations()`
      Default:
        {}

    - #### classes

          public abstract Class<?>[] classes

      The annotated classes to use for loading an `ApplicationContext`.

      Check out the Javadoc for `AnnotationConfigContextLoader.detectDefaultConfigurationClasses()` for details on how default configuration classes will be detected if no annotated classes are specified. See the documentation for `loader()` for further details regarding default loaders.

      This attribute may not be used in conjunction with `locations()` or `value()`.

      Since:
        3.1
      See Also:
        `Configuration`, `AnnotationConfigContextLoader`, `inheritLocations()`
      Default:
        {}

    - #### initializers

          public abstract Class<? extends ApplicationContextInitializer<? extends ConfigurableApplicationContext>>[] initializers

      The application context initializer classes to use for initializing a `ConfigurableApplicationContext`.

      The concrete `ConfigurableApplicationContext` type supported by each declared initializer must be compatible with the type of `ApplicationContext` created by the `SmartContextLoader` in use.

      `SmartContextLoader` implementations typically detect whether Spring's `Ordered` interface has been implemented or if the @`Order` annotation is present and sort instances accordingly prior to invoking them.

      Since:
        3.2
      See Also:
        `ApplicationContextInitializer`, `ConfigurableApplicationContext`, `inheritInitializers()`, `loader()`
      Default:
        {}

    - #### inheritLocations

          public abstract boolean inheritLocations

      Whether or not `resource locations` or annotated classes from test superclasses should be inherited.

      The default value is `true`. This means that an annotated class will inherit the resource locations or annotated classes defined by test superclasses. Specifically, the resource locations or annotated classes for a given test class will be appended to the list of resource locations or annotated classes defined by test superclasses. Thus, subclasses have the option of extending the list of resource locations or annotated classes.

      If `inheritLocations` is set to `false`, the resource locations or annotated classes for the annotated class will shadow and effectively replace any resource locations or annotated classes defined by superclasses.

      In the following example that uses path-based resource locations, the `ApplicationContext` for `ExtendedTest` will be loaded from "base-context.xml" and "extended-context.xml", in that order. Beans defined in "extended-context.xml" may therefore override those defined in "base-context.xml".

           @ContextConfiguration("base-context.xml")
           public class BaseTest {
               // ...
           }

           @ContextConfiguration("extended-context.xml")
           public class ExtendedTest extends BaseTest {
               // ...
           }

      Similarly, in the following example that uses annotated classes, the `ApplicationContext` for `ExtendedTest` will be loaded from the `BaseConfig` and `ExtendedConfig` configuration classes, in that order. Beans defined in `ExtendedConfig` may therefore override those defined in `BaseConfig`.

           @ContextConfiguration(classes=BaseConfig.class)
           public class BaseTest {
               // ...
           }

           @ContextConfiguration(classes=ExtendedConfig.class)
           public class ExtendedTest extends BaseTest {
               // ...
           }

      Since:
        2.5
      Default:
        true

    - #### inheritInitializers

          public abstract boolean inheritInitializers

      Whether or not context initializers from test superclasses should be inherited.

      The default value is `true`. This means that an annotated class will inherit the application context initializers defined by test superclasses. Specifically, the initializers for a given test class will be added to the set of initializers defined by test superclasses. Thus, subclasses have the option of extending the set of initializers.

      If `inheritInitializers` is set to `false`, the initializers for the annotated class will shadow and effectively replace any initializers defined by superclasses.

      In the following example, the `ApplicationContext` for `ExtendedTest` will be initialized using `BaseInitializer` and `ExtendedInitializer`. Note, however, that the order in which the initializers are invoked depends on whether they implement `Ordered` or are annotated with `@Order`.

           @ContextConfiguration(initializers = BaseInitializer.class)
           public class BaseTest {
               // ...
           }

           @ContextConfiguration(initializers = ExtendedInitializer.class)
           public class ExtendedTest extends BaseTest {
               // ...
           }

      Since:
        3.2
      Default:
        true

    - #### loader

          public abstract Class<? extends ContextLoader> loader

      The type of `SmartContextLoader` (or `ContextLoader`) to use for loading an `ApplicationContext`.

      If not specified, the loader will be inherited from the first superclass that is annotated with `@ContextConfiguration` and specifies an explicit loader. If no class in the hierarchy specifies an explicit loader, a default loader will be used instead.

      The default concrete implementation chosen at runtime will be `DelegatingSmartContextLoader`. For further details on the default behavior of various concrete `ContextLoaders`, check out the Javadoc for `AbstractContextLoader`, `GenericXmlContextLoader`, and `AnnotationConfigContextLoader`.

      Since:
        2.5
      Default:
        org.springframework.test.context.ContextLoader.class