国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:11日期:2023-08-04 15:56:19

使用ehcache-spring-annotations使得在工程中簡單配置即可使用緩存

下載地址:http://code.google.com/p/ehcache-spring-annotations/

需要的jar包,首先需要的是我們之前做SpringMVC時(shí)的各個(gè)Spring的jar包

然后需要把ehcache-spring-annotations-1.2.0文件夾內(nèi)lib內(nèi)的,非spring的jar加進(jìn)去,因?yàn)槲覀円呀?jīng)增加了我們版本的spring然后還需要?jiǎng)討B(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/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/contexthttp://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 class='org.springframework.cache.ehcache.EhCacheManagerFactoryBean'> <property name='configLocation' value='classpath:ehcache.xml'/> </bean> <bean 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('查詢編號(hào):' + code); return new Date().toLocaleString() + '-->' + code; } @SuppressWarnings('deprecation') @Transactional(propagation = Propagation.REQUIRED) public String update(String code){ System.out.println('更新編號(hào):' + 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)簽實(shí)現(xiàn)。

Action類需要改動(dòng)一下,代碼如下:

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做不同的操作,然后分別訪問以下幾個(gè)路徑,為了方便看效果和學(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í)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 久久亚洲精品国产精品777777 | 欧美一级日本一级韩国一级 | 亚洲精品国产福利一区二区三区 | 日韩一区二区在线观看 | 怡红院亚洲红怡院天堂麻豆 | 国产毛片精品 | 日本一区二区高清不卡 | 9191精品国产费久久 | 国产一级在线观看视频 | 在线观看亚洲精品国产 | 欧美日韩一区二区三区在线播放 | 美女亚洲综合 | 精品在线播放视频 | 高清成人爽a毛片免费网站 高清大学生毛片一级 | 欧美黑人性xxx猛交 欧美很黄视频在线观看 | 亚洲天堂免费 | 飘花国产午夜精品不卡 | 国产成人精品三级 | 欧美午夜三级我不卡在线观看 | 国产三香港三韩国三级不卡 | 国产成人99久久亚洲综合精品 | 乱码在线中文字幕加勒比 | 日韩特黄特色大片免费视频 | 爱啪网亚洲第一福利网站 | 另类在线| 国产精品自拍第一页 | 国产一级特黄一级毛片 | 国产成人经典三级在线观看 | 九九免费视频 | 久久91在线 | 欧美亚洲日本一区二区三区浪人 | 那里有黄色网址 | 久青草国产97香蕉在线视频xx | 亚洲高清在线播放 | 大香伊蕉国产短视频69 | 在线久 | 国产微拍精品福利视频 | 欧美日韩视频二区三区 | 国产精品亚洲第一区二区三区 | 毛片网站免费在线观看 | 美女视频黄a |