欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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文件夾內(nèi)lib內(nèi)的,非spring的jar加進去,因為我們已經(jīng)增加了我們版本的spring
然后還需要動態(tài)代理的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"); 
  } 
} 

改類包含根據(jù)參數(shù)獲取緩存值,更新緩存,情況緩存,都是使用注解標簽實現(xiàn)。

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; 
  } 
} 

根據(jù)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

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • JAVA十大排序算法之歸并排序詳解

    JAVA十大排序算法之歸并排序詳解

    這篇文章主要介紹了java中的歸并排序,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-08-08
  • Spring cloud 查詢返回廣告創(chuàng)意實例代碼

    Spring cloud 查詢返回廣告創(chuàng)意實例代碼

    在本篇文章里小編給大家整理的是關(guān)于Spring cloud 查詢返回廣告創(chuàng)意實例代碼,需要的朋友們可以跟著學習下。
    2019-08-08
  • SpringSecurity集成第三方登錄過程詳解(最新推薦)

    SpringSecurity集成第三方登錄過程詳解(最新推薦)

    在ThirdAuthenticationFilter 類的attemptAuthentication()方法中,我們通過authType類型,然后創(chuàng)建對應的Authentication實現(xiàn)來實現(xiàn)不同方式的登錄,下面給大家分享SpringSecurity集成第三方登錄過程,感興趣的朋友一起看看吧
    2024-05-05
  • spring集成okhttp3的步驟詳解

    spring集成okhttp3的步驟詳解

    okhttp是一個封裝URL,比HttpClient更友好易用的工具,下面這篇文章主要給大家介紹了關(guān)于spring集成okhttp3的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧。
    2018-04-04
  • spring?IOC控制反轉(zhuǎn)原理詳解

    spring?IOC控制反轉(zhuǎn)原理詳解

    這篇文章主要為大家詳細介紹了spring?IOC控制反轉(zhuǎn)原理,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-03-03
  • SpringBoot如何實現(xiàn)word文檔轉(zhuǎn)pdf

    SpringBoot如何實現(xiàn)word文檔轉(zhuǎn)pdf

    這篇文章主要介紹了SpringBoot如何實現(xiàn)word文檔轉(zhuǎn)pdf,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • Maven 倉庫國內(nèi)鏡像源收藏(小結(jié))

    Maven 倉庫國內(nèi)鏡像源收藏(小結(jié))

    這篇文章主要介紹了Maven 倉庫國內(nèi)鏡像源收藏(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • 淺談Java finally語句到底是在return之前還是之后執(zhí)行(必看篇)

    淺談Java finally語句到底是在return之前還是之后執(zhí)行(必看篇)

    下面小編就為大家?guī)硪黄獪\談Java finally語句到底是在return之前還是之后執(zhí)行(必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • SpringCloud重試機制配置詳解

    SpringCloud重試機制配置詳解

    本篇文章主要介紹了SpringCloud重試機制配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Spring Boot分段處理List集合多線程批量插入數(shù)據(jù)的解決方案

    Spring Boot分段處理List集合多線程批量插入數(shù)據(jù)的解決方案

    大數(shù)據(jù)量的List集合,需要把List集合中的數(shù)據(jù)批量插入數(shù)據(jù)庫中,本文給大家介紹Spring Boot分段處理List集合多線程批量插入數(shù)據(jù)的解決方案,感興趣的朋友跟隨小編一起看看吧
    2024-04-04

最新評論