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>
目录
相关文章
|
5月前
|
缓存 NoSQL Java
Spring 注解玩大全
Spring 注解玩大全
38 2
|
3月前
|
XML Java 数据格式
Spring的注解
Spring框架注解简化了Java应用开发,通过元数据替代XML配置。`@Component`、`@Controller`、`@Service`、`@Repository`都是组件注解,用于标记不同层的类,如`@Controller`用于控制层,`@Service`业务层,`@Repository`数据访问层。它们均会被组件扫描加入IOC容器,`@Component`是通用形式。通过`@ComponentScan`可以配置扫描规则,包括排除和包含特定类型的组件。`@Autowired`自动装配依赖,`@Value`能从属性文件读取值注入字段。`
|
4月前
|
缓存 JSON Java
探索Spring最常用的30个注解
探索Spring最常用的30个注解
19 0
|
Java 数据格式 Spring
一文全面深入了解Spring中的@Value注解
一文全面深入了解Spring中的@Value注解
1585 0
一文全面深入了解Spring中的@Value注解
|
11月前
|
XML JSON Java
spring 常用注解汇总
spring 常用注解汇总
|
Java Spring
Spring 的常见注解有哪些?
Spring 的常见注解有哪些?
46 0
|
XML JSON 前端开发
Spring常用注解你知道有哪些吗?
Spring常用注解你知道有哪些吗?
86 0
|
XML JSON 算法
spring常用注解(全)
Spring的一个核心功能是IOC,就是将Bean初始化加载到容器中,Bean是如何加载到容器的,可以使用Spring注解方式或者Spring XML配置方式。 Spring注解方式减少了配置文件内容,更加便于管理,并且使用注解可以大大提高了开发效率! 注解本身是没有功能的,和xml一样,注解和xml都是一种元数据,元数据即解释数据的数据,也就是所谓的配置。
351 0
|
Java Spring 容器
Spring中@Value注解详解
Spring中@Value注解详解
|
XML Java 数据格式