Class DefaultCacheAwareContextLoaderDelegate
- All Implemented Interfaces:
CacheAwareContextLoaderDelegate
CacheAwareContextLoaderDelegate strategy.
To use a static DefaultContextCache, invoke the
DefaultCacheAwareContextLoaderDelegate() constructor; otherwise,
invoke the DefaultCacheAwareContextLoaderDelegate(ContextCache)
and provide a custom ContextCache implementation.
As of Spring Framework 6.0, this class loads ApplicationContextFailureProcessor
implementations via the SpringFactoriesLoader mechanism and delegates to them in
loadContext(MergedContextConfiguration) to process context load failures.
As of Spring Framework 6.1, this class supports the failure threshold
feature described in CacheAwareContextLoaderDelegate.loadContext(MergedContextConfiguration),
delegating to ContextCacheUtils.retrieveContextFailureThreshold() to
obtain the threshold value to use.
As of Spring Framework 7.0, this class provides support for registering and unregistering context usage.
- Since:
- 4.1
- Author:
- Sam Brannen
-
Field Summary
Fields inherited from interface CacheAwareContextLoaderDelegate
CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME, DEFAULT_CONTEXT_FAILURE_THRESHOLD -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a newDefaultCacheAwareContextLoaderDelegateusing a staticDefaultContextCache.DefaultCacheAwareContextLoaderDelegate(ContextCache contextCache) Construct a newDefaultCacheAwareContextLoaderDelegateusing the suppliedContextCacheand the default or user-configured context failure threshold. -
Method Summary
Modifier and TypeMethodDescriptionvoidcloseContext(MergedContextConfiguration mergedConfig, @Nullable DirtiesContext.HierarchyMode hierarchyMode) Remove the application context for the suppliedMergedContextConfigurationfrom theContextCacheand close it if it is an instance ofConfigurableApplicationContext.protected ContextCacheGet theContextCacheused by this context loader delegate.booleanisContextLoaded(MergedContextConfiguration mergedConfig) Determine if the application context for the suppliedMergedContextConfigurationhas been loaded (i.e., is present in theContextCache).loadContext(MergedContextConfiguration mergedConfig) Load the application context for the suppliedMergedContextConfigurationby delegating to theContextLoaderconfigured in the givenMergedContextConfiguration.protected ApplicationContextloadContextInAotMode(org.springframework.test.context.cache.AotMergedContextConfiguration aotMergedConfig) protected ApplicationContextloadContextInternal(MergedContextConfiguration mergedConfig) Load theApplicationContextfor the supplied merged context configuration.voidregisterContextUsage(MergedContextConfiguration mergedConfig, Class<?> testClass) Register usage of the application context for the suppliedMergedContextConfigurationas well as usage of the application context for its parent, recursively.voidunregisterContextUsage(MergedContextConfiguration mergedConfig, Class<?> testClass) Unregister usage of the application context for the suppliedMergedContextConfigurationas well as usage of the application context for its parent, recursively.
-
Constructor Details
-
DefaultCacheAwareContextLoaderDelegate
public DefaultCacheAwareContextLoaderDelegate()Construct a newDefaultCacheAwareContextLoaderDelegateusing a staticDefaultContextCache.The 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.
- See Also:
-
DefaultCacheAwareContextLoaderDelegate
Construct a newDefaultCacheAwareContextLoaderDelegateusing the suppliedContextCacheand the default or user-configured context failure threshold.- See Also:
-
-
Method Details
-
isContextLoaded
Description copied from interface:CacheAwareContextLoaderDelegateDetermine if the application context for the suppliedMergedContextConfigurationhas been loaded (i.e., is present in theContextCache).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. CustomCacheAwareContextLoaderDelegateimplementations are therefore highly encouraged to override this method with a more meaningful implementation. Note that the standardCacheAwareContextLoaderDelegateimplementation in Spring overrides this method appropriately.- Specified by:
isContextLoadedin interfaceCacheAwareContextLoaderDelegate- Parameters:
mergedConfig- the merged context configuration used to load the application context; nevernull- Returns:
trueif the application context has been loaded- See Also:
-
loadContext
Description copied from interface:CacheAwareContextLoaderDelegateLoad the application context for the suppliedMergedContextConfigurationby delegating to theContextLoaderconfigured in the givenMergedContextConfiguration.If the context is present in the
ContextCacheit will simply be returned; otherwise, it will be loaded, stored in the cache, and returned.As of Spring Framework 6.0, implementations of this method should load
ApplicationContextFailureProcessorimplementations via theSpringFactoriesLoadermechanism, catch any exception thrown by theContextLoader, and delegate to each of the configured failure processors to process the context load failure if the exception is an instance ofContextLoadException.As of Spring Framework 6.1, implementations of this method are encouraged to support the failure threshold feature. Specifically, if repeated attempts are made to load an application context and that application context consistently fails to load — for example, due to a configuration error that prevents the context from successfully loading — this method should preemptively throw an
IllegalStateExceptionif the configured failure threshold has been exceeded. Note that theContextCacheprovides support for tracking and incrementing the failure count for a given context cache key.The cache statistics should be logged by invoking
ContextCache.logStatistics().- Specified by:
loadContextin interfaceCacheAwareContextLoaderDelegate- Parameters:
mergedConfig- the merged context configuration to use to load the application context; nevernull- Returns:
- the application context (never
null) - See Also:
-
closeContext
public void closeContext(MergedContextConfiguration mergedConfig, @Nullable DirtiesContext.HierarchyMode hierarchyMode) Description copied from interface:CacheAwareContextLoaderDelegateRemove the application context for the suppliedMergedContextConfigurationfrom theContextCacheand close it if it is an instance ofConfigurableApplicationContext.The semantics of the supplied
HierarchyModemust be honored when removing the context from the cache. See the Javadoc forDirtiesContext.HierarchyModefor 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.
- Specified by:
closeContextin interfaceCacheAwareContextLoaderDelegate- Parameters:
mergedConfig- the merged context configuration for the application context to close; nevernullhierarchyMode- the hierarchy mode; may benullif the context is not part of a hierarchy- See Also:
-
registerContextUsage
Description copied from interface:CacheAwareContextLoaderDelegateRegister usage of the application context for the suppliedMergedContextConfigurationas well as usage of the application context for its parent, recursively.This is intended to be invoked whenever a
TestExecutionListenerinteracts with the application context(s) on behalf of the supplied test class.- Specified by:
registerContextUsagein interfaceCacheAwareContextLoaderDelegate- Parameters:
mergedConfig- the context key; nevernulltestClass- the test class that is using the application context(s)- See Also:
-
unregisterContextUsage
Description copied from interface:CacheAwareContextLoaderDelegateUnregister usage of the application context for the suppliedMergedContextConfigurationas well as usage of the application context for its parent, recursively.This informs the
ContextCachethat the application context(s) can be safely paused if no other test classes are actively using the same application context(s).- Specified by:
unregisterContextUsagein interfaceCacheAwareContextLoaderDelegate- Parameters:
mergedConfig- the context key; nevernulltestClass- the test class that is no longer using the application context(s)- See Also:
-
getContextCache
Get theContextCacheused by this context loader delegate. -
loadContextInternal
protected ApplicationContext loadContextInternal(MergedContextConfiguration mergedConfig) throws Exception Load theApplicationContextfor the supplied merged context configuration.Supports both the
SmartContextLoaderandContextLoaderSPIs.- Throws:
Exception- if an error occurs while loading the application context
-
loadContextInAotMode
protected ApplicationContext loadContextInAotMode(org.springframework.test.context.cache.AotMergedContextConfiguration aotMergedConfig) throws Exception - Throws:
Exception
-