【译】Spring的@EnableCaching注解


Spring的@EnableCaching注解

@EnableCaching注解是spring framework中的注解驱动的缓存管理功能。自spring版本3.1起加入了该注解。如果你使用了这个注解,那么你就不需要在XML文件中配置cache manager了。

当你在配置类(@Configuration)上使用@EnableCaching注解时,会触发一个post processor,这会扫描每一个spring bean,查看是否已经存在注解对应的缓存。如果找到了,就会自动创建一个代理拦截方法调用,使用缓存的bean执行处理。

如果你对缓存感兴趣并想了解更多,请阅读spring caching. 本文会帮助你了解如何使用@EnableCaching注解。

接下来的例子演示了@EnableCaching的用法。在代码中,我缓存了Book类找那个的方法。

代码

  1.   //Book.java
  2.   import org.springframework.cache.annotation.Cacheable;
  3.   public class Book {
  4.   @Cacheable(value = { "sampleCache" })
  5.   public String getBook(int id) {
  6.   System.out.println("Method executed..");
  7.   if (id == 1) {
  8.   return "Book 1";
  9.   } else {
  10.   return "Book 2";
  11.   }
  12.   }
  13.   }
  14.    
  15.   //CachingConfig.java
  16.   import java.util.Arrays;
  17.   import org.springframework.cache.CacheManager;
  18.   import org.springframework.cache.annotation.EnableCaching;
  19.   import org.springframework.cache.concurrent.ConcurrentMapCache;
  20.   import org.springframework.cache.support.SimpleCacheManager;
  21.   import org.springframework.context.annotation.Bean;
  22.   import org.springframework.context.annotation.Configuration;
  23.    
  24.   @Configuration
  25.   @EnableCaching
  26.   public class CachingConfig {
  27.   @Bean
  28.   public Book book() {
  29.   return new Book();
  30.   }
  31.    
  32.   @Bean
  33.   public CacheManager cacheManager() {
  34.   SimpleCacheManager cacheManager = new SimpleCacheManager();
  35.   cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("sampleCache")));
  36.   return cacheManager;
  37.   }
  38.   }

上面的java config和下面的xml配置文件是等效的:

  1.   <beans>
  2.   <cache:annotation-driven/>
  3.   <bean id="book" class="Book"/>
  4.   <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
  5.   <property name="caches">
  6.   <set>
  7.   <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
  8.   <property name="name" value="sampleCache"/>
  9.   bean>
  10.   set>
  11.   property>
  12.   bean>
  13.   beans>
  1.   //EnableCachingAnnotationExample.java
  2.    
  3.   import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4.    
  5.   public class EnableCachingAnnotationExample {
  6.   public static void main(String[] args) {
  7.   AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
  8.   ctx.register(CachingConfig.class);
  9.   ctx.refresh();
  10.   Book book = ctx.getBean(Book.class);
  11.   // calling getBook method first time.
  12.   System.out.println(book.getBook(1));
  13.   // calling getBook method second time. This time, method will not
  14.   // execute.
  15.   System.out.println(book.getBook(1));
  16.   // calling getBook method third time with different value.
  17.   System.out.println(book.getBook(2));
  18.   ctx.close();
  19.   }
  20.   }

会得到如下的输出

  1.   Method executed..
  2.   Book 1
  3.   Book 1
  4.   Method executed..
  5.   Book 2