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>
目录
相关文章
|
存储 XML JSON
【面试题精讲】Protobuf
【面试题精讲】Protobuf
|
边缘计算 算法 安全
CDN百科第五讲 | CDN和游戏加速器有什么区别?
很多懂IT的游戏玩家都会将CDN和游戏加速器混淆,实际上从效果上看,CDN和网游加速器都具备让网络访问变快的能力,可以帮助玩家游戏的体验和访问效率提升,但是在它们在原理上是有本质区别的,本期CDN百科为你解答。
3337 0
CDN百科第五讲 | CDN和游戏加速器有什么区别?
|
Windows
Mac 技术篇-Windows Remote Desktop远程连接windows系统时键盘输入字母自动变为快捷键操作问题解决方法
Mac 技术篇-Windows Remote Desktop远程连接windows系统时键盘输入字母自动变为快捷键操作问题解决方法
1784 0
Mac 技术篇-Windows Remote Desktop远程连接windows系统时键盘输入字母自动变为快捷键操作问题解决方法
|
数据采集 监控 数据可视化
BI工具在数据分析和业务洞察中的应用
BI工具在数据分析和业务洞察中的应用
337 11
|
传感器 编解码 运维
示例SysML设计“罗卜”快跑自动驾驶
【10月更文挑战第6天】本文介绍了“罗卜”自动驾驶汽车系统的完整设计,使用SysML的Internal Block Diagram (IBD) 描述了系统的主要子系统及其内部结构和交互。通过定义块、部分属性、端口、接口和连接器,IBD图详细展示了感知系统、控制系统、导航系统和动力系统之间的数据传输和交互。文章分析了IBD图的优点,包括清晰定义系统结构、统一接口和交互、提高系统设计的可理解性和可维护性,并讨论了其在系统集成和测试中的应用。同时,也指出了IBD图的局限性,如复杂性管理困难、动态行为表示不足和学习曲线陡峭等问题。
536 4
|
Rust 前端开发 iOS开发
Tauri 开发实践— Tauri 工程搭建
本文首发于微信公众号“前端徐徐”,介绍了在 macOS 环境下使用 Rust 和 Tauri 构建跨平台桌面应用的过程。首先需安装 Rust 及系统依赖,参考链接:[Rust 入门](https://www.rust-lang.org/zh-CN/learn/get-started) 和 [Tauri 前置条件](https://tauri.app/zh-cn/v1/guides/getting-started/prerequisites)。
446 0
Tauri 开发实践— Tauri 工程搭建
|
缓存 搜索推荐 调度
什么样的CDN才算是一个好的CDN
CDN,即内容分发网络,通过分布于各地的服务器节点加速静态资源的加载,如图片、视频及网页文件等。好的CDN应具备泛解析、自定义HTTPS、隐藏源IP、缓存加速、SEO优化、Gzip压缩、智能调度、全球加速及防盗链等功能,确保高效、安全、稳定的用户体验。
320 0
|
SQL 缓存 架构师
一文梳理 Code Review 方法论与实践总结
作为卓越工程文化的一部分,Code Review 其实一直在进行中,只是各团队根据自身情况张驰有度,松紧可能也不一,这里简单梳理一下 CR 的方法和团队实践。
865 93
一文梳理 Code Review 方法论与实践总结
|
设计模式 Java 开发者
谈谈springboot的工厂模式
【4月更文挑战第13天】Spring Boot中的工厂模式是一种用于解耦组件创建过程的设计模式,它允许系统在运行时根据需要动态地创建不同类型的对象。这种模式在Spring框架中得到了广泛的应用,特别是在依赖注入(DI)和控制反转(IoC)的上下文中,它有助于管理复杂的依赖关系并提高代码的可维护性和可扩展性
537 7
|
调度
【RT-Thread】学习日记之系统节拍Tick
【RT-Thread】学习日记之系统节拍Tick
209 0