Interface ContextCache

All Known Implementing Classes:
DefaultContextCache

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, potentially configured with a maximum size, pause mode, and 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:
  • Field Details

    • CONTEXT_CACHE_LOGGING_CATEGORY

      static final String CONTEXT_CACHE_LOGGING_CATEGORY
      The name of the logging category used for reporting ContextCache statistics.
      See Also:
    • DEFAULT_MAX_CONTEXT_CACHE_SIZE

      static final int DEFAULT_MAX_CONTEXT_CACHE_SIZE
      The default maximum size of the context cache: 32.
      Since:
      4.3
      See Also:
    • MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME

      static final String MAX_CONTEXT_CACHE_SIZE_PROPERTY_NAME
      System property used to configure the maximum size of the ContextCache as a positive integer: "spring.test.context.cache.maxSize".

      May alternatively be configured via the SpringProperties mechanism.

      Note that implementations of ContextCache are not required to actually support a maximum cache size. Consult the documentation of the corresponding implementation for details.

      Since:
      4.3
      See Also:
    • CONTEXT_CACHE_PAUSE_PROPERTY_NAME

      static final String CONTEXT_CACHE_PAUSE_PROPERTY_NAME
      System property used to configure whether inactive application contexts stored in the ContextCache should be paused: "spring.test.context.cache.pause".

      Defaults to on_context_switch. Can be set to always or never to disable pausing of inactive application contexts — for example:

      -Dspring.test.context.cache.pause=never

      May alternatively be configured via the SpringProperties mechanism.

      Note that implementations of ContextCache are not required to support context pausing. Consult the documentation of the corresponding implementation for details.

      Since:
      7.0.3
      See Also:
  • Method Details