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

org.springframework.test.context.support

## Class AbstractGenericContextLoader

- java.lang.Object
- - org.springframework.test.context.support.AbstractContextLoader
  - - org.springframework.test.context.support.AbstractGenericContextLoader

- All Implemented Interfaces:
    ContextLoader, SmartContextLoader
  Direct Known Subclasses:
    AnnotationConfigContextLoader, GenericPropertiesContextLoader, GenericXmlContextLoader

      public abstract class AbstractGenericContextLoader
      extends AbstractContextLoader

  Abstract, generic extension of `AbstractContextLoader` that loads a `GenericApplicationContext`.
  - If instances of concrete subclasses are invoked via the `ContextLoader` SPI, the context will be loaded from the locations provided to `loadContext(String...)`.
  - If instances of concrete subclasses are invoked via the `SmartContextLoader` SPI, the context will be loaded from the `MergedContextConfiguration` provided to `loadContext(MergedContextConfiguration)`. In such cases, a `SmartContextLoader` will decide whether to load the context from locations or annotated classes.

  Concrete subclasses must provide an appropriate implementation of `createBeanDefinitionReader()`, potentially overriding `loadBeanDefinitions()` as well.

  Since:
    2.5
  Author:
    Sam Brannen, Juergen Hoeller
  See Also:
    `loadContext(MergedContextConfiguration)`, `loadContext(String...)`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected static Log` | `logger`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AbstractGenericContextLoader()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected abstract BeanDefinitionReader` | `createBeanDefinitionReader(GenericApplicationContext context)` Factory method for creating a new `BeanDefinitionReader` for loading bean definitions into the supplied `context`.
    `protected void` | `customizeBeanFactory(DefaultListableBeanFactory beanFactory)` Customize the internal bean factory of the ApplicationContext created by this `ContextLoader`.
    `protected void` | `customizeContext(GenericApplicationContext context)` Customize the `GenericApplicationContext` created by this `ContextLoader` after bean definitions have been loaded into the context but before the context is refreshed.
    `protected void` | `loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig)` Load bean definitions into the supplied `context` from the locations or classes in the supplied `MergedContextConfiguration`.
    `ConfigurableApplicationContext` | `loadContext(MergedContextConfiguration mergedConfig)` Load a Spring ApplicationContext from the supplied `MergedContextConfiguration`.
    `ConfigurableApplicationContext` | `loadContext(String... locations)` Load a Spring ApplicationContext from the supplied `locations`.
    `protected void` | `prepareContext(GenericApplicationContext context)` Prepare the `GenericApplicationContext` created by this `ContextLoader`.

    - ### Methods inherited from class org.springframework.test.context.support.AbstractContextLoader

      `generateDefaultLocations, getResourceSuffix, isGenerateDefaultLocations, modifyLocations, prepareContext, processContextConfiguration, processLocations`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### logger

          protected static final Log logger

  - ### Constructor Detail

    - #### AbstractGenericContextLoader

          public AbstractGenericContextLoader()

  - ### Method Detail

    - #### loadContext

          public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig)
                                                           throws Exception

      Load a Spring ApplicationContext from the supplied `MergedContextConfiguration`.

      Implementation details:

      - Creates a `GenericApplicationContext` instance.
      - Calls `prepareContext(GenericApplicationContext)` for backwards compatibility with the `ContextLoader` SPI.
      - Calls `AbstractContextLoader.prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)` to allow for customizing the context before bean definitions are loaded.
      - Calls `customizeBeanFactory(DefaultListableBeanFactory)` to allow for customizing the context's `DefaultListableBeanFactory`.
      - Delegates to `loadBeanDefinitions(GenericApplicationContext, MergedContextConfiguration)` to populate the context from the locations or classes in the supplied `MergedContextConfiguration`.
      - Delegates to `AnnotationConfigUtils` for `registering` annotation configuration processors.
      - Calls `customizeContext(GenericApplicationContext)` to allow for customizing the context before it is refreshed.
      - `Refreshes` the context and registers a JVM shutdown hook for it.
      Parameters:
        `mergedConfig` - the merged context configuration to use to load the application context
      Returns:
        a new application context
      Throws:
        `Exception` - if context loading failed
      Since:
        3.1
      See Also:
        `SmartContextLoader.loadContext(MergedContextConfiguration)`, `GenericApplicationContext`

    - #### loadContext

          public final ConfigurableApplicationContext loadContext(String... locations)
                                                           throws Exception

      Load a Spring ApplicationContext from the supplied `locations`.

      Implementation details:

      - Creates a `GenericApplicationContext` instance.
      - Calls `prepareContext(GenericApplicationContext)` to allow for customizing the context before bean definitions are loaded.
      - Calls `customizeBeanFactory(DefaultListableBeanFactory)` to allow for customizing the context's `DefaultListableBeanFactory`.
      - Delegates to `createBeanDefinitionReader(GenericApplicationContext)` to create a `BeanDefinitionReader` which is then used to populate the context from the specified locations.
      - Delegates to `AnnotationConfigUtils` for `registering` annotation configuration processors.
      - Calls `customizeContext(GenericApplicationContext)` to allow for customizing the context before it is refreshed.
      - `Refreshes` the context and registers a JVM shutdown hook for it.

      Note: this method does not provide a means to set active bean definition profiles for the loaded context. See `loadContext(MergedContextConfiguration)` and `AbstractContextLoader.prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)` for an alternative.

      Parameters:
        `locations` - the resource locations to use to load the application context
      Returns:
        a new application context
      Throws:
        `Exception` - if context loading failed
      Since:
        2.5
      See Also:
        `ContextLoader.loadContext(java.lang.String...)`, `GenericApplicationContext`, `loadContext(MergedContextConfiguration)`

    - #### prepareContext

          protected void prepareContext(GenericApplicationContext context)

      Prepare the `GenericApplicationContext` created by this `ContextLoader`. Called before bean definitions are read.

      The default implementation is empty. Can be overridden in subclasses to customize `GenericApplicationContext`'s standard settings.

      Parameters:
        `context` - the context that should be prepared
      Since:
        2.5
      See Also:
        `loadContext(MergedContextConfiguration)`, `loadContext(String...)`, `GenericApplicationContext.setAllowBeanDefinitionOverriding(boolean)`, `GenericApplicationContext.setResourceLoader(org.springframework.core.io.ResourceLoader)`, `GenericApplicationContext.setId(java.lang.String)`

    - #### customizeBeanFactory

          protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory)

      Customize the internal bean factory of the ApplicationContext created by this `ContextLoader`.

      The default implementation is empty but can be overridden in subclasses to customize `DefaultListableBeanFactory`'s standard settings.

      Parameters:
        `beanFactory` - the bean factory created by this `ContextLoader`
      Since:
        2.5
      See Also:
        `loadContext(MergedContextConfiguration)`, `loadContext(String...)`, `DefaultListableBeanFactory.setAllowBeanDefinitionOverriding(boolean)`, `DefaultListableBeanFactory.setAllowEagerClassLoading(boolean)`, `AbstractAutowireCapableBeanFactory.setAllowCircularReferences(boolean)`, `AbstractAutowireCapableBeanFactory.setAllowRawInjectionDespiteWrapping(boolean)`

    - #### loadBeanDefinitions

          protected void loadBeanDefinitions(GenericApplicationContext context,
                                 MergedContextConfiguration mergedConfig)

      Load bean definitions into the supplied `context` from the locations or classes in the supplied `MergedContextConfiguration`.

      The default implementation delegates to the `BeanDefinitionReader` returned by `createBeanDefinitionReader(GenericApplicationContext)` to `load` the bean definitions.

      Subclasses must provide an appropriate implementation of `createBeanDefinitionReader(GenericApplicationContext)`. Alternatively subclasses may provide a no-op implementation of `createBeanDefinitionReader()` and override this method to provide a custom strategy for loading or registering bean definitions.

      Parameters:
        `context` - the context into which the bean definitions should be loaded
        `mergedConfig` - the merged context configuration
      Since:
        3.1
      See Also:
        `loadContext(MergedContextConfiguration)`

    - #### createBeanDefinitionReader

          protected abstract BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context)

      Factory method for creating a new `BeanDefinitionReader` for loading bean definitions into the supplied `context`.
      Parameters:
        `context` - the context for which the `BeanDefinitionReader` should be created
      Returns:
        a `BeanDefinitionReader` for the supplied context
      Since:
        2.5
      See Also:
        `loadContext(String...)`, `loadBeanDefinitions(org.springframework.context.support.GenericApplicationContext, org.springframework.test.context.MergedContextConfiguration)`, `BeanDefinitionReader`

    - #### customizeContext

          protected void customizeContext(GenericApplicationContext context)

      Customize the `GenericApplicationContext` created by this `ContextLoader` after bean definitions have been loaded into the context but before the context is refreshed.

      The default implementation is empty but can be overridden in subclasses to customize the application context.

      Parameters:
        `context` - the newly created application context
      Since:
        2.5
      See Also:
        `loadContext(MergedContextConfiguration)`, `loadContext(String...)`