public abstract class AbstractGenericWebContextLoader extends AbstractContextLoader
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)
.
loadContext(MergedContextConfiguration)
,
loadContext(String...)
Constructor and Description |
---|
AbstractGenericWebContextLoader() |
Modifier and Type | Method and Description |
---|---|
protected void |
configureWebResources(GenericWebApplicationContext context,
WebMergedContextConfiguration webMergedConfig)
Configures web resources for the supplied web application context (WAC).
|
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 . |
protected void |
validateMergedContextConfiguration(WebMergedContextConfiguration mergedConfig)
Validate the supplied
WebMergedContextConfiguration with respect to
what this context loader supports. |
generateDefaultLocations, getResourceSuffix, getResourceSuffixes, isGenerateDefaultLocations, modifyLocations, prepareContext, processContextConfiguration, processLocations
protected static final Log logger
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception
WebApplicationContext
from the supplied
MergedContextConfiguration
.
Implementation details:
validateMergedContextConfiguration(WebMergedContextConfiguration)
to allow subclasses to validate the supplied configuration before proceeding.GenericWebApplicationContext
instance.MergedContextConfiguration
references a
parent configuration,
the corresponding ApplicationContext
will be retrieved and
set as the parent
for the context created by this method.configureWebResources(org.springframework.web.context.support.GenericWebApplicationContext, org.springframework.test.context.web.WebMergedContextConfiguration)
to create the
MockServletContext
and set it in the WebApplicationContext
.AbstractContextLoader.prepareContext(org.springframework.context.ConfigurableApplicationContext, org.springframework.test.context.MergedContextConfiguration)
to allow for customizing the context
before bean definitions are loaded.customizeBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory, org.springframework.test.context.web.WebMergedContextConfiguration)
to allow for customizing the
context's DefaultListableBeanFactory
.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
.AnnotationConfigUtils
for
registering
annotation configuration processors.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.mergedConfig
- the merged context configuration to use to load the
application contextException
- if context loading failedSmartContextLoader.loadContext(MergedContextConfiguration)
,
GenericWebApplicationContext
protected void validateMergedContextConfiguration(WebMergedContextConfiguration mergedConfig)
WebMergedContextConfiguration
with respect to
what this context loader supports.
The default implementation is a no-op but can be overridden by subclasses as appropriate.
mergedConfig
- the merged configuration to validateIllegalStateException
- if the supplied configuration is not valid
for this context loaderprotected void configureWebResources(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig)
If the supplied WAC has no parent or its parent is not a WAC, the supplied WAC will be configured as the Root WAC (see "Root WAC Configuration" below).
Otherwise the context hierarchy of the supplied WAC will be traversed
to find the top-most WAC (i.e., the root); and the ServletContext
of the Root WAC will be set as the ServletContext
for the supplied
WAC.
WebMergedContextConfiguration
.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.MockServletContext
will be created using the resource base
path and resource loader.GenericWebApplicationContext
is then stored in
the MockServletContext
under the
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
key.MockServletContext
is set in the
WebApplicationContext
.context
- the web application context for which to configure the web
resourceswebMergedConfig
- the merged context configuration to use to load the
web application contextprotected void customizeBeanFactory(DefaultListableBeanFactory beanFactory, WebMergedContextConfiguration webMergedConfig)
WebApplicationContext
created by this context loader.
The default implementation is empty but can be overridden in subclasses
to customize DefaultListableBeanFactory
's standard settings.
beanFactory
- the bean factory created by this context loaderwebMergedConfig
- the merged context configuration to use to load the
web application contextloadContext(MergedContextConfiguration)
,
DefaultListableBeanFactory.setAllowBeanDefinitionOverriding(boolean)
,
DefaultListableBeanFactory.setAllowEagerClassLoading(boolean)
,
AbstractAutowireCapableBeanFactory.setAllowCircularReferences(boolean)
,
AbstractAutowireCapableBeanFactory.setAllowRawInjectionDespiteWrapping(boolean)
protected abstract void loadBeanDefinitions(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig)
context
from the locations or classes in the supplied WebMergedContextConfiguration
.
Concrete subclasses must provide an appropriate implementation.
context
- the context into which the bean definitions should be loadedwebMergedConfig
- the merged context configuration to use to load the
web application contextloadContext(MergedContextConfiguration)
protected void customizeContext(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig)
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.
context
- the newly created web application contextwebMergedConfig
- the merged context configuration to use to load the
web application contextloadContext(MergedContextConfiguration)
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.locations
- the resource locations to use to load the application contextUnsupportedOperationException
Exception
- if context loading failedContextLoader.loadContext(java.lang.String[])