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

org.springframework.test.context.web

## Class AbstractGenericWebContextLoader

- java.lang.Object
- - org.springframework.test.context.support.AbstractContextLoader
  - - org.springframework.test.context.web.AbstractGenericWebContextLoader

- All Implemented Interfaces:
    ContextLoader, SmartContextLoader
  Direct Known Subclasses:
    AnnotationConfigWebContextLoader, GenericXmlWebContextLoader

      public abstract class AbstractGenericWebContextLoader
      extends AbstractContextLoader

  Abstract, generic extension of `AbstractContextLoader` that loads a `GenericWebApplicationContext`.

  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. Note that `AbstractGenericWebContextLoader` does not support the `loadContext(String... locations)` method from the legacy `ContextLoader` SPI.

  Concrete subclasses must provide an appropriate implementation of `loadBeanDefinitions(org.springframework.web.context.support.GenericWebApplicationContext, org.springframework.test.context.web.WebMergedContextConfiguration)`.

  Since:
    3.2
  Author:
    Sam Brannen
  See Also:
    `loadContext(MergedContextConfiguration)`, `loadContext(String...)`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `AbstractGenericWebContextLoader()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected void` | `configureWebResources(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig)` Configures web resources for the supplied web application context.
    `protected void` | `customizeBeanFactory(DefaultListableBeanFactory beanFactory, WebMergedContextConfiguration webMergedConfig)` Customize the internal bean factory of the `WebApplicationContext` created by this context loader.
    `protected void` | `customizeContext(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig)` Customize the `GenericWebApplicationContext` created by this context loader after bean definitions have been loaded into the context but before the context is refreshed.
    `protected abstract void` | `loadBeanDefinitions(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig)` Load bean definitions into the supplied `context` from the locations or classes in the supplied `WebMergedContextConfiguration`.
    `ConfigurableApplicationContext` | `loadContext(MergedContextConfiguration mergedConfig)` Load a Spring `WebApplicationContext` from the supplied `MergedContextConfiguration`.
    `ApplicationContext` | `loadContext(String... locations)` `AbstractGenericWebContextLoader` should be used as a `SmartContextLoader`, not as a legacy `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`

- - ### Constructor Detail

    - #### AbstractGenericWebContextLoader

          public AbstractGenericWebContextLoader()

  - ### Method Detail

    - #### loadContext

          public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig)
                                                           throws Exception

      Load a Spring `WebApplicationContext` from the supplied `MergedContextConfiguration`.

      Implementation details:

      - Creates a `GenericWebApplicationContext` instance.
      - Delegates to `configureWebResources(org.springframework.web.context.support.GenericWebApplicationContext, org.springframework.test.context.web.WebMergedContextConfiguration)` to create the `MockServletContext` and set it in the `WebApplicationContext`.
      - Calls `AbstractContextLoader.prepareContext(org.springframework.context.ConfigurableApplicationContext, org.springframework.test.context.MergedContextConfiguration)` to allow for customizing the context before bean definitions are loaded.
      - Calls `customizeBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory, org.springframework.test.context.web.WebMergedContextConfiguration)` to allow for customizing the context's `DefaultListableBeanFactory`.
      - Delegates to `loadBeanDefinitions(org.springframework.web.context.support.GenericWebApplicationContext, org.springframework.test.context.web.WebMergedContextConfiguration)` to populate the context from the locations or classes in the supplied `MergedContextConfiguration`.
      - Delegates to `AnnotationConfigUtils` for registering annotation configuration processors.
      - Calls `customizeContext(org.springframework.web.context.support.GenericWebApplicationContext, org.springframework.test.context.web.WebMergedContextConfiguration)` 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 web application context
      Throws:
        `Exception` - if context loading failed
      See Also:
        `SmartContextLoader.loadContext(MergedContextConfiguration)`, `GenericWebApplicationContext`

    - #### configureWebResources

          protected void configureWebResources(GenericWebApplicationContext context,
                                   WebMergedContextConfiguration webMergedConfig)

      Configures web resources for the supplied web application context.

      Implementation details:

      - The resource base path is retrieved from the supplied `WebMergedContextConfiguration`.
      - A `ResourceLoader` is instantiated for the `MockServletContext`: if the resource base path is prefixed with "`classpath:`", a `DefaultResourceLoader` will be used; otherwise, a `FileSystemResourceLoader` will be used.
      - A `MockServletContext` will be created using the resource base path and resource loader.
      - The supplied `GenericWebApplicationContext` is then stored in the `MockServletContext` under the `WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE` key.
      - Finally, the `MockServletContext` is set in the `WebApplicationContext`.
      Parameters:
        `context` - the web application context for which to configure the web resources
        `webMergedConfig` - the merged context configuration to use to load the web application context

    - #### customizeBeanFactory

          protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory,
                                  WebMergedContextConfiguration webMergedConfig)

      Customize the internal bean factory of the `WebApplicationContext` created by this context loader.

      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 context loader
        `webMergedConfig` - the merged context configuration to use to load the web application context
      See Also:
        `loadContext(MergedContextConfiguration)`, `DefaultListableBeanFactory.setAllowBeanDefinitionOverriding(boolean)`, `DefaultListableBeanFactory.setAllowEagerClassLoading(boolean)`, `AbstractAutowireCapableBeanFactory.setAllowCircularReferences(boolean)`, `AbstractAutowireCapableBeanFactory.setAllowRawInjectionDespiteWrapping(boolean)`

    - #### loadBeanDefinitions

          protected abstract void loadBeanDefinitions(GenericWebApplicationContext context,
                                 WebMergedContextConfiguration webMergedConfig)

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

      Concrete subclasses must provide an appropriate implementation.

      Parameters:
        `context` - the context into which the bean definitions should be loaded
        `webMergedConfig` - the merged context configuration to use to load the web application context
      See Also:
        `loadContext(MergedContextConfiguration)`

    - #### customizeContext

          protected void customizeContext(GenericWebApplicationContext context,
                              WebMergedContextConfiguration webMergedConfig)

      Customize the `GenericWebApplicationContext` created by this context loader 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 web application context.

      Parameters:
        `context` - the newly created web application context
        `webMergedConfig` - the merged context configuration to use to load the web application context
      See Also:
        `loadContext(MergedContextConfiguration)`

    - #### loadContext

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

      `AbstractGenericWebContextLoader` should be used as a `SmartContextLoader`, not as a legacy `ContextLoader`. Consequently, this method is not supported.
      Parameters:
        `locations` - the resource locations to use to load the application context
      Returns:
        a new application context
      Throws:
        `UnsupportedOperationException`
        `Exception` - if context loading failed
      See Also:
        `ContextLoader.loadContext(java.lang.String[])`