Interface ContextCache
- All Known Implementing Classes:
- DefaultContextCache
ContextCache defines the SPI for caching Spring
ApplicationContexts within the
Spring TestContext Framework.
A ContextCache maintains a cache of ApplicationContexts
keyed by MergedContextConfiguration instances, potentially configured
with a maximum size and
a custom eviction policy.
As of Spring Framework 6.1, this SPI includes optional support for tracking and incrementing failure counts. As of Spring Framework 7.0, this SPI includes optional support for registering and unregistering context usage.
Rationale
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.
- Since:
- 4.2
- Author:
- Sam Brannen, Juergen Hoeller
- See Also:
- 
Nested Class SummaryNested ClassesModifier and TypeInterfaceDescriptionstatic interfaceRepresents a function that loads anApplicationContext.
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringThe name of the logging category used for reportingContextCachestatistics.static final intThe default maximum size of the context cache: 32.static final StringSystem property used to configure the maximum size of theContextCacheas a positive integer: "spring.test.context.cache.maxSize".
- 
Method SummaryModifier and TypeMethodDescriptionvoidclear()Clear all contexts from the cache, clearing context hierarchy information as well.voidClear hit count and miss count statistics for the cache (i.e., reset counters to zero).booleanDetermine whether there is a cached context for the given key.Obtain a cachedApplicationContextfor the given key.default intDetermine the number of contexts within the cache that are currently in use.default intGet the failure count for the given key.intGet the overall hit count for this cache.intGet the overall miss count for this cache.intDetermine the number of parent contexts currently tracked within the cache.default voidIncrement the failure count for the given key.voidLog the statistics for thisContextCacheatDEBUGlevel using the "org.springframework.test.context.cache" logging category.voidput(MergedContextConfiguration key, ApplicationContext context) Deprecated.default ApplicationContextput(MergedContextConfiguration key, ContextCache.LoadFunction loadFunction) Explicitly add anApplicationContextto the cache under the given key, potentially honoring a custom eviction policy.default voidregisterContextUsage(MergedContextConfiguration key, Class<?> testClass) Register usage of theApplicationContextfor the suppliedMergedContextConfigurationand any of its parents.voidremove(MergedContextConfiguration key, @Nullable DirtiesContext.HierarchyMode hierarchyMode) Remove the context with the given key from the cache and explicitly close it if it is an instance ofConfigurableApplicationContext.voidreset()Reset all state maintained by this cache including statistics.intsize()Determine the number of contexts currently stored in the cache.default voidunregisterContextUsage(MergedContextConfiguration key, Class<?> testClass) Unregister usage of theApplicationContextfor the suppliedMergedContextConfigurationand any of its parents.
- 
Field Details- 
CONTEXT_CACHE_LOGGING_CATEGORYThe name of the logging category used for reportingContextCachestatistics.- See Also:
 
- 
DEFAULT_MAX_CONTEXT_CACHE_SIZEstatic final int DEFAULT_MAX_CONTEXT_CACHE_SIZEThe default maximum size of the context cache: 32.- Since:
- 4.3
- See Also:
 
- 
MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAMESystem property used to configure the maximum size of theContextCacheas a positive integer: "spring.test.context.cache.maxSize".May alternatively be configured via the SpringPropertiesmechanism.Note that implementations of ContextCacheare not required to actually support a maximum cache size. Consult the documentation of the corresponding implementation for details.- Since:
- 4.3
- See Also:
 
 
- 
- 
Method Details- 
containsDetermine whether there is a cached context for the given key.- Parameters:
- key- the context key; never- null
- Returns:
- trueif the cache contains a context with the given key
 
- 
getObtain a cachedApplicationContextfor the given key.If the cached application context was previously paused, it must be restarted. This applies to parent contexts as well. In addition, the hit and miss counts must be updated accordingly. - Parameters:
- key- the context key; never- null
- Returns:
- the corresponding ApplicationContextinstance, ornullif not found in the cache
- See Also:
 
- 
putDeprecated.since Spring Framework 7.0 in favor ofput(MergedContextConfiguration, LoadFunction)Explicitly add anApplicationContextto the cache under the given key, potentially honoring a custom eviction policy.- Parameters:
- key- the context key; never- null
- context- the- ApplicationContext; never- null
- See Also:
 
