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
猜你喜欢
- springboot项目启动,访问报404错误今天在做一个springboot项目的时候,是接着别人的项目写的,写完之后想做一下测试,于是就
- SpringCloudStream配置以下配置摘自《SpringCloud微服务实战》,配置主要包括两大部分:Stream配置(基础配置、通
- 现在就为大家介绍一种基于因子分解的RSA算法,这种加密算法有两种实现形式:1、公钥加密,私钥解密;2、私钥加密,公钥解密。下面就为大家分析一
- 了解过spring-Boot这个技术的,应该知道Spring-Boot的核心配置文件application.properties,当然也可以
- Spring Data Jpa复杂查询总结只是做一个总结所以就不多说废话了实体类@Entity@Table(name = "t_h
- Mybatis typeAlias配置1.定义别名<typeAliases> <ty
- 在前面的文章<Mybatis配置之<properties>属性配置元素详述>,我们讲述了<properties
- 用法一:常量在JDK1.5之前,我们定义常量都是:publicstaticfianl....。现在好了,有了枚举,可以把相关的常量分组到一个
- 一、ArrayList类概述什么是集合:提供一种存储空间可变的存储模型,存储的数据容量可以发生改变ArrayList集合的特点:底层是数组实
- 1.创建一个线程Java操作线程最核心的类就是Thread类创建线程有很多方法,下面我们写一个Mythread类继承 Thread 类重写r
- 一、结构型模式结构型模式有什么好处?从程序的结构上实现松耦合,从而可以扩大整体的类结构,用来解决更大的问题二、适配器模式USB网线转换器三、
- Hibernate是一个彻底的ORM(Object Relational Mapping,对象关系映射)开源框架。我们先看一下官方文档所给出
- 一、分步骤集成1.1 整合连接池hikariCP介绍:HikariCP 是一个高性能的 JDBC 连接池组件,可以避免连接频繁建立、关闭的开
- 本文研究的主要是Java回调函数与观察者模式的实现,具体介绍和实现代码如下。观察者模式(有时又被称为发布(publish )-订阅(Subs
- IOS与网页JS交互随着移动APP的快速迭代开发趋势,越来越多的APP中嵌入了html网页,但在一些大中型APP中,尤其是电商类
- 平时用到的库仓库名地址备注mavenCentralhttps://repo1.maven.org/maven2/
- 一、什么是ASMASM是一个java字节码操纵框架,它能被用来动态生成类或者增强既有类的功能。ASM 可以直接产生二进制 class 文件,
- Android超清晰6.0权限申请AndPermission的具体实现代码,供大家参考,具体内容如下前言这是我经常使用的框架,原因:1.思路
- 工具方法:本文的目的是把json串转成map键值对存储,而且只存储叶节点的数据maven 引用jar包版本:<dependency&g
- 如图所示的效果相信大家都不陌生,我们可以使用很多种方法去实现此效果,这里自己采用CountDownTimer定时器简单封装下此效果,方便我们