@EnableCaching
annotation is the annotation-driven cache management feature in the spring framework. This annotation has been added to the spring since the version 3.1. If you are using this annotation, then you are not required to write the XML bean definitions for cache manager.
When you annotate your configuration class with @EnableCaching
annotation, this triggers a post processor that would scan every Spring bean for the presence of caching annotations on public methods. If such an annotation is found, a proxy is automatically created to intercept the method call and handle the caching behavior accordingly.
If you are interested in reading more about the caching, please read my tutorial on spring caching. This tutorial helps you to understand the use of @EnableCaching
annotation in spring. If you have any questions, please write it in the comments section.
Here is a simple example that demonstrate the use of @EnableCaching
functionality. In the below code, I am trying to cache a method in the Book
class.
@EnableCaching Example
Book.java
import org.springframework.cache.annotation.Cacheable; public class Book { @Cacheable(value = { "sampleCache" }) public String getBook(int id) { System.out.println("Method executed.."); if (id == 1) { return "Book 1"; } else { return "Book 2"; } } }
CachingConfig.java
import java.util.Arrays; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.concurrent.ConcurrentMapCache; import org.springframework.cache.support.SimpleCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @EnableCaching public class CachingConfig { @Bean public Book book() { return new Book(); } @Bean public CacheManager cacheManager() { SimpleCacheManager cacheManager = new SimpleCacheManager(); cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("sampleCache"))); return cacheManager; } }
The above Java Configuration is equivalent of writing the following XML configurations in your spring configuration file:
<beans> <cache:annotation-driven/> <bean id="book" class="Book"/> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"> <property name="name" value="sampleCache"/> </bean> </set> </property> </bean> </beans>
EnableCachingAnnotationExample.java
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class EnableCachingAnnotationExample { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(CachingConfig.class); ctx.refresh(); Book book = ctx.getBean(Book.class); // calling getBook method first time. System.out.println(book.getBook(1)); // calling getBook method second time. This time, method will not // execute. System.out.println(book.getBook(1)); // calling getBook method third time with different value. System.out.println(book.getBook(2)); ctx.close(); } }
If you run the above program, you will get the following output:
Method executed.. Book 1 Book 1 Method executed.. Book 2
@EnableCaching Parameters
Most of the time you have to use the @EnableCaching
annotation without any parameters. But, this annotation provides three parameters:
- mode – This parameter indicates how caching advice should be applied.
- order – This parameter specifies the ordering of the execution of the caching advisor when multiple advices are applied at a specific joinpoint (Also Read : AOP in Spring.
- proxyTargetClass – This parameter indicates whether subclass-based (CGLIB) proxies are to be created as opposed to standard Java interface-based proxies.