public abstract class AbstractGenericContextLoader extends AbstractContextLoader
AbstractContextLoader
that loads a
GenericApplicationContext
.
ContextLoader
SPI, the
context will be loaded from the locations provided to
loadContext(String...)
.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.Concrete subclasses must provide an appropriate implementation of
createBeanDefinitionReader()
,
potentially overriding loadBeanDefinitions()
as well.
loadContext(MergedContextConfiguration)
,
loadContext(String...)
Constructor and Description |
---|
AbstractGenericContextLoader() |
Modifier and Type | Method and Description |
---|---|
protected abstract BeanDefinitionReader |
createBeanDefinitionReader(GenericApplicationContext context)
Factory method for creating a new
BeanDefinitionReader for loading
bean definitions into the supplied context . |
protected GenericApplicationContext |
createContext()
Factory method for creating the
GenericApplicationContext used by
this ContextLoader . |
protected void |
customizeBeanFactory(DefaultListableBeanFactory beanFactory)
Customize the internal bean factory of the ApplicationContext created by
this
ContextLoader . |
protected void |
customizeContext(GenericApplicationContext context)
Customize the
GenericApplicationContext created by this
ContextLoader 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 supplied
context
from the locations or classes in the supplied MergedContextConfiguration . |
ConfigurableApplicationContext |
loadContext(MergedContextConfiguration mergedConfig)
Load a Spring ApplicationContext from the supplied
MergedContextConfiguration . |
ConfigurableApplicationContext |
loadContext(String... locations)
Load a Spring ApplicationContext from the supplied
locations . |
protected void |
prepareContext(GenericApplicationContext context)
Prepare the
GenericApplicationContext created by this ContextLoader . |
protected void |
validateMergedContextConfiguration(MergedContextConfiguration mergedConfig)
Validate the supplied
MergedContextConfiguration with respect to
what this context loader supports. |
customizeContext, generateDefaultLocations, getResourceSuffix, getResourceSuffixes, isGenerateDefaultLocations, modifyLocations, prepareContext, processContextConfiguration, processLocations
protected static final Log logger
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception
MergedContextConfiguration
.
Implementation details:
validateMergedContextConfiguration(MergedContextConfiguration)
to allow subclasses to validate the supplied configuration before proceeding.createContext()
to create a GenericApplicationContext
instance.MergedContextConfiguration
references a
parent configuration,
the corresponding ApplicationContext
will be retrieved and
set as the parent
for the context created by this method.prepareContext(GenericApplicationContext)
for backwards
compatibility with the ContextLoader
SPI.AbstractContextLoader.prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)
to allow for customizing the context before bean definitions are loaded.customizeBeanFactory(DefaultListableBeanFactory)
to allow for customizing the
context's DefaultListableBeanFactory
.loadBeanDefinitions(GenericApplicationContext, MergedContextConfiguration)
to populate the context from the locations or classes in the supplied
MergedContextConfiguration
.AnnotationConfigUtils
for
registering
annotation configuration processors.customizeContext(GenericApplicationContext)
to allow for customizing the context
before it is refreshed.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.mergedConfig
- the merged context configuration to use to load the
application contextException
- if context loading failedSmartContextLoader.loadContext(MergedContextConfiguration)
,
GenericApplicationContext
protected void validateMergedContextConfiguration(MergedContextConfiguration mergedConfig)
MergedContextConfiguration
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 loaderpublic final ConfigurableApplicationContext loadContext(String... locations) throws Exception
locations
.
Implementation details:
createContext()
to create a GenericApplicationContext
instance.prepareContext(GenericApplicationContext)
to allow for customizing the context
before bean definitions are loaded.customizeBeanFactory(DefaultListableBeanFactory)
to allow for customizing the
context's DefaultListableBeanFactory
.createBeanDefinitionReader(GenericApplicationContext)
to create a
BeanDefinitionReader
which is then used to populate the context
from the specified locations.AnnotationConfigUtils
for
registering
annotation configuration processors.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)
and AbstractContextLoader.prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)
for an alternative.
locations
- the resource locations to use to load the application contextException
- if context loading failedContextLoader.loadContext(java.lang.String...)
,
GenericApplicationContext
,
loadContext(MergedContextConfiguration)
protected GenericApplicationContext createContext()
GenericApplicationContext
used by
this ContextLoader
.
The default implementation creates a GenericApplicationContext
using the default constructor. This method may get overridden e.g. to use
a custom context subclass or to create a GenericApplicationContext
with a custom DefaultListableBeanFactory
implementation.
GenericApplicationContext
protected void prepareContext(GenericApplicationContext context)
GenericApplicationContext
created by this ContextLoader
.
Called before bean definitions are read.
The default implementation is empty. Can be overridden in subclasses to
customize GenericApplicationContext
's standard settings.
context
- the context that should be preparedloadContext(MergedContextConfiguration)
,
loadContext(String...)
,
GenericApplicationContext.setAllowBeanDefinitionOverriding(boolean)
,
GenericApplicationContext.setResourceLoader(org.springframework.core.io.ResourceLoader)
,
AbstractApplicationContext.setId(java.lang.String)
,
AbstractContextLoader.prepareContext(ConfigurableApplicationContext, MergedContextConfiguration)
protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory)
ContextLoader
.
The default implementation is empty but can be overridden in subclasses
to customize DefaultListableBeanFactory
's standard settings.
beanFactory
- the bean factory created by this ContextLoader
loadContext(MergedContextConfiguration)
,
loadContext(String...)
,
DefaultListableBeanFactory.setAllowBeanDefinitionOverriding(boolean)
,
DefaultListableBeanFactory.setAllowEagerClassLoading(boolean)
,
AbstractAutowireCapableBeanFactory.setAllowCircularReferences(boolean)
,
AbstractAutowireCapableBeanFactory.setAllowRawInjectionDespiteWrapping(boolean)
protected void loadBeanDefinitions(GenericApplicationContext context, MergedContextConfiguration mergedConfig)
context
from the locations or classes in the supplied MergedContextConfiguration
.
The default implementation delegates to the BeanDefinitionReader
returned by createBeanDefinitionReader(GenericApplicationContext)
to
load
the
bean definitions.
Subclasses must provide an appropriate implementation of
createBeanDefinitionReader(GenericApplicationContext)
. Alternatively subclasses
may provide a no-op implementation of createBeanDefinitionReader()
and override this method to provide a custom strategy for loading or
registering bean definitions.
context
- the context into which the bean definitions should be loadedmergedConfig
- the merged context configurationloadContext(MergedContextConfiguration)
protected abstract BeanDefinitionReader createBeanDefinitionReader(GenericApplicationContext context)
BeanDefinitionReader
for loading
bean definitions into the supplied context
.context
- the context for which the BeanDefinitionReader
should be createdBeanDefinitionReader
for the supplied contextloadContext(String...)
,
loadBeanDefinitions(org.springframework.context.support.GenericApplicationContext, org.springframework.test.context.MergedContextConfiguration)
,
BeanDefinitionReader
protected void customizeContext(GenericApplicationContext context)
GenericApplicationContext
created by this
ContextLoader
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.
context
- the newly created application contextloadContext(MergedContextConfiguration)
,
loadContext(String...)
,
AbstractContextLoader.customizeContext(ConfigurableApplicationContext, MergedContextConfiguration)