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

Spring實(shí)戰(zhàn)之方法級(jí)別緩存用法示例

 更新時(shí)間:2020年01月14日 09:37:26   作者:cakincqm  
這篇文章主要介紹了Spring實(shí)戰(zhàn)之方法級(jí)別緩存用法,結(jié)合實(shí)例形式分析了spring方法級(jí)別緩存配置、屬性文件、領(lǐng)域模型及相關(guān)使用技巧,需要的朋友可以參考下

本文實(shí)例講述了Spring實(shí)戰(zhàn)之方法級(jí)別緩存用法。分享給大家供大家參考,具體如下:

一 配置文件

<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:p="http://www.springframework.org/schema/p"
   xmlns:cache="http://www.springframework.org/schema/cache"
   xmlns:context="http://www.springframework.org/schema/context"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
   http://www.springframework.org/schema/cache
   http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-4.0.xsd">
   <context:component-scan
      base-package="org.crazyit.app.service" />
   <cache:annotation-driven
      cache-manager="cacheManager" />
   <!-- 配置EhCache的CacheManager 通過configLocation指定ehcache.xml文件的位置 -->
   <bean id="ehCacheManager"
      class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
      p:configLocation="classpath:ehcache.xml" p:shared="false" />
   <!-- 配置基于EhCache的緩存管理器 并將EhCache的CacheManager注入該緩存管理器Bean -->
   <bean id="cacheManager"
      class="org.springframework.cache.ehcache.EhCacheCacheManager"
      p:cacheManager-ref="ehCacheManager">
   </bean>
</beans>

二 屬性文件

<?xml version="1.0" encoding="gbk"?>
<ehcache>
  <diskStore path="java.io.tmpdir" />
   <!-- 配置默認(rèn)的緩存區(qū) -->
  <defaultCache
    maxElementsInMemory="10000"
    eternal="false"
    timeToIdleSeconds="120"
    timeToLiveSeconds="120"
    maxElementsOnDisk="10000000"
    diskExpiryThreadIntervalSeconds="120"
    memoryStoreEvictionPolicy="LRU"/>
   <!-- 配置名為users1的緩存區(qū) -->
  <cache name="users1"
    maxElementsInMemory="10000"
    eternal="false"
    overflowToDisk="true"
    timeToIdleSeconds="300"
    timeToLiveSeconds="600" />
  <cache name="users2"
    maxElementsInMemory="10000"
    eternal="false"
    overflowToDisk="true"
    timeToIdleSeconds="300"
    timeToLiveSeconds="600" />
</ehcache>

三 領(lǐng)域模型

package org.crazyit.app.domain;
public class User
{
   private String name;
   private int age;
   public User()
   {}
   public User(String name, int age)
   {
      super();
      this.name = name;
      this.age = age;
   }
   public String getName()
   {
      return name;
   }
   public void setName(String name)
   {
      this.name = name;
   }
   public int getAge()
   {
      return age;
   }
   public void setAge(int age)
   {
      this.age = age;
   }
}

四 Service

1 接口類

package org.crazyit.app.service;
import org.crazyit.app.domain.User;
public interface UserService
{
   User getUsersByNameAndAge(String name, int age);
   User getAnotherUser(String name, int age);
}

2 實(shí)現(xiàn)類

package org.crazyit.app.service.impl;
import org.crazyit.app.service.UserService;
import org.crazyit.app.domain.User;
import org.springframework.stereotype.Service;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Scope;
@Service("userService")
public class UserServiceImpl implements UserService
{
  @Cacheable(value = "users1")
  public User getUsersByNameAndAge(String name, int age)
  {
    System.out.println("--正在執(zhí)行findUsersByNameAndAge()查詢方法--");
    return new User(name, age);
  }
  @Cacheable(value = "users2")
  public User getAnotherUser(String name, int age)
  {
    System.out.println("--正在執(zhí)行findAnotherUser()查詢方法--");
    return new User(name, age);
  }
}

五 測(cè)試類

package lee;
import org.crazyit.app.service.UserService;
import org.crazyit.app.domain.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest
{
  public static void main(String[] args)
  {
    ApplicationContext ctx =
      new ClassPathXmlApplicationContext("beans.xml");
    UserService us = ctx.getBean("userService" , UserService.class);
    // 第一次調(diào)用us對(duì)象的方法時(shí)會(huì)執(zhí)行該方法,并緩存方法的結(jié)果
    User u1 = us.getUsersByNameAndAge("孫悟空", 500);
    // 由于getAnotherUser()方法使用另一個(gè)緩存區(qū),
    // 因此無法使用getUsersByNameAndAge()方法緩存區(qū)的數(shù)據(jù)。
    User u2 = us.getAnotherUser("孫悟空", 500);
    System.out.println(u1 == u2); // 輸出false
    // getAnotherUser("孫悟空", 500)已經(jīng)執(zhí)行過一次,故下面代碼使用緩存
    User u3 = us.getAnotherUser("孫悟空", 500);
    System.out.println(u2 == u3); // 輸出true
  }
}

