Spring如何基于注解配置使用ehcache

 更新时间:2020年10月31日 10:01:08   作者:cuisuqiang  
这篇文章主要介绍了Spring如何基于注解配置使用ehcache,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

使用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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java 反射机制

    Java 反射机制

    这篇文章简要的说明了Java的反射机制,Java的反射是框架设计的灵魂,本文通过例子能看的更加清晰的理解
    2021-06-06
  • Spring Batch轻量级批处理框架实战

    Spring Batch轻量级批处理框架实战

    本文主要介绍了Spring Batch轻量级批处理框架实战,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-10-10
  • Java OpenCV图像处理之仿射变换,透视变换,旋转详解

    Java OpenCV图像处理之仿射变换,透视变换,旋转详解

    这篇文章主要为大家详细介绍了Java OpenCV图像处理中仿射变换,透视变换,旋转的实现,文中的示例代码讲解详细,快跟随小编一起学习一下
    2022-10-10
  • 关于Nacos配置管理的统一配置管理、自动刷新详解

    关于Nacos配置管理的统一配置管理、自动刷新详解

    这篇文章主要介绍了关于Nacos配置管理的统一配置管理、自动刷新详解,Nacos是阿里的一个开源产品,是针对微服务架构中的服务发现、配置管理、服务治理的综合型解决方案,需要的朋友可以参考下
    2023-05-05
  • Java 中synchronize函数的实例详解

    Java 中synchronize函数的实例详解

    这篇文章主要介绍了Java 中synchronize函数的实例详解的相关资料,希望通过本文能帮助到大家理解使用synchronize函数的使用方法,需要的朋友可以参考下
    2017-09-09
  • Java StringUtils字符串分割转数组的实现

    Java StringUtils字符串分割转数组的实现

    这篇文章主要介绍了Java StringUtils字符串分割转数组的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • 在Java中使用下划线分隔数的字面值的用法讲解

    在Java中使用下划线分隔数的字面值的用法讲解

    这篇文章主要介绍了在Java中使用下划线分隔数字的字面值的用法讲解,这是Java7以后加入的新特性,需要的朋友可以参考下
    2016-03-03
  • 没有编辑器的环境下是如何创建Servlet(Tomcat+Java)项目的?

    没有编辑器的环境下是如何创建Servlet(Tomcat+Java)项目的?

    今天给大家带来的是关于Java的相关知识,文章围绕着在没有编辑器的环境下如何创建Servlet(Tomcat+Java)项目展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • 关于@ResponseBody 默认输出的误区的解答

    关于@ResponseBody 默认输出的误区的解答

    这篇文章主要介绍了关于@ResponseBody 默认输出的误区的解答,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • java对象拷贝常见面试题及应答汇总

    java对象拷贝常见面试题及应答汇总

    在本篇文章里小编给大家整理的是关于java对象拷贝常见面试题的相关内容,需要的朋友们可以学习下。
    2020-02-02

最新评论