- 
putdefault ApplicationContext put(MergedContextConfiguration key, ContextCache.LoadFunction loadFunction) Explicitly add anApplicationContextto the cache under the given key, potentially honoring a custom eviction policy.The supplied ContextCache.LoadFunctionwill be invoked to load theApplicationContext.Concrete implementations which honor a custom eviction policy must override this method to ensure that an evicted context is removed from the cache and closed before a new context is loaded via the supplied LoadFunction.- Parameters:
- key- the context key; never- null
- loadFunction- a function which loads the context for the supplied key; never- null
- Returns:
- the ApplicationContext; nevernull
- Since:
- 7.0
- See Also:
 
- 
removeRemove the context with the given key from the cache and explicitly close it if it is an instance ofConfigurableApplicationContext.Generally speaking, this method should be called to properly evict a context from the cache (for example, due to a custom eviction policy) or if the state of a singleton bean has been modified, potentially affecting future interaction with the context. In addition, the semantics of the supplied HierarchyModemust be honored. See the Javadoc forDirtiesContext.HierarchyModefor details.- Parameters:
- key- the context key; never- null
- hierarchyMode- the hierarchy mode; may be- nullif the context is not part of a hierarchy
 
- 
getFailureCountGet the failure count for the given key.A failure is any attempt to load the ApplicationContextfor the given key that results in an exception.The default implementation of this method always returns 0. Concrete implementations are therefore highly encouraged to override this method andincrementFailureCount(MergedContextConfiguration)with appropriate behavior. Note that the standardContextContextimplementation in Spring overrides these methods appropriately.- Parameters:
- key- the context key; never- null
- Since:
- 6.1
- See Also:
 
- 
incrementFailureCountIncrement the failure count for the given key.The default implementation of this method does nothing. Concrete implementations are therefore highly encouraged to override this method and getFailureCount(MergedContextConfiguration)with appropriate behavior. Note that the standardContextContextimplementation in Spring overrides these methods appropriately.- Parameters:
- key- the context key; never- null
- Since:
- 6.1
- See Also:
 
- 
registerContextUsageRegister usage of theApplicationContextfor the suppliedMergedContextConfigurationand any of its parents.The default implementation of this method does nothing. Concrete implementations are therefore highly encouraged to override this method, unregisterContextUsage(MergedContextConfiguration, Class), andgetContextUsageCount()with appropriate behavior. Note that the standardContextContextimplementation in Spring overrides these methods appropriately.- Parameters:
- key- the context key; never- null
- testClass- the test class that is using the application context(s)
- Since:
- 7.0
- See Also:
 
- 
unregisterContextUsageUnregister usage of theApplicationContextfor the suppliedMergedContextConfigurationand any of its parents.If no other test classes are actively using the same application context(s), the application context(s) should be paused. The default implementation of this method does nothing. Concrete implementations are therefore highly encouraged to override this method, registerContextUsage(MergedContextConfiguration, Class), andgetContextUsageCount()with appropriate behavior. Note that the standardContextContextimplementation in Spring overrides these methods appropriately.- Parameters:
- key- the context key; never- null
- testClass- the test class that is no longer using the application context(s)
- Since:
- 7.0
- See Also:
 
- 
getContextUsageCountdefault int getContextUsageCount()Determine the number of contexts within the cache that are currently in use.The default implementation of this method always returns 0. Concrete implementations are therefore highly encouraged to override this method,registerContextUsage(MergedContextConfiguration, Class), andunregisterContextUsage(MergedContextConfiguration, Class)with appropriate behavior. Note that the standardContextContextimplementation in Spring overrides these methods appropriately.- Since:
- 7.0
- See Also:
 
- 
sizeint size()Determine the number of contexts currently stored in the cache.If the cache contains more than Integer.MAX_VALUEelements, this method must returnInteger.MAX_VALUE.
- 
getParentContextCountint getParentContextCount()Determine the number of parent contexts currently tracked within the cache.
- 
getHitCountint getHitCount()Get the overall hit count for this cache.A hit is any access to the cache that returns a non-null context for the queried key. 
- 
getMissCountint getMissCount()Get the overall miss count for this cache.A miss is any access to the cache that returns a nullcontext for the queried key.
- 
resetvoid reset()Reset all state maintained by this cache including statistics.- See Also:
 
- 
clearvoid clear()Clear all contexts from the cache, clearing context hierarchy information as well.
- 
clearStatisticsvoid clearStatistics()Clear hit count and miss count statistics for the cache (i.e., reset counters to zero).
- 
logStatisticsvoid logStatistics()Log the statistics for thisContextCacheatDEBUGlevel using the "org.springframework.test.context.cache" logging category.The following information should be logged. - name of the concrete ContextCacheimplementation
- size()
- context usage count
- parent context count
- hit count
- miss count
- any other information useful for monitoring the state of this cache
 
- name of the concrete 
 
- 
put(MergedContextConfiguration, LoadFunction)