class ContextCache
extends java.lang.Object
ApplicationContexts
in a test environment.
Maintains a cache of contexts
keyed by
MergedContextConfiguration
instances. This has significant performance
benefits if initializing the context would take time. While initializing a
Spring context itself is very quick, some beans in a context, such as a
LocalSessionFactoryBean
for working with Hibernate, may take some time
to initialize. Hence it often makes sense to perform that initialization only
once per test suite.
Modifier and Type | Field and Description |
---|---|
private java.util.Map<MergedContextConfiguration,ApplicationContext> |
contextMap
Map of context keys to Spring ApplicationContext instances.
|
private int |
hitCount |
private int |
missCount |
Constructor and Description |
---|
ContextCache() |
Modifier and Type | Method and Description |
---|---|
(package private) void |
clear()
Clears all contexts from the cache.
|
(package private) void |
clearStatistics()
Clears hit and miss count statistics for the cache (i.e., resets counters
to zero).
|
(package private) boolean |
contains(MergedContextConfiguration key)
Return whether there is a cached context for the given key.
|
(package private) ApplicationContext |
get(MergedContextConfiguration key)
Obtain a cached ApplicationContext for the given key.
|
(package private) int |
getHitCount()
Get the overall hit count for this cache.
|
(package private) int |
getMissCount()
Get the overall miss count for this cache.
|
private void |
incrementHitCount()
Increment the hit count by one.
|
private void |
incrementMissCount()
Increment the miss count by one.
|
(package private) void |
put(MergedContextConfiguration key,
ApplicationContext context)
Explicitly add an ApplicationContext instance to the cache under the given key.
|
(package private) ApplicationContext |
remove(MergedContextConfiguration key)
Remove the context with the given key.
|
(package private) void |
setDirty(MergedContextConfiguration key)
Mark the context with the given key as dirty, effectively
removing the context from the cache and explicitly
closing it if it is an
instance of ConfigurableApplicationContext . |
(package private) int |
size()
Determine the number of contexts currently stored in the cache.
|
java.lang.String |
toString()
|
private final java.util.Map<MergedContextConfiguration,ApplicationContext> contextMap
private int hitCount
private int missCount
void clear()
void clearStatistics()
boolean contains(MergedContextConfiguration key)
key
- the context key (never null
)ApplicationContext get(MergedContextConfiguration key)
key
- the context key (never null
)null
if not found in the cache.remove(org.springframework.test.context.MergedContextConfiguration)
private void incrementHitCount()
private void incrementMissCount()
null
context for a queried key.int getHitCount()
int getMissCount()
null
context for a
queried key.void put(MergedContextConfiguration key, ApplicationContext context)
key
- the context key (never null
)context
- the ApplicationContext instance (never null
)ApplicationContext remove(MergedContextConfiguration key)
key
- the context key (never null
)null
if not found in the cache.setDirty(org.springframework.test.context.MergedContextConfiguration)
void setDirty(MergedContextConfiguration key)
removing
the context from the cache and explicitly
closing
it if it is an
instance of ConfigurableApplicationContext
.
Generally speaking, you would only call this method if you change the state of a singleton bean, potentially affecting future interaction with the context.
key
- the context key (never null
)remove(org.springframework.test.context.MergedContextConfiguration)
int size()
public java.lang.String toString()
toString
in class java.lang.Object