The cache of application contexts becomes too big when running a complete suite of tests that each load a full ApplicationContext with a JPA EntityManagerFactory. There are situations where a tiny difference in the configuration forces us to load a different context.
Since the context cache is only there for performance reasons, its maximum size should be configurable – a system property might be the best choice, while no property means unbounded size – so that the least recently used can be properly closed and evicted from the cache.
- Introduce a mechanism for configuring the maximum number of contexts that should be cached at any given time.
- Possible mechanisms include: simple system property or a system property supported via SpringProperties.
- Redesign DefaultContextCache to implement an LRU cache eviction algorithm.
- Ensure that evicted contexts get properly closed.
- Consider internally delegating to a third-party implementation such as a Guava cache.