六 測(cè)試結(jié)果

--正在執(zhí)行findUsersByNameAndAge()查詢方法--
--正在執(zhí)行findAnotherUser()查詢方法--
false
true

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 利用IDEA工具修改Maven多模塊項(xiàng)目標(biāo)識(shí)包名全過程記錄

    利用IDEA工具修改Maven多模塊項(xiàng)目標(biāo)識(shí)包名全過程記錄

    當(dāng)我們?yōu)榧追椒?wù)提供軟件開發(fā)服務(wù)時(shí),需要按照甲方的要求去修改軟件的標(biāo)識(shí),對(duì)于Maven項(xiàng)目來說就對(duì)應(yīng)著groupId,一般地寫對(duì)方公司的域名,如com.example,接下來通過本文給大家分享IDEA修改Maven多模塊項(xiàng)目標(biāo)識(shí)包名,感興趣的朋友一起看看吧
    2022-09-09
  • JAVA中簡(jiǎn)單的for循環(huán)異常踩坑

    JAVA中簡(jiǎn)單的for循環(huán)異常踩坑

    這篇文章主要為大家介紹了JAVA中簡(jiǎn)單的for循環(huán)異常踩坑避雷詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-07-07
  • SpringBoot?HikariCP連接池詳解

    SpringBoot?HikariCP連接池詳解

    這篇文章主要介紹了SpringBoot2.0?中?HikariCP?數(shù)據(jù)庫連接池原理解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 判斷List和Map是否相等并合并List中相同的Map

    判斷List和Map是否相等并合并List中相同的Map

    今天小編就為大家分享一篇關(guān)于判斷List和Map是否相等并合并List中相同的Map,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • 分析ThreadLocal內(nèi)存泄漏問題

    分析ThreadLocal內(nèi)存泄漏問題

    ThreadLocal的作用是提供線程內(nèi)的局部變量,這種變量在線程生命周期內(nèi)起作用,減少同一個(gè)線程內(nèi)多個(gè)函數(shù)或者組件之間一些公共變量傳遞的復(fù)雜度,但是如果濫用ThreadLocal可能會(huì)導(dǎo)致內(nèi)存泄漏,所以本文將為大家分析ThreadLocal內(nèi)存泄漏問題
    2023-07-07
  • java中Vector類的常用方法詳解

    java中Vector類的常用方法詳解

    這篇文章主要為大家詳細(xì)介紹了java中Vector類的常用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-02-02
  • Java8中方便又實(shí)用的Map函數(shù)總結(jié)

    Java8中方便又實(shí)用的Map函數(shù)總結(jié)

    java8之后,常用的Map接口中添加了一些非常實(shí)用的函數(shù),可以大大簡(jiǎn)化一些特定場(chǎng)景的代碼編寫,提升代碼可讀性,快跟隨小編一起來看看吧
    2022-11-11
  • 一起學(xué)JAVA基礎(chǔ)之運(yùn)算符

    一起學(xué)JAVA基礎(chǔ)之運(yùn)算符

    計(jì)算機(jī)的最基本用途之一就是執(zhí)行數(shù)學(xué)運(yùn)算,作為一門計(jì)算機(jī)語言,Java也提供了一套豐富的運(yùn)算符來操縱變量,下面這篇文章主要給大家介紹了關(guān)于JAVA基礎(chǔ)之運(yùn)算符的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Maven配置中repositories、distributionManagement、pluginRepositories用法及將已有jar包部署到私服

    Maven配置中repositories、distributionManagement、pluginRepositori

    這篇文章主要介紹了Maven配置中repositories、distributionManagement、pluginRepositories用法及將已有jar包部署到私服,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • Java基礎(chǔ)之SpringBoot整合knife4j

    Java基礎(chǔ)之SpringBoot整合knife4j

    Swagger現(xiàn)在已經(jīng)成了最流行的接口文檔生成與管理工具,但是你是否在用的時(shí)候也在吐槽,它是真的不好看,接口測(cè)試的json數(shù)據(jù)沒法格式化,測(cè)試地址如果更改了還要去改配置,接口測(cè)試時(shí)增加token驗(yàn)證是真的麻煩…針對(duì)Swagger的種種缺點(diǎn),Knife4j就呼之欲出了.需要的朋友可以參考下
    2021-05-05

最新評(píng)論

柳江县| 保康县| 西乌珠穆沁旗| 镇安县| 莎车县| 盈江县| 西昌市| 佛冈县| 新沂市| 岚皋县| 沂源县| 绥德县| 黎川县| 丘北县| 安乡县| 巨鹿县| 郑州市| 巴楚县| 旺苍县| 绍兴县| 泗水县| 大邑县| 广河县| 皋兰县| 辰溪县| 柳江县| 曲阜市| 安多县| 开平市| 内江市| 贵溪市| 霞浦县| 灵寿县| 康定县| 阳新县| 新沂市| 云阳县| 南投市| 江津市| 泽库县| 白山市|