类 AbstractGenericWebContextLoader
- java.lang.Object
- org.springframework.test.context.support.AbstractContextLoader
- org.springframework.test.context.web.AbstractGenericWebContextLoader
- 所有已实现的接口:
ContextLoader
,SmartContextLoader
public abstract class AbstractGenericWebContextLoader extends AbstractContextLoader
Abstract, generic extension ofAbstractContextLoader
that loads aGenericWebApplicationContext
.If instances of concrete subclasses are invoked via the
SmartContextLoader
SPI, the context will be loaded from theMergedContextConfiguration
provided toloadContext(MergedContextConfiguration)
. In such cases, aSmartContextLoader
will decide whether to load the context from locations or annotated classes. Note thatAbstractGenericWebContextLoader
does not support theloadContext(String... locations)
method from the legacyContextLoader
SPI.Concrete subclasses must provide an appropriate implementation of
loadBeanDefinitions(org.springframework.web.context.support.GenericWebApplicationContext, org.springframework.test.context.web.WebMergedContextConfiguration)
.- 从以下版本开始:
- 3.2
- 作者:
- Sam Brannen, Phillip Webb
- 另请参阅:
loadContext(MergedContextConfiguration)
,loadContext(String...)
构造器概要
构造器 构造器 说明 AbstractGenericWebContextLoader()
方法概要
所有方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 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 theWebApplicationContext
created by this context loader.protected void
customizeContext(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig)
Customize theGenericWebApplicationContext
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 suppliedcontext
from the locations or classes in the suppliedWebMergedContextConfiguration
.ApplicationContext
loadContext(String... locations)
AbstractGenericWebContextLoader
should be used as aSmartContextLoader
, not as a legacyContextLoader
.ConfigurableApplicationContext
loadContext(MergedContextConfiguration mergedConfig)
Load a SpringWebApplicationContext
from the suppliedMergedContextConfiguration
.protected void
validateMergedContextConfiguration(WebMergedContextConfiguration mergedConfig)
Validate the suppliedWebMergedContextConfiguration
with respect to what this context loader supports.从类继承的方法 org.springframework.test.context.support.AbstractContextLoader
customizeContext, generateDefaultLocations, getResourceSuffix, getResourceSuffixes, isGenerateDefaultLocations, modifyLocations, prepareContext, processContextConfiguration, processLocations
构造器详细资料
AbstractGenericWebContextLoader
public AbstractGenericWebContextLoader()
方法详细资料
loadContext
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception
Load a SpringWebApplicationContext
from the suppliedMergedContextConfiguration
.Implementation details:
- Calls
validateMergedContextConfiguration(WebMergedContextConfiguration)
to allow subclasses to validate the supplied configuration before proceeding. - Creates a
GenericWebApplicationContext
instance. - If the supplied
MergedContextConfiguration
references a parent configuration, the correspondingApplicationContext
will be retrieved and set as the parent for the context created by this method. - Delegates to
configureWebResources(org.springframework.web.context.support.GenericWebApplicationContext, org.springframework.test.context.web.WebMergedContextConfiguration)
to create theMockServletContext
and set it in theWebApplicationContext
. - 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'sDefaultListableBeanFactory
. - 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 suppliedMergedContextConfiguration
. - 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.
- 参数:
mergedConfig
- the merged context configuration to use to load the application context- 返回:
- a new web application context
- 抛出:
Exception
- if context loading failed- 另请参阅:
SmartContextLoader.loadContext(MergedContextConfiguration)
,GenericWebApplicationContext
- Calls
validateMergedContextConfiguration
protected void validateMergedContextConfiguration(WebMergedContextConfiguration mergedConfig)
Validate the suppliedWebMergedContextConfiguration
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 validate- 抛出:
IllegalStateException
- if the supplied configuration is not valid for this context loader- 从以下版本开始:
- 4.0.4
configureWebResources
protected void configureWebResources(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig)
Configures web resources for the supplied web application context (WAC).Implementation Details
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 theServletContext
for the supplied WAC.Root WAC Configuration
- The resource base path is retrieved from the supplied
WebMergedContextConfiguration
. - A
ResourceLoader
is instantiated for theMockServletContext
: if the resource base path is prefixed with "classpath:
", aDefaultResourceLoader
will be used; otherwise, aFileSystemResourceLoader
will be used. - A
MockServletContext
will be created using the resource base path and resource loader. - The supplied
GenericWebApplicationContext
is then stored in theMockServletContext
under theWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
key. - Finally, the
MockServletContext
is set in theWebApplicationContext
.
- 参数:
context
- the web application context for which to configure the web resourceswebMergedConfig
- the merged context configuration to use to load the web application context
- The resource base path is retrieved from the supplied
customizeBeanFactory
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory, WebMergedContextConfiguration webMergedConfig)
Customize the internal bean factory of theWebApplicationContext
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 context- 另请参阅:
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 suppliedcontext
from the locations or classes in the suppliedWebMergedContextConfiguration
.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 context- 另请参阅:
loadContext(MergedContextConfiguration)
customizeContext
protected void customizeContext(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig)
Customize theGenericWebApplicationContext
created by this context loader after bean definitions have been loaded into the context but before the context is refreshed.The default implementation simply delegates to
AbstractContextLoader.customizeContext(ConfigurableApplicationContext, MergedContextConfiguration)
.- 参数:
context
- the newly created web application contextwebMergedConfig
- the merged context configuration to use to load the web application context- 另请参阅:
loadContext(MergedContextConfiguration)
,AbstractContextLoader.customizeContext(ConfigurableApplicationContext, MergedContextConfiguration)
loadContext
public final ApplicationContext loadContext(String... locations) throws Exception
AbstractGenericWebContextLoader
should be used as aSmartContextLoader
, not as a legacyContextLoader
. Consequently, this method is not supported.- 参数:
locations
- the resource locations to use to load the application context- 返回:
- a new application context
- 抛出:
UnsupportedOperationException
- in this implementationException
- if context loading failed- 另请参阅:
ContextLoader.loadContext(java.lang.String[])