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...)
Modifier and Type | Field and Description |
---|---|
protected static Log |
logger |
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 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(java.lang.String... locations)
Load a Spring ApplicationContext from the supplied
locations . |
protected void |
prepareContext(GenericApplicationContext context)
Prepare the
GenericApplicationContext created by this ContextLoader . |
protected void |
prepareContext(GenericApplicationContext applicationContext,
MergedContextConfiguration mergedConfig)
Prepare the
GenericApplicationContext created by this
SmartContextLoader before bean definitions are read. |
generateDefaultLocations, getResourceSuffix, isGenerateDefaultLocations, modifyLocations, processContextConfiguration, processLocations
public final ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws java.lang.Exception
MergedContextConfiguration
.
Implementation details:
GenericApplicationContext
instance.prepareContext(GenericApplicationContext, 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.Refreshes
the
context and registers a JVM shutdown hook for it.mergedConfig
- the merged context configuration to use to load the
application contextjava.lang.Exception
- if context loading failedSmartContextLoader.loadContext(MergedContextConfiguration)
,
GenericApplicationContext
public final ConfigurableApplicationContext loadContext(java.lang.String... locations) throws java.lang.Exception
locations
.
Implementation details:
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 prepareContext(GenericApplicationContext, MergedContextConfiguration)
for an alternative.
locations
- the resource locations to use to load the application contextjava.lang.Exception
- if context loading failedContextLoader.loadContext(java.lang.String...)
,
GenericApplicationContext
,
loadContext(MergedContextConfiguration)
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)
,
GenericApplicationContext.setId(java.lang.String)
protected void prepareContext(GenericApplicationContext applicationContext, MergedContextConfiguration mergedConfig)
GenericApplicationContext
created by this
SmartContextLoader
before bean definitions are read.
The default implementation:
prepareContext(GenericApplicationContext)
for backwards
compatibility with the ContextLoader
SPI.MergedContextConfiguration
in the
Environment
of the context.MergedContextConfiguration
and
invokes each with the
given application context.Any ApplicationContextInitializers
implementing
Ordered
or marked with @Order
will be sorted appropriately.
applicationContext
- the newly created application contextmergedConfig
- the merged context configurationApplicationContextInitializer#initialize(GenericApplicationContext)
,
loadContext(MergedContextConfiguration)
,
GenericApplicationContext.setAllowBeanDefinitionOverriding(boolean)
,
GenericApplicationContext.setResourceLoader(org.springframework.core.io.ResourceLoader)
,
GenericApplicationContext.setId(java.lang.String)
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...)