Spring + EhcacheA while ago I wrote about method caching in Java using Ehcache Annotations for Spring. However, Spring 3.1 release, among many enhancements, brings native support for method caching with so-called cache abstraction. The main usage scenario of cache abstraction is to reduce the number of expensive executions and/or slow methods returning the same result for given parameters. The usage of Spring cache abstraction consists of two aspects: cache declaration and configuration. Spring provides many ways for declaring and configuring caching in an application: annotation-based, XML-based or mixed. In this article I will create an application that utilizes cache using Java-based approach. The example used in this article is a bit modified project taken from the original article.

Create a new web project

To create a project we use spring-mvc-quickstart Maven2 archetype that can be found here: spring-mvc-quickstart-archetype. After installing the archetype in your local repository we run the following command to create new Spring 3.1 MVC project:

Once the project is created it may be imported to Eclipse/STS using “Import Existing Maven Projects”.

Ehcache dependency

Spring cache abstraction currently supports two cache storages: JDK ConcurrentMap-based Cache and Ehcache-based Cache that will be used in this example. In order to use Ehcache-based Cache storage in our application we need to add appropriate dependency on Ehcache in POM file:

Cache configuration

Our application is configured via Spring @Configuration classes and the cache configuration will be configured the same way thanks to @EnableCaching annotation that enables Spring annotation-driven cache management capability to the application.

To finish the configuration we need to create ehcache.xml file that configures Ehcache. The file should be in the classpath (according to the above configuration), so we place it in src/main/resources:

CachingConfig class may be now imported by our WebMvcConfig class:

Cache declaration

Now it’s time to add caching capabilities to the application. We declare caching on MessageRepository by setting the following rules:

  • use cache with name messageCache for caching messages for invocations of findByRecipient(String)
  • use cache with name messagesCache for caching messages for invocations of findAll()
  • remove all elements from both caches after the invocation of createMessage(Message)

To declare method cacheable we use the org.springframework.cache.annotation.Cacheable annotation – method or type level annotation indicating the method or multiple methods can be cached. To invalidate the cache we use org.springframework.cache.annotation.CacheEvict annotation:

Note that both caches used in the above example appeared ehcache.xml file shown in previous section. To quickly see the result of the method caching we may create a @Controller that calls our repository methods:

On application startup we may observe that Ehcache manager is actually created and also cachable methods are created:

Please note that the method will be cached using default key – meaning that the method parameter is considered as a key and default cache condition – meaning that the method is always cached. Consult API docs for more details regarding these attributes. When calling the method for the first time we may observe in logs that the method was actually executed (hibernate.show_sql property is set to true):

Another call will not execute the method, instead its result will be returned from cache. And that’s it!


Spring cache abstraction brings to developers the possibility of method caching in applications with a minimal code changes and simple configuration to control the cache behavior. Comparing to already mentioned Ehcache Annotations for Spring it has one big advantage in my opinion: Spring provides an abstraction, which makes it extensible to support more cache managers in future or to provide your own ones.


Development Manager and Java Developer at Aspire Systems Poland. Open source enthusiast, team leader, teacher, blogger and Twitter user @kolorobot