前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring 的 @EnableCaching 注解

Spring 的 @EnableCaching 注解

作者头像
cxuan
发布2019-06-03 15:17:23
7.5K0
发布2019-06-03 15:17:23
举报
文章被收录于专栏:Java建设者Java建设者

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

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

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

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

代码语言:javascript
复制
importorg.springframework.cache.annotation.Cacheable;


public class Book{

   @Cacheable(value="simpleCache")
   public String getBook(int id){
       System.out.println("Method cached");
       if(id==1){
           return "Book 1";
      }else{
           return "Book 2";
      }
  }
}



importorg.springframework.cache.CacheManager;
importorg.springframework.cache.annotation.EnableCaching;
importorg.springframework.cache.concurrent.ConcurrentMapCache;
importorg.springframework.cache.support.SimpleCacheManager;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;

importjava.util.Arrays;

@Configuration
@EnableCaching
public class CachingConfig{

   @Bean
   public Book book(){
       return new Book();
  }

   @Bean
   public CacheManager cacheManager(){
       SimpleCacheManager simpleCacheManager = new SimpleCacheManager();
       simpleCacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("simpleCache")));
       return simpleCacheManager;
  }

}

importorg.springframework.context.annotation.AnnotationConfigApplicationContext;

public class EnableCachingAnnotationExample{

   public static void main(String[] args) {
       AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
       context.register(CachingConfig.class);
       context.refresh();

       Bookbook=context.getBean(Book.class);
       System.out.println(book.getBook(1));
       System.out.println(book.getBook(1));
       System.out.println(book.getBook(2));

       context.close();
  }
}

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

代码语言:javascript
复制
<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>

测试类

代码语言:javascript
复制
importorg.springframework.context.annotation.AnnotationConfigApplicationContext;

public class EnableCachingAnnotationExample{
   public static void main(String[] args) {
       AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); 
       ctx.register(CachingConfig.class);
       ctx.refresh();
       Bookbook 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();
  }
}

输出:

代码语言:javascript
复制
Method executed..
Book 1
Book 1
Method executed..
Book 2
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-03-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Java建设者 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档