Spring @EnableCaching Annotation Example
November 23, 2021
On this page we will learn Spring @EnableCaching
annotation.
1. The
@EnableCaching
enables annotation-driven cache management capability.
2. The
<cache:annotation-driven/>
XML namespace is equivalent to @EnableCaching
annotation.
3. The
@EnableCaching
is used with @Configuration
classes.
4. The
@EnableCaching
invokes @CacheResult
, @CachePut
, @CacheRemove
, @CacheRemoveAll
annotations.
5. The
@EnableCaching
annotation is introduced in Spring 3.1.
6. The
@EnableCaching
annotation has following attributes.
a. mode : Value can be
AdviceMode.ASPECTJ
or AdviceMode.PROXY
. Default is AdviceMode.PROXY
.
b. order : Indicate the ordering of the execution of the caching advisor when multiple advices are applied at a specific joinpoint. The values can be
HIGHEST_PRECEDENCE
or LOWEST_PRECEDENCE
. Default is Ordered.LOWEST_PRECEDENCE
.
c. proxyTargetClass : By default value is false and standard Java interface-based proxies are created. When the value is true, CGLIB proxies are created.
7. The
@EnableCaching
is used as following.
@Configuration @EnableCaching public class EnableCachingConfig { @Bean public CacheManager cacheManager() { SimpleCacheManager cacheManager = new SimpleCacheManager(); cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("test"))); return cacheManager; } ------ }
Complete Example
build.gradleapply plugin: 'java' apply plugin: 'eclipse' archivesBaseName = 'SpringCache' version = '0.0.1-SNAPSHOT' repositories { mavenCentral() } dependencies { compile 'org.springframework.boot:spring-boot-starter:2.5.5' }
package com.concretepage; import org.springframework.cache.annotation.Cacheable; public class Employee { @Cacheable(value = { "test" }) public String getEmployee(int empId) { System.out.println("execute method.."); if (empId == 1) { return "Ram"; } else { return "Sudhir"; } } }
package com.concretepage; 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 EnableCachingConfig { @Bean public Employee employee() { return new Employee(); } @Bean public CacheManager cacheManager() { SimpleCacheManager cacheManager = new SimpleCacheManager(); cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("test"))); return cacheManager; } }
package com.concretepage; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class EnableCachingTest { public static void main(String[] args) { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(EnableCachingConfig.class); ctx.refresh(); Employee employee = ctx.getBean(Employee.class); // calling getEmployee method first time. System.out.println(employee.getEmployee(1)); // calling getEmployee method second time. This time, method will not // execute. System.out.println(employee.getEmployee(1)); // calling getEmployee method third time with different value. System.out.println(employee.getEmployee(2)); } }
execute method.. Ram Ram execute method.. Sudhir