Class AbstractGenericWebContextLoader
- All Implemented Interfaces:
- AotContextLoader,- ContextLoader,- SmartContextLoader
- Direct Known Subclasses:
- AnnotationConfigWebContextLoader,- GenericXmlWebContextLoader
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, Phillip Webb
- See Also:
- 
Field SummaryFields
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionprotected voidconfigureWebResources(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) Configures web resources for the supplied web application context (WAC).protected GenericWebApplicationContextFactory method for creating theGenericWebApplicationContextused by thisContextLoader.protected voidcustomizeBeanFactory(DefaultListableBeanFactory beanFactory, WebMergedContextConfiguration webMergedConfig) Customize the internal bean factory of theWebApplicationContextcreated by this context loader.protected voidcustomizeContext(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) Customize theGenericWebApplicationContextcreated by this context loader after bean definitions have been loaded into the context but before the context is refreshed.protected abstract voidloadBeanDefinitions(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) Load bean definitions into the suppliedcontextfrom the locations or classes in the suppliedWebMergedContextConfiguration.final ApplicationContextloadContext(String... locations) AbstractGenericWebContextLoadershould be used as aSmartContextLoader, not as a legacyContextLoader.final ApplicationContextloadContext(MergedContextConfiguration mergedConfig) Load aGenericWebApplicationContextfor the suppliedMergedContextConfiguration.loadContextForAotProcessing(MergedContextConfiguration mergedConfig) Load aGenericWebApplicationContextfor AOT build-time processing based on the suppliedMergedContextConfiguration.loadContextForAotRuntime(MergedContextConfiguration mergedConfig, ApplicationContextInitializer<ConfigurableApplicationContext> initializer) Load aGenericWebApplicationContextfor AOT run-time execution based on the suppliedMergedContextConfigurationandApplicationContextInitializer.protected voidValidate the suppliedWebMergedContextConfigurationwith respect to what this context loader supports.Methods inherited from class org.springframework.test.context.support.AbstractContextLoadercustomizeContext, generateDefaultLocations, getResourceSuffix, getResourceSuffixes, isGenerateDefaultLocations, modifyLocations, prepareContext, processContextConfiguration, processLocationsMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.springframework.test.context.SmartContextLoaderprocessContextConfiguration, processLocations
- 
Field Details- 
logger
 
- 
- 
Constructor Details- 
AbstractGenericWebContextLoaderpublic AbstractGenericWebContextLoader()
 
- 
- 
Method Details- 
loadContextpublic final ApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception Load aGenericWebApplicationContextfor the suppliedMergedContextConfiguration.Implementation details: - Calls validateMergedContextConfiguration(WebMergedContextConfiguration)to allow subclasses to validate the supplied configuration before proceeding.
- Creates a GenericWebApplicationContextinstance.
- If the supplied MergedContextConfigurationreferences a parent configuration, the correspondingApplicationContextwill 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 theMockServletContextand 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 AnnotationConfigUtilsfor 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.
- Refreshesthe context and registers a JVM shutdown hook for it.
 - Specified by:
- loadContextin interface- SmartContextLoader
- Parameters:
- mergedConfig- the merged context configuration to use to load the application context
- Returns:
- a new web application context
- Throws:
- ContextLoadException- if context loading failed
- Exception
- See Also:
 
- Calls 
- 
loadContextForAotProcessingpublic final GenericWebApplicationContext loadContextForAotProcessing(MergedContextConfiguration mergedConfig) throws Exception Load aGenericWebApplicationContextfor AOT build-time processing based on the suppliedMergedContextConfiguration.In contrast to loadContext(MergedContextConfiguration), this method does not refresh theApplicationContextor register a JVM shutdown hook for it. Otherwise, this method implements behavior identical toloadContext(MergedContextConfiguration).- Specified by:
- loadContextForAotProcessingin interface- AotContextLoader
- 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
- Since:
- 6.0
- See Also:
 
- 
loadContextForAotRuntimepublic final GenericWebApplicationContext loadContextForAotRuntime(MergedContextConfiguration mergedConfig, ApplicationContextInitializer<ConfigurableApplicationContext> initializer) throws Exception Load aGenericWebApplicationContextfor AOT run-time execution based on the suppliedMergedContextConfigurationandApplicationContextInitializer.- Specified by:
- loadContextForAotRuntimein interface- AotContextLoader
- Parameters:
- mergedConfig- the merged context configuration to use to load the application context
- initializer- the- ApplicationContextInitializerthat should be applied to the context in order to recreate bean definitions
- Returns:
- a new web application context
- Throws:
- Exception- if context loading failed
- Since:
- 6.0
- See Also:
 
- 
validateMergedContextConfigurationValidate the suppliedWebMergedContextConfigurationwith respect to what this context loader supports.The default implementation is a no-op but can be overridden by subclasses as appropriate. - Parameters:
- mergedConfig- the merged configuration to validate
- Throws:
- IllegalStateException- if the supplied configuration is not valid for this context loader
- Since:
- 4.0.4
 
- 
createContextFactory method for creating theGenericWebApplicationContextused by thisContextLoader.The default implementation creates a GenericWebApplicationContextusing the default constructor. This method may be overridden — for example, to use a custom context subclass or to create aGenericWebApplicationContextwith a customDefaultListableBeanFactoryimplementation.- Returns:
- a newly instantiated GenericWebApplicationContext
- Since:
- 5.3.23
 
- 
configureWebResourcesprotected void configureWebResources(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) Configures web resources for the supplied web application context (WAC).Implementation DetailsIf 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 ServletContextof the Root WAC will be set as theServletContextfor the supplied WAC.Root WAC Configuration- The resource base path is retrieved from the supplied
 WebMergedContextConfiguration.
- A ResourceLoaderis instantiated for theMockServletContext: if the resource base path is prefixed with "classpath:", aDefaultResourceLoaderwill be used; otherwise, aFileSystemResourceLoaderwill be used.
- A MockServletContextwill be created using the resource base path and resource loader.
- The supplied GenericWebApplicationContextis then stored in theMockServletContextunder theWebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTEkey.
- Finally, the MockServletContextis set in theWebApplicationContext.
 - 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
 
- The resource base path is retrieved from the supplied
 
- 
customizeBeanFactoryprotected void customizeBeanFactory(DefaultListableBeanFactory beanFactory, WebMergedContextConfiguration webMergedConfig) Customize the internal bean factory of theWebApplicationContextcreated 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)
 
 
- 
loadBeanDefinitionsprotected abstract void loadBeanDefinitions(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) Load bean definitions into the suppliedcontextfrom the locations or classes in the suppliedWebMergedContextConfiguration.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:
 
- 
customizeContextprotected void customizeContext(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) Customize theGenericWebApplicationContextcreated 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).- Parameters:
- context- the newly created web application context
- webMergedConfig- the merged context configuration to use to load the web application context
- See Also:
 
- 
loadContextAbstractGenericWebContextLoadershould be used as aSmartContextLoader, not as a legacyContextLoader. Consequently, this method is not supported.- Specified by:
- loadContextin interface- ContextLoader
- Specified by:
- loadContextin interface- SmartContextLoader
- Parameters:
- locations- the resource locations to use to load the application context
- Returns:
- a new application context
- Throws:
- UnsupportedOperationException- in this implementation
- Exception- if context loading failed
- See Also:
 
 
-