Package org.springframework.test.context

# Interface CacheAwareContextLoaderDelegate

All Known Implementing Classes:
  `DefaultCacheAwareContextLoaderDelegate`

public interface CacheAwareContextLoaderDelegate
A `CacheAwareContextLoaderDelegate` is responsible for loading and closing application contexts, interacting transparently with a `ContextCache` behind the scenes.

Note: `CacheAwareContextLoaderDelegate` does not extend the `ContextLoader` or `SmartContextLoader` interface.

Since:
  3.2.2
Author:
  Sam Brannen

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME`
System property used to configure the failure threshold for errors encountered while attempting to load an application context: "spring.test.context.failure.threshold".
`static final int`
`DEFAULT_CONTEXT_FAILURE_THRESHOLD`
The default failure threshold for errors encountered while attempting to load an application context: 1.

## Method Summary

Modifier and Type
Method
Description
`void`
`closeContext(MergedContextConfiguration mergedConfig, DirtiesContext.HierarchyMode hierarchyMode)`
Remove the application context for the supplied `MergedContextConfiguration` from the `ContextCache` and close it if it is an instance of `ConfigurableApplicationContext`.
`default boolean`
`isContextLoaded(MergedContextConfiguration mergedConfig)`
Determine if the application context for the supplied `MergedContextConfiguration` has been loaded (i.e., is present in the `ContextCache`).
`org.springframework.context.ApplicationContext`
`loadContext(MergedContextConfiguration mergedConfig)`
Load the application context for the supplied `MergedContextConfiguration` by delegating to the `ContextLoader` configured in the given `MergedContextConfiguration`.

## Field Details

### DEFAULT_CONTEXT_FAILURE_THRESHOLD

static final int DEFAULT_CONTEXT_FAILURE_THRESHOLD
The default failure threshold for errors encountered while attempting to load an application context: 1.

Since:
  6.1
See Also:
  - `CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME`
  - Constant Field Values

### CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME

static final String CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME
System property used to configure the failure threshold for errors encountered while attempting to load an application context: "spring.test.context.failure.threshold".

May alternatively be configured via the `SpringProperties` mechanism.

Implementations of `CacheAwareContextLoaderDelegate` are not required to support this feature. Consult the documentation of the corresponding implementation for details. Note, however, that the standard `CacheAwareContextLoaderDelegate` implementation in Spring supports this feature.

Since:
  6.1
See Also:
  - `DEFAULT_CONTEXT_FAILURE_THRESHOLD`
  - `loadContext(MergedContextConfiguration)`
  - Constant Field Values

## Method Details

### isContextLoaded

default boolean isContextLoaded(MergedContextConfiguration mergedConfig)
Determine if the application context for the supplied `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.

Parameters:
  `mergedConfig` - the merged context configuration used to load the application context; never `null`
Returns:
  `true` if the application context has been loaded
Since:
  5.2
See Also:
  - `loadContext(org.springframework.test.context.MergedContextConfiguration)`
  - `closeContext(org.springframework.test.context.MergedContextConfiguration, org.springframework.test.annotation.DirtiesContext.HierarchyMode)`

### loadContext

org.springframework.context.ApplicationContext loadContext(MergedContextConfiguration mergedConfig)
Load the application context for the supplied `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.

As of Spring Framework 6.0, implementations of this method should load `ApplicationContextFailureProcessor` implementations via the `SpringFactoriesLoader` mechanism, catch any exception thrown by the `ContextLoader`, and delegate to each of the configured failure processors to process the context load failure if the exception is an instance of `ContextLoadException`.

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 `IllegalStateException` if the configured failure threshold has been exceeded. Note that the `ContextCache` provides support for tracking and incrementing the failure count for a given context cache key.

The cache statistics should be logged by invoking `ContextCache.logStatistics()`.

Parameters:
  `mergedConfig` - the merged context configuration to use to load the application context; never `null`
Returns:
  the application context (never `null`)
Throws:
  `IllegalStateException` - if an error occurs while retrieving or loading the application context
See Also:
  - `isContextLoaded(org.springframework.test.context.MergedContextConfiguration)`
  - `closeContext(org.springframework.test.context.MergedContextConfiguration, org.springframework.test.annotation.DirtiesContext.HierarchyMode)`
  - `CONTEXT_FAILURE_THRESHOLD_PROPERTY_NAME`

### closeContext

void closeContext(MergedContextConfiguration mergedConfig, @Nullable DirtiesContext.HierarchyMode hierarchyMode)
Remove the application context for the supplied `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.

Parameters:
  `mergedConfig` - 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 hierarchy
Since:
  4.1
See Also:
  - `isContextLoaded(org.springframework.test.context.MergedContextConfiguration)`
  - `loadContext(org.springframework.test.context.MergedContextConfiguration)`