Spring3.2使用Ehcache注解
来源:互联网 发布:淘宝代购mk包是真的吗 编辑:程序博客网 时间:2024/05/23 15:35
项目组使用的框架是Spring3.2+hibernate4,老师让我研究一下Spring的缓存机制。在网上找了下,发现Ehcache使用的最多,个人也不太喜欢写太多配置文件,就决定采用注解的方式实现缓存。本文算是个人的一点经验总结,闲话不多说,进入正题吧。
一.配置Spring缓存
首先要做的是在Spring的主配置文件中加入缓存的配置信息,beans节点的属性如下所示:
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" 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.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.2.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd ">其中,cache和tx是实现注解缓存所必须的。主配置文件中关于缓存的配置信息如下所示:
<!-- 开启注解缓存 --><cache:annotation-driven /><!-- cacheManager工厂类,指定ehcache.xml的位置 --><bean id="cacheManagerFactory"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation" value="classpath:ehcache.xml"></property><property name="shared" value="true"></property></bean><!-- 声明cacheManager --><bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"><property name="cacheManager" ref="cacheManagerFactory"></property></bean>这里要注意两个问题,一个是EhCacheManagerFactoryBean的shared属性要显示的设定为true,否则会一直报错,本人就在这个问题上纠结了很久;另一个是classpath,这个路径指向的是src的子目录,本人曾尝试把ehcache.xml放在WEB-INF下,但一直没有成功,最后只好放在src的子目录下了。
二.配置ehcache.xml文件
spring配置文件里只是配置了一些代理信息,具体的缓存还是要在ehcache.xml中配置。ehcache.xml如下所示:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="ehcache.xsd"> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="100" eternal="true" overflowToDisk="true" diskPersistent="false" diskExpiryThreadIntervalSeconds="120"/> <cache name="domainCache" maxElementsInMemory="100" eternal="true" overflowToDisk="true" /> <cache name="entityCache" maxElementsInMemory="100" eternal="true" overflowToDisk="true" /> </ehcache>
三.在代码中使用注解
配置信息整理完之后就可以在代码中使用注解了,如下所示:
@Cacheable(value ="domainCache",key="#id")public MetaDicDomain getById(String id) {MetaDic dic = metaDicDao.get(id);MetaDicDomain dicDomain= new MetaDicDomain(dic);return dicDomain;}
PS:需要ehcache的依赖包
这个比较坑,如果少了ehcache的依赖包即使配置好了所有的文件也是没法使用缓存的。。。我这里使用的是ehcache-2.7.5.jar,没有出现问题。
0 0
- Spring3.2使用Ehcache注解
- spring3.2+ehcache 注解使用
- EhCache注解使用
- 使用spring3.2 @Scheduled注解 定时任务
- EHcache注解使用(ehcache-spring-annotations)
- EHcache注解使用(ehcache-spring-annotations)
- Spring3.x--使用@Autowired注解
- Spring3事务管理--使用@Transactional 注解
- Spring 使用注解配置使用ehcache
- 使用ehcache-spring-annotations开启ehcache的注解功能
- 使用ehcache-spring-annotations开启ehcache的注解功能
- spring3.0注解(2)
- Spring3.2新注解@ControllerAdvice
- Spring3.2新注解@ControllerAdvice
- Spring3.2新注解@ControllerAdvice
- springMVC--spring3.X注解(2)
- 在Spring中快速使用EHCache注解
- 在Spring中快速使用EHCache注解
- 金山腾讯组件xp守护者
- 你知道哪几种茶对电脑一族有益健康吗
- javaBean使用BeanUtils操作(高新技术学习)
- C++ vector示例
- 初识Node.js
- Spring3.2使用Ehcache注解
- Building MFC application with /MD[d] (CRT dll version)requires MFC shared dll version~~~~
- 2014年2月27日 Matlab 相关图像处理函数学习
- Java中ArrayList和LinkedList区别
- 男子改造硬盘,骗英特尔百万
- 浙大ACM 3455 Shizuka's Letter
- discuz论坛登录慢,访问页面正常
- Ubuntu 中软件的安装、卸载以及查看的方法总结
- ARM裸机开发-bootloader—点亮LED灯