温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring基于注解配置使用ehcache

发布时间:2020-11-02 15:19:16 来源:亿速云 阅读:232 作者:Leah 栏目:开发技术

本篇文章为大家展示了Spring基于注解配置使用ehcache,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

使用ehcache-spring-annotations使得在工程中简单配置即可使用缓存

需要的jar包,首先需要的是我们之前做SpringMVC时的各个Spring的jar包

然后需要把ehcache-spring-annotations-1.2.0文件夹内lib内的,非spring的jar加进去,因为我们已经增加了我们版本的spring
然后还需要动态代理的cglib包

在spring主配置文件中配置ehcache注解的使用:

<&#63;xml version="1.0" encoding="UTF-8"&#63;> 
<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下:

<&#63;xml version="1.0" encoding="UTF-8"&#63;> 
<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&#63;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&#63;key=1&code=java
读取缓存
http://localhost:8080/spring/hello.do&#63;key=1&code=java
更新缓存
http://localhost:8080/spring/hello.do&#63;key=2&code=java
读取最新缓存
http://localhost:8080/spring/hello.do&#63;key=1&code=java
情况缓存
http://localhost:8080/spring/hello.do&#63;key=3

上述内容就是Spring基于注解配置使用ehcache,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI