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>
目录
相关文章
|
4天前
|
存储 关系型数据库 分布式数据库
PostgreSQL 18 发布,快来 PolarDB 尝鲜!
PostgreSQL 18 发布,PolarDB for PostgreSQL 全面兼容。新版本支持异步I/O、UUIDv7、虚拟生成列、逻辑复制增强及OAuth认证,显著提升性能与安全。PolarDB-PG 18 支持存算分离架构,融合海量弹性存储与极致计算性能,搭配丰富插件生态,为企业提供高效、稳定、灵活的云数据库解决方案,助力企业数字化转型如虎添翼!
|
15天前
|
弹性计算 关系型数据库 微服务
基于 Docker 与 Kubernetes(K3s)的微服务:阿里云生产环境扩容实践
在微服务架构中,如何实现“稳定扩容”与“成本可控”是企业面临的核心挑战。本文结合 Python FastAPI 微服务实战,详解如何基于阿里云基础设施,利用 Docker 封装服务、K3s 实现容器编排,构建生产级微服务架构。内容涵盖容器构建、集群部署、自动扩缩容、可观测性等关键环节,适配阿里云资源特性与服务生态,助力企业打造低成本、高可靠、易扩展的微服务解决方案。
1313 5
|
2天前
|
监控 JavaScript Java
基于大模型技术的反欺诈知识问答系统
随着互联网与金融科技发展,网络欺诈频发,构建高效反欺诈平台成为迫切需求。本文基于Java、Vue.js、Spring Boot与MySQL技术,设计实现集欺诈识别、宣传教育、用户互动于一体的反欺诈系统,提升公众防范意识,助力企业合规与用户权益保护。
|
14天前
|
机器学习/深度学习 人工智能 前端开发
通义DeepResearch全面开源!同步分享可落地的高阶Agent构建方法论
通义研究团队开源发布通义 DeepResearch —— 首个在性能上可与 OpenAI DeepResearch 相媲美、并在多项权威基准测试中取得领先表现的全开源 Web Agent。
1357 87
|
2天前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
4天前
|
弹性计算 安全 数据安全/隐私保护
2025年阿里云域名备案流程(新手图文详细流程)
本文图文详解阿里云账号注册、服务器租赁、域名购买及备案全流程,涵盖企业实名认证、信息模板创建、域名备案提交与管局审核等关键步骤,助您快速完成网站上线前的准备工作。
194 82
2025年阿里云域名备案流程(新手图文详细流程)