spring结合ehcache-spring-annotations配置缓存
为什么80%的码农都做不了架构师?>>>
网上关于spring结合ehcache做缓存的方案比较多,依据spring的版本大致可以分成两种情况,在spring3.1提供中提供了自己的cache方案;3.1以下的版本,需要结合ehcache等缓存框架来实现。话说回来,这里利用spring3.0.5RELEASE版本和ehcache-spring-annotations,实现简单的缓存配置(虽然网上给出了很多解决方案,如果不动手,不记录,也只能等到用得时候才研究了)。
spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!-- beans中注意添加ehcache-spring的命名空间和ehcache-spring -->
<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:oxm="http://www.springframework.org/schema/oxm" xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd"><!-- 自动扫描注解 --><ehcache:annotation-driven /><!-- 缓存配置,如需要可以另加配置 --><ehcache:config cache-manager="cacheManager"><ehcache:evict-expired-elementsinterval="60" /></ehcache:config><!-- 缓存管理器,指定ehcache的配置文件路径 --><bean id="cacheManager"class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"><property name="configLocation" value="classpath:ehcache.xml" /></bean></beans>
ehcache.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"><defaultCache eternal="true" maxElementsInMemory="100" overflowToDisk="false" /><cache name="messageCache" maxElementsInMemory="10" eternal="true" overflowToDisk="false" /><cache name="messagesCache" maxElementsInMemory="10" eternal="true" overflowToDisk="false" />
</ehcache>
使用注解
@Cacheable(cacheName = "messagesCache")public Collection<Message> findAllMessages() {Collection<Message> values = messages.values();Set<Message> messages = new HashSet<Message>();synchronized (messages) {Iterator<Message> iterator = values.iterator();while (iterator.hasNext()) {messages.add(iterator.next());}}LOG.debug("== got all messages, size={}", messages.size());if(storageDelegate != null)storageDelegate.findAllMessages();return Collections.unmodifiableCollection(messages);}@TriggersRemove(cacheName = "messagesCache", when = When.AFTER_METHOD_INVOCATION, removeAll = true)public void addMessage(Message message) {long id = newID.incrementAndGet();message.setId(id);messages.put(id, message);LOG.debug("== added a message with id={}", id);if(storageDelegate != null)storageDelegate.addMessage(message);}
jar依赖
<dependency><groupId>com.googlecode.ehcache-spring-annotations</groupId><artifactId>ehcache-spring-annotations</artifactId><version>1.1.2</version>
</dependency>
注:这里用得是maven的方式,给出了依赖配置。
关于ehcache-spring-annotations完整版的例子,可以参看http://blog.goyello.com/2010/07/29/quick-start-with-ehcache-annotations-for-spring/,写得已经很详细了,不太清楚的地方可以把他的源码拿下来看看,注意他的例子里面用了spring-mvc。
转载于:https://my.oschina.net/psuyun/blog/161127
spring结合ehcache-spring-annotations配置缓存相关推荐
- spring+ehcache实现页面整体缓存和页面局部缓存
第一步:首先配置ehcache.xml指定我们的SimplePageCachingFilter缓存 ,这里指定页面缓存的生命周期是60秒,还有timeToIdleSeconds的时间爱你是120秒, ...
- 缓存插件 Spring支持EHCache缓存
Spring仅仅是提供了对缓存的支持,但它并没有任何的缓存功能的实现,spring使用的是第三方的缓存框架来实现缓存的功能.其中,spring对EHCache提供了很好的支持. 在介绍Spring的缓 ...
- 转Spring+Hibernate+EHcache配置(二)
Spring AOP+EHCache简单缓存系统解决方案 需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系 ...
- SpringMVC +Spring + MyBatis + Mysql + Redis(作为二级缓存) 配置
转载:http://blog.csdn.net/xiadi934/article/details/50786293 项目环境: 在SpringMVC +Spring + MyBatis + MySQL ...
- Spring Caching配置缓存过期时间
文章目录 一.Spring Cache是什么? 二.使用步骤 1.开启基于注解的缓存 2.配置缓存 三.解决方案 方案一:通过编写config设置缓存相关项 方案二:通过配置文件 一.Spring C ...
- Spring指南之使用Spring缓存数据(Spring Framework官方文档之缓存抽象详解)
1.请参见官方文档Spring指南之使用 Spring 缓存数据 2.请参见Spring官方文档之缓存抽象 3.参见github代码 文章目录 一.简介 二.你将创造什么(What You Will ...
- ehcache + spring 整合以及配置说明 ,附带整合问题 (已解决)
新做的项目,因为流量不大 就是一个征信平台,高峰流量不多,但缓存是必须的,cache到server上就可以,不需要额外的memcache.redis之类的东西. 但是遇到一个大坑,事情是这样的: 通过 ...
- 介绍 Spring 3.1 M1 中的缓存功能
介绍 Spring 3.1 M1 中的缓存功能- 中文版 (转) Spring 3.1 提供了对已有的 Spring 应用增加缓存的支持,这个特性对应用本身来说是透明的,通过缓存抽象层,使得对已有代码 ...
- Spring,ehcache整合报错
摘要:在做Spring整合ehcache配置的时候出现了下面的错误,提示如下:java.lang.ClassNotFoundException: org.springframework.cache.e ...
最新文章
- 通过yiic来创建yii应用
- Service的生命周期
- 一篇文章,了解清楚路由器的各种组网
- 【渝粤教育】电大中专新媒体营销实务 (3)作业 题库
- mysql5.6与mysql5.5不同
- elementUi Dialog 对话框使用中数据获取问题
- 机票分享第一篇 机票由何而来
- linux grep 非_帮助非技术人员转向Linux的8条技巧
- js ---- 对象去重
- 高斯消元法(Gauss Elimination) 分析 题解 模板——czyuan原创
- 重磅丨三年沉淀 2018全球人工智能技术大会蓄势待发
- TCP/IP 报文格式(IP数据包、TCP报头、UDP报头)
- 2017第25届春季中西部(重庆)医疗器械展览会会刊(参展商名录)
- ubuntu清空回收站命令
- 快学 Go 语言 第 3 课 —— 分支与循环
- OpenCV实践之路——opencv玩数独之一九宫格轮廓提取与透视变换
- 基于C++的带权无向图的实现 (三)- Prim最小生成树算法
- 用vs20008生成MFC项目,测试wince
- 语音识别发展史与入门书籍简介
- 计算机专业考研方向及院校排名(转)
热门文章
- Web应用部署在WebLogic中Basic认证无法运行问题及解决
- [Ext JS6] Grid 某些行不允许删除和选择的实现
- [Extjs 4] 类系统
- vue打包放到Java项目里_【vue】webpack打包vue项目并且运行在Tomcat里面
- as 从java_从Java调用AS400 RPG
- android获取服务器时间格式,Android 获取服务器与客户端时差的实例代码
- 郑州大学c语言课程设计2000行,C语言课程设计大作业(1).pptx
- LCA(最近公共祖先)
- Git flow常用命令
- 微信小程序 ---- 学习目标认识小程序