Spring 的 @EnableCaching 注解

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

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


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


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

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


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配置文件是等效的:



<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>


测试类


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();
}
}

输出:

Method executed..
Book 1
Book 1
Method executed..
Book 2


            </div>
目录
相关文章
|
边缘计算 算法 安全
CDN百科第五讲 | CDN和游戏加速器有什么区别?
很多懂IT的游戏玩家都会将CDN和游戏加速器混淆,实际上从效果上看,CDN和网游加速器都具备让网络访问变快的能力,可以帮助玩家游戏的体验和访问效率提升,但是在它们在原理上是有本质区别的,本期CDN百科为你解答。
3384 0
CDN百科第五讲 | CDN和游戏加速器有什么区别?
|
传感器 编解码 运维
示例SysML设计“罗卜”快跑自动驾驶
【10月更文挑战第6天】本文介绍了“罗卜”自动驾驶汽车系统的完整设计,使用SysML的Internal Block Diagram (IBD) 描述了系统的主要子系统及其内部结构和交互。通过定义块、部分属性、端口、接口和连接器,IBD图详细展示了感知系统、控制系统、导航系统和动力系统之间的数据传输和交互。文章分析了IBD图的优点,包括清晰定义系统结构、统一接口和交互、提高系统设计的可理解性和可维护性,并讨论了其在系统集成和测试中的应用。同时,也指出了IBD图的局限性,如复杂性管理困难、动态行为表示不足和学习曲线陡峭等问题。
585 4
|
缓存 5G 开发者
【提效】docker镜像构建优化-提速10倍
本文主要记录了自己通过查阅相关资料,一步步排查问题,最后通过优化Docerfile文件将docker镜像构建从十几分钟降低到1分钟左右,效率提高了10倍左右。
1044 122
|
存储 缓存 监控
Linux缓存管理:如何安全地清理系统缓存
在Linux系统中,内存管理至关重要。本文详细介绍了如何安全地清理系统缓存,特别是通过使用`/proc/sys/vm/drop_caches`接口。内容包括清理缓存的原因、步骤、注意事项和最佳实践,帮助你在必要时优化系统性能。
1168 78
|
机器学习/深度学习 人工智能 算法
AI在医疗健康领域的应用
随着人工智能技术的不断发展,其在医疗健康领域的应用也日益广泛。从辅助诊断、个性化治疗方案的制定,到疾病预防和健康管理,AI技术都在发挥着重要作用。本文将探讨AI在医疗健康领域的应用,包括其在医学影像分析、基因编辑、药物研发等方面的应用,以及其对医疗行业未来发展的影响。
港澳台居民大陆居住证号码正则表达式
港澳台居民大陆居住证号码正则表达式
2763 0
|
机器学习/深度学习 存储 并行计算
【Pytorch神经网络理论篇】 27 图神经网络DGL库:简介+安装+卸载+数据集+PYG库+NetWorkx库
DGL库是由纽约大学和亚马逊联手推出的图神经网络框架,支持对异构图的处理,开源相关异构图神经网络的代码,在GCMC、RGCN等业内知名的模型实现上也取得了很好的效果。
2665 0
|
Python
Python 用sympy做高数题,不定积分、定积分、极限、求导样样精通!
Python 用sympy做高数题,不定积分、定积分、极限、求导样样精通!
594 0
|
自然语言处理 搜索推荐
jieba分词有哪些模式,分别详细介绍
jieba分词有哪些模式,分别详细介绍
1964 0