public class DefaultCacheAwareContextLoaderDelegate extends Object implements CacheAwareContextLoaderDelegate
CacheAwareContextLoaderDelegate
interface.
To use a static DefaultContextCache
, invoke the
DefaultCacheAwareContextLoaderDelegate()
constructor; otherwise,
invoke the DefaultCacheAwareContextLoaderDelegate(ContextCache)
and provide a custom ContextCache
implementation.
Constructor and Description |
---|
DefaultCacheAwareContextLoaderDelegate()
Construct a new
DefaultCacheAwareContextLoaderDelegate using
a static DefaultContextCache . |
DefaultCacheAwareContextLoaderDelegate(ContextCache contextCache)
Construct a new
DefaultCacheAwareContextLoaderDelegate using
the supplied ContextCache . |
Modifier and Type | Method and Description |
---|---|
void |
closeContext(MergedContextConfiguration mergedContextConfiguration,
DirtiesContext.HierarchyMode hierarchyMode)
Remove the application context for the
supplied
MergedContextConfiguration from the ContextCache
and close it if it is
an instance of ConfigurableApplicationContext . |
protected ContextCache |
getContextCache()
Get the
ContextCache used by this context loader delegate. |
boolean |
isContextLoaded(MergedContextConfiguration mergedContextConfiguration)
Determine if the application context for
the supplied
MergedContextConfiguration has been loaded (i.e.,
is present in the ContextCache ). |
ApplicationContext |
loadContext(MergedContextConfiguration mergedContextConfiguration)
Load the application context for the supplied
MergedContextConfiguration by delegating to the ContextLoader
configured in the given MergedContextConfiguration . |
protected ApplicationContext |
loadContextInternal(MergedContextConfiguration mergedContextConfiguration)
Load the
ApplicationContext for the supplied merged context configuration. |
public DefaultCacheAwareContextLoaderDelegate()
DefaultCacheAwareContextLoaderDelegate
using
a static DefaultContextCache
.
This default cache is static so that each context can be cached and reused for all subsequent tests that declare the same unique context configuration within the same JVM process.
public DefaultCacheAwareContextLoaderDelegate(ContextCache contextCache)
DefaultCacheAwareContextLoaderDelegate
using
the supplied ContextCache
.DefaultCacheAwareContextLoaderDelegate()
protected ContextCache getContextCache()
ContextCache
used by this context loader delegate.protected ApplicationContext loadContextInternal(MergedContextConfiguration mergedContextConfiguration) throws Exception
ApplicationContext
for the supplied merged context configuration.
Supports both the SmartContextLoader
and ContextLoader
SPIs.
Exception
- if an error occurs while loading the application contextpublic boolean isContextLoaded(MergedContextConfiguration mergedContextConfiguration)
CacheAwareContextLoaderDelegate
MergedContextConfiguration
has been loaded (i.e.,
is present in the ContextCache
).
Implementations of this method must not load the
application context as a side effect. In addition, implementations of
this method should not log the cache statistics via
ContextCache.logStatistics()
.
The default implementation of this method always returns false
.
Custom CacheAwareContextLoaderDelegate
implementations are
therefore highly encouraged to override this method with a more meaningful
implementation. Note that the standard CacheAwareContextLoaderDelegate
implementation in Spring overrides this method appropriately.
isContextLoaded
in interface CacheAwareContextLoaderDelegate
mergedContextConfiguration
- the merged context configuration used
to load the application context; never null
true
if the application context has been loadedCacheAwareContextLoaderDelegate.loadContext(org.springframework.test.context.MergedContextConfiguration)
,
CacheAwareContextLoaderDelegate.closeContext(org.springframework.test.context.MergedContextConfiguration, org.springframework.test.annotation.DirtiesContext.HierarchyMode)
public ApplicationContext loadContext(MergedContextConfiguration mergedContextConfiguration)
CacheAwareContextLoaderDelegate
MergedContextConfiguration
by delegating to the ContextLoader
configured in the given MergedContextConfiguration
.
If the context is present in the ContextCache
it will simply
be returned; otherwise, it will be loaded, stored in the cache, and returned.
The cache statistics should be logged by invoking
ContextCache.logStatistics()
.
loadContext
in interface CacheAwareContextLoaderDelegate
mergedContextConfiguration
- the merged context configuration to use
to load the application context; never null
null
)CacheAwareContextLoaderDelegate.isContextLoaded(org.springframework.test.context.MergedContextConfiguration)
,
CacheAwareContextLoaderDelegate.closeContext(org.springframework.test.context.MergedContextConfiguration, org.springframework.test.annotation.DirtiesContext.HierarchyMode)
public void closeContext(MergedContextConfiguration mergedContextConfiguration, @Nullable DirtiesContext.HierarchyMode hierarchyMode)
CacheAwareContextLoaderDelegate
MergedContextConfiguration
from the ContextCache
and close it if it is
an instance of ConfigurableApplicationContext
.
The semantics of the supplied HierarchyMode
must be honored when
removing the context from the cache. See the Javadoc for DirtiesContext.HierarchyMode
for details.
Generally speaking, this method should only be called if the state of a singleton bean has been changed (potentially affecting future interaction with the context) or if the context needs to be prematurely removed from the cache.
closeContext
in interface CacheAwareContextLoaderDelegate
mergedContextConfiguration
- the merged context configuration for the
application context to close; never null
hierarchyMode
- the hierarchy mode; may be null
if the context
is not part of a hierarchyCacheAwareContextLoaderDelegate.isContextLoaded(org.springframework.test.context.MergedContextConfiguration)
,
CacheAwareContextLoaderDelegate.loadContext(org.springframework.test.context.MergedContextConfiguration)