spring本身内置了对Cache的支持,本次记录的是基于Java API的ConcurrentMap的CacheManager配置。

1、xml文件中增加命名空间

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,有一个cache-manager属性用来指定当前所使用的CacheManager对应的bean的名称,默认是cacheManager -->
  <cache:annotation-driven/>
</beans>
 

<cache:annotation-driven/>有一个cache-manager属性用来指定当前所使用的CacheManager对应的bean的名称,默认是cacheManager

2、配置CacheManager

CacheManager是Spring定义的一个用来管理Cache的接口。Spring自身已经为我们提供了两种CacheManager的实现。一种是基于Java API的ConcurrentMap,另一种是基于第三方Cache实现--EhCache.。

基于ConcurrentMap的配置

<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean">
<!--xxxx对应@Cacheable中的value值-->
<property name="name" value="xxxx"/>
</bean>
</set>
</property>
</bean>
 

完成以上步骤后,可进行测试下。

第一次调用时,走其中的方法,第二次不走其中方法,直接从缓存中抽取。

最新文章

  1. 数据库热备份工具innobackupex的安装
  2. Handler机制来处理子线程去更新UI线程控件
  3. 【mysql5.6】数据类型
  4. Mac显示和隐藏文件的命令
  5. 转】Mahout分步式程序开发 基于物品的协同过滤ItemCF
  6. Hibernate 二级缓存 总结整理(转)
  7. As3 里的正则相关
  8. HDU 1394 Minimum Inversion Number (线段树 单点更新 求逆序数)
  9. kendo ui 单击取消编辑数据grid减少的原因和治疗方法的数据
  10. linux里的那么点东西(持续更新)
  11. 【openstack N版】——摘除一个计算节点
  12. 文件描述符与FILE
  13. RABBITMQ too many heartbeats missed
  14. ASP.NET Web API 2 之文件下载
  15. centos 7 添加中文输入法
  16. Python基础(函数部分)-day04
  17. PAT 乙级 1069 微博转发抽奖(20) C++版
  18. jq给单选框 radio添加或删除选中状态
  19. HDU 4532 湫秋系列故事——安排座位 (组合+DP)
  20. Dubbo原码解析(version:2.5.3)

热门文章

  1. 深入浅出 java.String
  2. -bash: fultter: command not found
  3. 负载均衡配置篇(Nginx)
  4. Python—使用列表构造栈数据结构
  5. Shell语法 【if while for】
  6. Acunetix WVS安全测试软件使用教程(入门级)
  7. 实现迭代器(\_\_next\_\_和\_\_iter\_\_)
  8. BTree
  9. jquery预加载的几种例子
  10. 函数(Python)