Spring如何基于注解配置使用ehcache
程序员文章站
2022-07-06 15:57:50
使用ehcache-spring-annotations使得在工程中简单配置即可使用缓存下载地址:需要的jar包,首先需要的是我们之前做springmvc时的各个spring的jar包然后需要把ehc...
使用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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
spring如何使用命名空间p简化bean的配置
-
三、解决Spring MVC拦截器导致静态资源访问失败(基于java注解配置)
-
Sping MVC不使用任何注解处理(jQuery)Ajax请求(基于XML配置)
-
springboot如何使用logback-spring配置日志格式,并分环境配置
-
Spring5参考指南:基于注解的容器配置
-
spring之aop基于注解开发配置
-
使用 Spring 2.5 基于注解驱动的 Spring MVC
-
spring boot中关于获取配置文件注解的使用@ConfigurationProperties、@Value、@PropertySource
-
Spring如何基于注解显式实现自动装配
-
Spring如何基于注解配置使用ehcache