最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2020年10月31日 10:01:08   作者:cuisuqiang  
這篇文章主要介紹了Spring如何基于注解配置使用ehcache,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下

使用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ìn)去,因為我們已經(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ù)獲取緩存值,更新緩存,情況緩存,都是使用注解標(biāo)簽實現(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做不同的操作,然后分別訪問以下幾個路徑,為了方便看效果和學(xué)習(xí),我把工程代碼放到了附件:

第一次沒有緩存
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決 IDEA 創(chuàng)建 Gradle 項目沒有src目錄問題

    解決 IDEA 創(chuàng)建 Gradle 項目沒有src目錄問題

    這篇文章主要介紹了解決 IDEA 創(chuàng)建 Gradle 項目沒有src目錄問題,本文圖文并茂給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-06-06
  • Spring Security自定義登錄原理及實現(xiàn)詳解

    Spring Security自定義登錄原理及實現(xiàn)詳解

    這篇文章主要介紹了Spring Security自定義登錄原理及實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • Java中的HashSet集合存儲數(shù)據(jù)的結(jié)構(gòu)詳解

    Java中的HashSet集合存儲數(shù)據(jù)的結(jié)構(gòu)詳解

    這篇文章主要介紹了Java中的HashSet集合存儲數(shù)據(jù)的結(jié)構(gòu)詳解,數(shù)組結(jié)構(gòu)他把元素進(jìn)行分組,相同哈希值的元素是一組,鏈表/紅黑樹結(jié)構(gòu)把相同哈希值的元素鏈接到一起,存儲數(shù)據(jù)到集合中,先計算元素的哈希值,需要的朋友可以參考下
    2023-09-09
  • IDEA導(dǎo)出jar打包成exe應(yīng)用程序的小結(jié)

    IDEA導(dǎo)出jar打包成exe應(yīng)用程序的小結(jié)

    這篇文章主要介紹了IDEA導(dǎo)出jar打包成exe應(yīng)用程序,需要的朋友可以參考下
    2020-08-08
  • SpringBoot+MyBatisPlus對Map中Date格式轉(zhuǎn)換處理的方法詳解

    SpringBoot+MyBatisPlus對Map中Date格式轉(zhuǎn)換處理的方法詳解

    在?SpringBoot?項目中,?如何統(tǒng)一?JSON?格式化中的日期格式。本文將為大家介紹一種方法:利用MyBatisPlus實現(xiàn)對Map中Date格式轉(zhuǎn)換處理,需要的可以參考一下
    2022-10-10
  • Java中使用異或運算符實現(xiàn)加密字符串

    Java中使用異或運算符實現(xiàn)加密字符串

    這篇文章主要介紹了Java中使用異或運算符實現(xiàn)加密字符串,本文直接給出實現(xiàn)代碼,以及運算結(jié)果加密實例,需要的朋友可以參考下
    2015-06-06
  • idea神級插件及如何安裝Bito插件【Bito-ChatGPT】

    idea神級插件及如何安裝Bito插件【Bito-ChatGPT】

    這篇文章主要介紹了介紹一款idea神級插件【Bito-ChatGPT】,Bito插件的強(qiáng)大之處在于它可以幫助開發(fā)人員更快地提交代碼,同時還提供了一些有用的功能,如自動補(bǔ)全提交信息、快速查看歷史記錄等,需要的朋友可以參考下
    2023-04-04
  • SpringBoot 并發(fā)登錄人數(shù)控制的實現(xiàn)方法

    SpringBoot 并發(fā)登錄人數(shù)控制的實現(xiàn)方法

    這篇文章主要介紹了SpringBoot 并發(fā)登錄人數(shù)控制的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Mybatis中特殊SQL的執(zhí)行的實現(xiàn)示例

    Mybatis中特殊SQL的執(zhí)行的實現(xiàn)示例

    本文主要介紹了Mybatis中特殊SQL的執(zhí)行的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 手把手教你實現(xiàn)Java第三方應(yīng)用登錄

    手把手教你實現(xiàn)Java第三方應(yīng)用登錄

    本文主要介紹了手把手教你實現(xiàn)Java第三方應(yīng)用登錄,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08

最新評論

密山市| 富裕县| 大关县| 历史| 略阳县| 浦东新区| 江安县| 平凉市| 金山区| 客服| 陈巴尔虎旗| 桂林市| 皋兰县| 庆阳市| 莒南县| 慈溪市| 克什克腾旗| 水城县| 渝中区| 扎囊县| 信阳市| 龙口市| 大连市| 喀喇| 康乐县| 广安市| 吉林市| 财经| 泌阳县| 宜兴市| 全州县| 凤山市| 湄潭县| 蓬莱市| 潼南县| 沿河| 象州县| 辽阳县| 宁阳县| 九龙城区| 天峨县|