Spring如何基于注解配置使用ehcache
作者:cuisuqiang 发布时间:2022-08-16 03:24:32
使用ehcache-spring-annotations使得在工程中简单配置即可使用缓存
下载地址:http://code.google.com/p/ehcache-spring-annotations/
需要的jar包,首先需要的是我们之前做SpringMVC时的各个Spring的jar包
然后需要把ehcache-spring-annotations-1.2.0文件夹内lib内的,非spring的jar加进去,因为我们已经增加了我们版本的spring
然后还需要 * 的cglib包
在spring主配置文件中配置ehcache注解的使用:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://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 cache-manager="ehCacheManager" />
<bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>
<bean id="sacheService" class="test.CacheService"></bean>
</beans>
配置缓存配置文件ehcache.xml,改文件放在SRC下:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir" />
<defaultCache eternal="false" maxElementsInMemory="1000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />
<cache name="testCache" eternal="false" maxElementsInMemory="100"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="300" memoryStoreEvictionPolicy="LRU" />
</ehcache>
CacheService是示例类,代码如下:
package test;
import java.util.Date;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.googlecode.ehcache.annotations.Cacheable;
import com.googlecode.ehcache.annotations.TriggersRemove;
public class CacheService{
@SuppressWarnings("deprecation")
@Cacheable(cacheName = "testCache")
public String getName(String code){
System.out.println("查询编号:" + code);
return new Date().toLocaleString() + "-->" + code;
}
@SuppressWarnings("deprecation")
@Transactional(propagation = Propagation.REQUIRED)
public String update(String code){
System.out.println("更新编号:" + code);
return new Date().toLocaleString() + "-->" + code;
}
@TriggersRemove(cacheName="testCache",removeAll=true)
public void flush(){
System.out.println("情况缓存");
System.out.println("Processing testFlushing");
}
}
改类包含根据参数获取缓存值,更新缓存,情况缓存,都是使用注解标签实现。
Action类需要改动一下,代码如下:
package test;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
// http://localhost:8080/spring/hello.do?key=1&code=java
@org.springframework.stereotype.Controller
public class HelloController{
private CacheService sacheService;
@SuppressWarnings("deprecation")
@RequestMapping("/hello.do")
public String hello(HttpServletRequest request,HttpServletResponse response){
String key = request.getParameter("key");
if("1".equals(key)){
request.setAttribute("message", sacheService.getName(request.getParameter("code")));
}else if("2".equals(key)){
request.setAttribute("message", sacheService.update(request.getParameter("code")));
}else{
sacheService.flush();
request.setAttribute("message", sacheService.getName(request.getParameter("code")));
}
return "hello";
}
public CacheService getSacheService() {
return sacheService;
}
@Autowired
public void setSacheService(CacheService sacheService) {
this.sacheService = sacheService;
}
}
根据key做不同的操作,然后分别访问以下几个路径,为了方便看效果和学习,我把工程代码放到了附件:
第一次没有缓存
http://localhost:8080/spring/hello.do?key=1&code=java
读取缓存
http://localhost:8080/spring/hello.do?key=1&code=java
更新缓存
http://localhost:8080/spring/hello.do?key=2&code=java
读取最新缓存
http://localhost:8080/spring/hello.do?key=1&code=java
情况缓存
http://localhost:8080/spring/hello.do?key=3
来源:https://www.iteye.com/blog/cuisuqiang-2050675


猜你喜欢
- 最近做了关于在Android设备上外接扫码的项目,在此记录一下关于Android USB扫码枪获取内容的问题首先我这边使用是USB HID的
- 作为最基础的引用数据类型,Java 设计者为 String 提供了字符串常量池以提高其性能,那么字符串常量池的具体原理是什么,我们带着以下三
- 一、前言用过Spring Cloud的同学都知道在使用动态配置刷新的我们要配置一个@RefreshScope 在类上才可以实现对象属性的的动
- 星期天小哼和小哈约在一起玩桌游,他们正在玩一个非常古怪的扑克游戏——“小猫钓鱼”。游戏的规则是这样的:将一副扑克牌平均分成两份,每人拿一份。
- 前言腾讯动漫app v8.1.6 工具:jadx、frida、pixel3 安卓10提示:以下是本篇文章正文内容,案例可供参考一、问题1.1
- 你好,今天我要和大家分享一些东西,举例来说这个在JavaScript中用的很多。我要讲讲回调(callbacks)。你知道什么时候用,怎么用
- 本文实例讲述了Android编程实现动态支持多语言的方法。分享给大家供大家参考,具体如下:资源文件values/strings.xml中添加
- 问题(1)条件锁是什么?(2)条件锁适用于什么场景?(3)条件锁的await()是在其它线程signal()的时候唤醒的吗?简介条件锁,是指
- 在Winform开发中中,我们为了方便客户选择,往往使用系统的字典数据选择,毕竟选择总比输入来的快捷、统一,一般我们都会简单封装一下,以便方
- 一、带时区的时间1.获取当前时间对象(带时区)import java.time.ZonedDateTime;public class dem
- 在做商城的项目中,有这么个需求,就是一个产品下有两个价格,一个是市场价,一个是销售价,这时要把市场价添加个删除线;刚开始遇到这个时,在网上找
- 1、pom.xml文件添加distributionManagement节点。模块项目中如果存在父子项目,且父子项目的jar包都需要上传到 *
- XML作为一种业界公认的数据交换格式,在各个平台与语言之上,都有广泛使用和实现。其标准型,可靠性,安全性......毋庸置疑。在androi
- 本文实例为大家分享了利用Java实现HDFS文件上传下载的具体代码,供大家参考,具体内容如下1、pom.xml配置<!--配置--&g
- 这个CardStackViewpager的灵感来自Github上面的 FlippableStackView开源项目,而我想实现的效果方向上恰
- 本文实例讲述了C#获取网页源代码的方法。分享给大家供大家参考。具体如下:public string GetPageHTML(string u
- 自C#1.0版本以来,我们要定义一个不可变数据类型的基本做法就是:先声明字段为readonly,再声明只包含get访问器的属性。例子如下:1
- 最近有个需求是这样的,人民币的符号“¥”因为安卓手机系统的不一致导致符号不是完全一样,所以用美工的给的图片代替,考虑到用的地
- 本文实例讲述了Android编程实现Toast只显示最后一条的方法。分享给大家供大家参考,具体如下:在做Android开发中,时不时的可能会
- 本篇随笔将讲解一下Android当中比较常用的两个布局容器--ScrollView和HorizontalScrollView,从字面意义上来