public interface ContextCache
ContextCache
defines the SPI for caching Spring
ApplicationContexts
within the Spring
TestContext Framework.
A ContextCache
maintains a cache of ApplicationContexts
keyed by MergedContextConfiguration
instances.
Context caching can have significant performance benefits if context
initialization is complex. Although the initialization of a Spring context
itself is typically very quick, some beans in a context — for example,
an embedded database or a LocalContainerEntityManagerFactoryBean
for
working with JPA — may take several seconds to initialize. Hence it
often makes sense to perform that initialization only once per test suite or
JVM process.
Modifier and Type | Field and Description |
---|---|
static String |
CONTEXT_CACHE_LOGGING_CATEGORY
The name of the logging category used for reporting
ContextCache
statistics. |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Clear all contexts from the cache, clearing context hierarchy information as well.
|
void |
clearStatistics()
Clear hit and miss count statistics for the cache (i.e., reset counters to zero).
|
boolean |
contains(MergedContextConfiguration key)
Determine whether there is a cached context for the given key.
|
ApplicationContext |
get(MergedContextConfiguration key)
Obtain a cached
ApplicationContext for the given key. |
int |
getHitCount()
Get the overall hit count for this cache.
|
int |
getMissCount()
Get the overall miss count for this cache.
|
int |
getParentContextCount()
Determine the number of parent contexts currently tracked within the cache.
|
void |
logStatistics()
Log the statistics for this
ContextCache at DEBUG level
using the "org.springframework.test.context.cache" logging category. |
void |
put(MergedContextConfiguration key,
ApplicationContext context)
Explicitly add an
ApplicationContext instance to the cache
under the given key. |
void |
remove(MergedContextConfiguration key,
DirtiesContext.HierarchyMode hierarchyMode)
Remove the context with the given key from the cache and explicitly
close
it if it is an instance of
ConfigurableApplicationContext . |
void |
reset()
Reset all state maintained by this cache including statistics.
|
int |
size()
Determine the number of contexts currently stored in the cache.
|
static final String CONTEXT_CACHE_LOGGING_CATEGORY
ContextCache
statistics.boolean contains(MergedContextConfiguration key)
key
- the context key (never null
)true
if the cache contains a context with the given keyApplicationContext get(MergedContextConfiguration key)
ApplicationContext
for the given key.
key
- the context key (never null
)ApplicationContext
instance, or null
if not found in the cacheremove(org.springframework.test.context.MergedContextConfiguration, org.springframework.test.annotation.DirtiesContext.HierarchyMode)
void put(MergedContextConfiguration key, ApplicationContext context)
ApplicationContext
instance to the cache
under the given key.key
- the context key (never null
)context
- the ApplicationContext
instance (never null
)void remove(MergedContextConfiguration key, DirtiesContext.HierarchyMode hierarchyMode)
ConfigurableApplicationContext
.
Generally speaking, this method should be called if the state of a singleton bean has been modified, potentially affecting future interaction with the context.
In addition, the semantics of the supplied HierarchyMode
must
be honored. See the Javadoc for DirtiesContext.HierarchyMode
for details.
key
- the context key; never null
hierarchyMode
- the hierarchy mode; may be null
if the context
is not part of a hierarchyint size()
If the cache contains more than Integer.MAX_VALUE
elements,
this method must return Integer.MAX_VALUE
.
int getParentContextCount()
int getHitCount()
A hit is any access to the cache that returns a non-null context for the queried key.
int getMissCount()
A miss is any access to the cache that returns a null
context for the queried key.
void reset()
clear()
,
clearStatistics()
void clear()
void clearStatistics()
void logStatistics()
ContextCache
at DEBUG
level
using the "org.springframework.test.context.cache" logging category.
The following information should be logged.
ContextCache
implementation