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>
目录
相关文章
|
算法 Java 测试技术
SpringBoot@Profile详解
SpringBoot@Profile详解
737 0
|
前端开发 JavaScript 开发者
别再只用普通函数了!箭头函数的四大神奇区别,让你的代码飞起来!
【8月更文挑战第23天】在Web前端开发中,JavaScript的箭头函数(引入于ES6)提供了一种比传统函数更加简洁的定义方法。箭头函数使用 &quot;=&gt;&quot; 替代 &quot;function&quot; 关键字,并且自动绑定外部 &quot;this&quot; 上下文,避免了传统函数中 &quot;this&quot; 值因调用方式不同而变化的问题。此外,箭头函数不拥有自己的 &quot;arguments&quot; 对象,但可以通过剩余参数语法获取所有参数。需要注意的是,箭头函数不能作为构造函数使用。理解这些差异有助于开发者编写更高效、清晰的代码。
582 0
|
SQL 关系型数据库 MySQL
mysql数据库——连接查询(内连接:自然连接,等值连接。外连接:左连接,右连接,全连接)
mysql数据库——连接查询(内连接:自然连接,等值连接。外连接:左连接,右连接,全连接)
2566 0
mysql数据库——连接查询(内连接:自然连接,等值连接。外连接:左连接,右连接,全连接)
|
自然语言处理
箭头函数和普通函数的区别是什么?
箭头函数和普通函数的区别是什么?
951 63
|
XML Java 数据安全/隐私保护
Spring Aop该如何使用
本文介绍了AOP(面向切面编程)的基本概念和术语,并通过具体业务场景演示了如何在Spring框架中使用Spring AOP。文章详细解释了切面、连接点、通知、切点等关键术语,并提供了完整的示例代码,帮助读者轻松理解和应用Spring AOP。
341 2
Spring Aop该如何使用
|
小程序 容器
微信小程序常用组件的简单使用 view,scroll-view,swiper,swiper-item,text,rich-text,button,image
本文介绍了微信小程序中常用组件的使用方法,包括view、scroll-view、swiper与swiper-item、text与rich-text、button以及image组件。详细解释了各组件的功能、属性以及如何在小程序页面中进行使用。
微信小程序常用组件的简单使用 view,scroll-view,swiper,swiper-item,text,rich-text,button,image
|
SQL 缓存 关系型数据库
MySQL Limit实现原理
本文深入解析了MySQL中`LIMIT`子句的实现原理及其在分页、性能优化等场景下的应用技巧。文章详细介绍了`LIMIT`的基本语法、MySQL内部处理流程,以及如何通过索引优化、覆盖索引等策略提升分页查询的性能,并提供了实践建议。
840 3
设计模式-工厂模式 Factory Pattern(简单工厂、工厂方法、抽象工厂)
这篇文章详细解释了工厂模式,包括简单工厂、工厂方法和抽象工厂三种类型。每种模式都通过代码示例展示了其应用场景和实现方法,并比较了它们之间的差异。简单工厂模式通过一个工厂类来创建各种产品;工厂方法模式通过定义一个创建对象的接口,由子类决定实例化哪个类;抽象工厂模式提供一个创建相关或依赖对象家族的接口,而不需要明确指定具体类。
设计模式-工厂模式 Factory Pattern(简单工厂、工厂方法、抽象工厂)
|
Java Maven
使用javadoc生成maven项目的文档
本文介绍了如何在Maven项目中使用maven-javadoc-plugin插件来生成项目的JavaDoc文档,并展示了配置插件、编写代码、运行Maven命令以及查看生成文档的完整步骤。
487 0
使用javadoc生成maven项目的文档
|
XML 安全 Java
Spring高手之路19——Spring AOP注解指南
在本文中,我们将深入探索Spring AOP(面向切面编程)的核心概念及其在现代Spring应用中的实际应用。从基本的注解使用到复杂的切面配置,本文将一步步指导你如何利用Spring AOP提升代码的模块化,帮助你在Spring开发路上更进一步。
349 3
Spring高手之路19——Spring AOP注解指南