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>
目录
相关文章
|
2月前
|
Java 开发者 Spring
【SpringBoot 异步魔法】@Async 注解:揭秘 SpringBoot 中异步方法的终极奥秘!
【8月更文挑战第25天】异步编程对于提升软件应用的性能至关重要,尤其是在高并发环境下。Spring Boot 通过 `@Async` 注解简化了异步方法的实现。本文详细介绍了 `@Async` 的基本用法及配置步骤,并提供了示例代码展示如何在 Spring Boot 项目中创建与管理异步任务,包括自定义线程池、使用 `CompletableFuture` 处理结果及异常情况,帮助开发者更好地理解和运用这一关键特性。
125 1
|
2月前
|
XML Java 测试技术
Spring5入门到实战------17、Spring5新功能 --Nullable注解和函数式注册对象。整合JUnit5单元测试框架
这篇文章介绍了Spring5框架的三个新特性:支持@Nullable注解以明确方法返回、参数和属性值可以为空;引入函数式风格的GenericApplicationContext进行对象注册和管理;以及如何整合JUnit5进行单元测试,同时讨论了JUnit4与JUnit5的整合方法,并提出了关于配置文件加载的疑问。
Spring5入门到实战------17、Spring5新功能 --Nullable注解和函数式注册对象。整合JUnit5单元测试框架
|
2月前
|
缓存 Java 数据库连接
Spring Boot奇迹时刻:@PostConstruct注解如何成为应用初始化的关键先生?
【8月更文挑战第29天】作为一名Java开发工程师,我一直对Spring Boot的便捷性和灵活性着迷。本文将深入探讨@PostConstruct注解在Spring Boot中的应用场景,展示其在资源加载、数据初始化及第三方库初始化等方面的作用。
52 0
|
6天前
|
Java Spring 容器
Spring使用异步注解@Async正确姿势
Spring使用异步注解@Async正确姿势,异步任务,spring boot
|
6天前
|
XML Java 数据格式
spring复习03,注解配置管理bean
Spring框架中使用注解配置管理bean的方法,包括常用注解的标识组件、扫描组件、基于注解的自动装配以及使用注解后的注意事项,并提供了一个基于注解自动装配的完整示例。
spring复习03,注解配置管理bean
|
6天前
|
XML 前端开发 Java
控制spring框架注解介绍
控制spring框架注解介绍
|
19天前
|
Java 数据库连接 数据格式
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
IOC/DI配置管理DruidDataSource和properties、核心容器的创建、获取bean的方式、spring注解开发、注解开发管理第三方bean、Spring整合Mybatis和Junit
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
|
2月前
|
Java 数据安全/隐私保护 Spring
揭秘Spring Boot自定义注解的魔法:三个实用场景让你的代码更加优雅高效
揭秘Spring Boot自定义注解的魔法:三个实用场景让你的代码更加优雅高效
|
2月前
|
XML Java 数据库
Spring5入门到实战------15、事务操作---概念--场景---声明式事务管理---事务参数--注解方式---xml方式
这篇文章是Spring5框架的实战教程,详细介绍了事务的概念、ACID特性、事务操作的场景,并通过实际的银行转账示例,演示了Spring框架中声明式事务管理的实现,包括使用注解和XML配置两种方式,以及如何配置事务参数来控制事务的行为。
Spring5入门到实战------15、事务操作---概念--场景---声明式事务管理---事务参数--注解方式---xml方式
|
2月前
|
XML 数据库 数据格式
Spring5入门到实战------14、完全注解开发形式 ----JdbcTemplate操作数据库(增删改查、批量增删改)。具体代码+讲解 【终结篇】
这篇文章是Spring5框架的实战教程的终结篇,介绍了如何使用注解而非XML配置文件来实现JdbcTemplate的数据库操作,包括增删改查和批量操作,通过创建配置类来注入数据库连接池和JdbcTemplate对象,并展示了完全注解开发形式的项目结构和代码实现。
Spring5入门到实战------14、完全注解开发形式 ----JdbcTemplate操作数据库(增删改查、批量增删改)。具体代码+讲解 【终结篇】
下一篇
无影云桌面