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 ofAbstractContextLoader
that loads aGenericApplicationContext
.- If instances of concrete subclasses are invoked via the
ContextLoader
SPI, the context will be loaded from the locations provided toloadContext(String...)
. - 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.
Concrete subclasses must provide an appropriate implementation of
createBeanDefinitionReader()
, potentially overridingloadBeanDefinitions()
as well.- Since:
- 2.5
- Author:
- Sam Brannen, Juergen Hoeller, Phillip Webb
- See Also:
loadContext(MergedContextConfiguration)
,loadContext(String...)
Constructor Summary
Constructors Constructor Description AbstractGenericContextLoader()
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract BeanDefinitionReader
createBeanDefinitionReader(GenericApplicationContext context)
Factory method for creating a newBeanDefinitionReader
for loading bean definitions into the suppliedcontext
.protected void
customizeBeanFactory(DefaultListableBeanFactory beanFactory)
Customize the internal bean factory of the ApplicationContext created by thisContextLoader
.protected void
customizeContext(GenericApplicationContext context)
Customize theGenericApplicationContext
created by thisContextLoader
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 suppliedcontext
from the locations or classes in the suppliedMergedContextConfiguration
.ConfigurableApplicationContext
loadContext(String... locations)
Load a Spring ApplicationContext from the suppliedlocations
.ConfigurableApplicationContext
loadContext(MergedContextConfiguration mergedConfig)
Load a Spring ApplicationContext from the suppliedMergedContextConfiguration
.protected void
prepareContext(GenericApplicationContext context)
Prepare theGenericApplicationContext
created by thisContextLoader
.protected void
validateMergedContextConfiguration(MergedContextConfiguration mergedConfig)
Validate the suppliedMergedContextConfiguration
with respect to what this context loader supports.Methods inherited from class org.springframework.test.context.support.AbstractContextLoader
customizeContext, generateDefaultLocations, getResourceSuffix, getResourceSuffixes, isGenerateDefaultLocations, modifyLocations, prepareContext, processContextConfiguration, processLocations
Constructor Detail
AbstractGenericContextLoader
public AbstractGenericContextLoader()
Method Detail
loadContext
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception
Load a Spring ApplicationContext from the suppliedMergedContextConfiguration
.Implementation details:
- Calls
validateMergedContextConfiguration(MergedContextConfiguration)
to allow subclasses to validate the supplied configuration before proceeding. - Creates a
GenericApplicationContext
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. - Calls
prepareContext(GenericApplicationContext)
for backwards compatibility with theContextLoader
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'sDefaultListableBeanFactory
. - Delegates to
loadBeanDefinitions(GenericApplicationContext, MergedContextConfiguration)
to populate the context from the locations or classes in the suppliedMergedContextConfiguration
. - Delegates to
AnnotationConfigUtils
forregistering
annotation configuration processors. - Calls
customizeContext(GenericApplicationContext)
to allow for customizing the context before it is refreshed. - Calls
AbstractContextLoader.customizeContext(ConfigurableApplicationContext, MergedContextConfiguration)
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
- Calls
validateMergedContextConfiguration
protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig)
Validate the suppliedMergedContextConfiguration
with 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
loadContext
public final ConfigurableApplicationContext loadContext(String... locations) throws Exception
Load a Spring ApplicationContext from the suppliedlocations
.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'sDefaultListableBeanFactory
. - Delegates to
createBeanDefinitionReader(GenericApplicationContext)
to create aBeanDefinitionReader
which is then used to populate the context from the specified locations. - Delegates to
AnnotationConfigUtils
forregistering
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)
andAbstractContextLoader.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)
- Creates a
prepareContext
protected void prepareContext(GenericApplicationContext context)
Prepare theGenericApplicationContext
created by thisContextLoader
. 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)
,AbstractApplicationContext.setId(java.lang.String)
,AbstractContextLoader.prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)
customizeBeanFactory
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory)
Customize the internal bean factory of the ApplicationContext created by thisContextLoader
.The default implementation is empty but can be overridden in subclasses to customize
DefaultListableBeanFactory
's standard settings.- Parameters:
beanFactory
- the bean factory created by thisContextLoader
- 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 suppliedcontext
from the locations or classes in the suppliedMergedContextConfiguration
.The default implementation delegates to the
BeanDefinitionReader
returned bycreateBeanDefinitionReader(GenericApplicationContext)
toload
the bean definitions.Subclasses must provide an appropriate implementation of
createBeanDefinitionReader(GenericApplicationContext)
. Alternatively subclasses may provide a no-op implementation ofcreateBeanDefinitionReader()
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 loadedmergedConfig
- the merged context configuration- Since:
- 3.1
- See Also:
loadContext(MergedContextConfiguration)
createBeanDefinitionReader
protected abstract BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context)
Factory method for creating a newBeanDefinitionReader
for loading bean definitions into the suppliedcontext
.- Parameters:
context
- the context for which theBeanDefinitionReader
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 theGenericApplicationContext
created by thisContextLoader
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...)
,AbstractContextLoader.customizeContext(ConfigurableApplicationContext, MergedContextConfiguration)