Spring 3.1:緩存和EhCache

如果在網上查找使用Spring 3.1內置緩存的示例,那么通常會碰到Spring的SimpleCacheManager ,Spring的家伙說這對“用于測試或簡單的緩存聲明很有用”。 實際上,我更喜歡將SimpleCacheManager看作是輕量級的,而不是簡單的。 在您希望每個JVM占用少量內存緩存的情況下很有用。 如果Spring的家伙正在經營一家超市,那么SimpleCacheManager將屬于他們自己品牌的“基本”產品范圍。

另一方面,如果您需要一個可擴展,持久和分布式的重型緩存,則Spring還附帶內置的ehCache包裝器。

好消息是,Spring的緩存實現之間的交換很容易。 從理論上講,這完全是配置問題,為了證明該理論正確,我從Caching和@Cacheable博客中獲取了示例代碼,并使用EhCache實現對其進行了運行。

配置步驟與我上一篇博客“ 緩存和配置”中描述的步驟相似,您仍然需要指定:

<cache:annotation-driven />

…在您的Spring配置文件中以打開緩存。 您還需要定義一個id為cacheManager的bean,只是這一次您引用Spring的EhCacheCacheManager類而不是SimpleCacheManager

<bean id='cacheManager'class='org.springframework.cache.ehcache.EhCacheCacheManager'p:cacheManager-ref='ehcache'/>

上面的示例演示了EhCacheCacheManager配置。 注意,它引用了另一個ID為“ ehcache ”的bean。 配置如下:

<bean id='ehcache' class='org.springframework.cache.ehcache.EhCacheManagerFactoryBean'p:configLocation='ehcache.xml'p:shared='true'/>

ehcache ”具有兩個屬性: configLocationshared
configLocation ”是一個可選屬性,用于指定ehcache配置文件的位置。 在測試代??碼中,我使用了以下示例文件:

<?xml version='1.0' encoding='UTF-8'?>
<ehcache xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='http://ehcache.org/ehcache.xsd'><defaultCache eternal='true' maxElementsInMemory='100' overflowToDisk='false' /><cache name='employee' maxElementsInMemory='10000' eternal='true' overflowToDisk='false' />
</ehcache>

…這將創建兩個緩存:一個默認緩存和一個名為“員工”的緩存。

如果缺少此文件,則EhCacheManagerFactoryBean只需選擇一個默認的ehcache配置文件: ehcache-failsafe.xml ,該文件位于ehcache的ehcache-core jar文件中。

另一個EhCacheManagerFactoryBean屬性是' shared '。 這被認為是可選的,因為文檔指出它定義了“ EHCache CacheManager是應該共享(作為VM級別的單例)還是獨立的(通常在應用程序內部)。 默認值為'false',創建一個獨立的實例。” 但是,如果將其設置為false,則將收到以下異常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0': Cannot resolve reference to bean 'cacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in class path resource [ehcache-example.xml]: Cannot resolve reference to bean 'ehcache' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehcache' defined in class path resource [ehcache-example.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: InputStreamConfigurationSource [stream=java.io.BufferedInputStream@424c414]at org.springframework.beans.factory.support.BeanDefinitionValueResolver.
resolveReference(BeanDefinitionValueResolver.java:328)at org.springframework.beans.factory.support.BeanDefinitionValueResolver.
resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
populateBean(AbstractAutowireCapableBeanFactory.java:1118)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
doCreateBean(AbstractAutowireCapableBeanFactory.java:517)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
createBean(AbstractAutowireCapableBeanFactory.java:456)
... stack trace shortened for clarityat org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.
java:683)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.
run(RemoteTestRunner.java:390)at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.
main(RemoteTestRunner.java:197)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in class path resource [ehcache-example.xml]: Cannot resolve reference to bean 'ehcache' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehcache' defined in class path resource [ehcache-example.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: InputStreamConfigurationSource [stream=java.io.BufferedInputStream@424c414]at org.springframework.beans.factory.support.BeanDefinitionValueResolver.
resolveReference(BeanDefinitionValueResolver.java:328)at org.springframework.beans.factory.support.BeanDefinitionValueResolver.
resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
... stack trace shortened for clarityat org.springframework.beans.factory.support.AbstractBeanFactory.
getBean(AbstractBeanFactory.java:193)at org.springframework.beans.factory.support.BeanDefinitionValueResolver.
resolveReference(BeanDefinitionValueResolver.java:322)... 38 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ehcache' defined in class path resource [ehcache-example.xml]: Invocation of init method failed; nested exception is net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: InputStreamConfigurationSource [stream=java.io.BufferedInputStream@424c414]at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
initializeBean(AbstractAutowireCapableBeanFactory.java:1455)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
doCreateBean(AbstractAutowireCapableBeanFactory.java:519)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
createBean(AbstractAutowireCapableBeanFactory.java:456)at org.springframework.beans.factory.support.AbstractBeanFactory$1
.getObject(AbstractBeanFactory.java:294)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.
getSingleton(DefaultSingletonBeanRegistry.java:225)at org.springframework.beans.factory.support.AbstractBeanFactory.
doGetBean(AbstractBeanFactory.java:291)at org.springframework.beans.factory.support.AbstractBeanFactory.
getBean(AbstractBeanFactory.java:193)at org.springframework.beans.factory.support.BeanDefinitionValueResolver.
resolveReference(BeanDefinitionValueResolver.java:322)... 48 more
Caused by: net.sf.ehcache.CacheException: Another unnamed CacheManager already exists in the same VM. Please provide unique names for each CacheManager in the config or do one of following:
1. Use one of the CacheManager.create() static factory methods to reuse same CacheManager with same name or create one if necessary
2. Shutdown the earlier cacheManager before creating new one with same name.
The source of the existing CacheManager is: InputStreamConfigurationSource [stream=java.io.BufferedInputStream@424c414]at net.sf.ehcache.CacheManager.assertNoCacheManagerExistsWithSameName(CacheManager.
java:521)at net.sf.ehcache.CacheManager.init(CacheManager.java:371)at net.sf.ehcache.CacheManager.(CacheManager.java:339)at org.springframework.cache.ehcache.EhCacheManagerFactoryBean.
afterPropertiesSet(EhCacheManagerFactoryBean.java:104)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.
initializeBean(AbstractAutowireCapableBeanFactory.java:1452)... 55 more

…當您嘗試運行一系列單元測試時。

我認為這歸結為Spring的ehcache管理器工廠的一個簡單錯誤,因為它試圖使用new()創建多個緩存實例,而不是使用“其中一種CacheManager.create()靜態工廠方法”作為例外,允許其重用相同名稱的相同CacheManager。 因此,我的第一個JUnit測試工作正常,但其他所有測試均失敗。

令人反感的代碼行是:

this.cacheManager = (this.shared ? CacheManager.create() : new CacheManager());

為了完整性,下面列出了我的完整XML配置文件:

<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns='http://www.springframework.org/schema/beans' xmlns:p='http://www.springframework.org/schema/p'xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd'><!-- Switch on the Caching --><cache:annotation-driven /><!-- Do the component scan path --><context:component-scan base-package='caching' /><bean id='cacheManager' class='org.springframework.cache.ehcache.EhCacheCacheManager' p:cacheManager-ref='ehcache'/><bean id='ehcache' class='org.springframework.cache.ehcache.EhCacheManagerFactoryBean' p:configLocation='ehcache.xml'  p:shared='true'/> 
</beans>

在使用ehcache時,要考慮的唯一其他配置詳細信息是Maven依賴項。 這些非常簡單,因為Ehcache的專家們將所有各種ehcache jar組合到一個Maven POM模塊中。 可以使用下面的XML將該POM模塊添加到項目的POM文件中:

<dependency><groupId>net.sf.ehcache</groupId><artifactId>ehcache</artifactId><version>2.6.0</version><type>pom</type><scope>test</scope></dependency>

最后,可以從Maven Central和Sourceforge存儲庫中獲得ehcache Jar文件:

<repositories><repository><id>sourceforge</id><url>http://oss.sonatype.org/content/groups/sourceforge/</url><releases><enabled>true</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories>

祝您編程愉快,別忘了分享!

參考: Spring 3.1:來自Captain Debug's Blog博客的JCG合作伙伴 Roger Hughes的緩存和EhCache 。


翻譯自: https://www.javacodegeeks.com/2012/10/spring-31-caching-and-ehcache.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/371310.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/371310.shtml
英文地址,請注明出處:http://en.pswp.cn/news/371310.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

mysql-表完整性約束

閱讀目錄 一 介紹二 not null與default三 unique四 primary key五 auto_increment六 foreign key七 總結一 介紹 回到頂部 約束條件與數據類型的寬度一樣&#xff0c;都是可選參數 作用&#xff1a;用于保證數據的完整性和一致性主要分為&#xff1a; PRIMARY KEY (PK) 標識…

可消費消息數量_17 個方面,綜合對比 主流消息隊列

一、資料文檔二、開發語言三、支持的協議四、消息存儲五、消息事務六、負載均衡七、集群方式八、管理界面九、可用性十、消息重復十一、吞吐量TPS十二、訂閱形式和消息分發十三、順序消息十四、消息確認十五、消息回溯十六、消息重試十七、并發度本文將從&#xff0c;Kafka、Ra…

opencv2.4.13+python2.7學習筆記--使用 knn對手寫數字OCR

閱讀對象&#xff1a;熟悉knn、了解opencv和python。 1.knn理論介紹&#xff1a;算法學習筆記&#xff1a;knn理論介紹 2. opencv中knn函數 路徑&#xff1a;opencv\sources\modules\ml\include\opencv2\ml\ml.hpp 3.案例 3.1數據集介紹 我們的目的是創建一個可以對手寫數字進行…

如何遠程管理Quartz

選項1&#xff1a;JMX 許多人問他們是否可以通過JMX管理Quartz&#xff0c;但我不確定為什么Quartz doc甚至不會提及它。 是的&#xff0c;您可以使用quartz.properties的以下命令啟用石英中的JMX org.quartz.scheduler.jmx.export true之后&#xff0c;您可以使用標準的JMX客…

熱啟動必須聯網嗎_供暖結束,地暖是關閉供水閥門還是關閉回水閥門?你做對了嗎?...

天氣漸漸暖和起來很多城市都停止供暖了一些家庭也停止使用地暖那么今天就來聊一聊&#xff0c;停止供暖后地暖系統應該怎么保養地暖不用時候是關閉供水閥門還是關閉回水閥門&#xff1f;供暖結束 暖氣閥門到底要不要關一般來說&#xff0c;我們供暖期結束是不用關閉總閥門的。因…

python學習(九) 網絡編程學習--簡易網站服務器

python 網絡編程和其他語言都是一樣的&#xff0c;服務器這塊步驟為&#xff1a;1. 創建套接字2. 綁定地址3. 監聽該描述符的所有請求4. 有新的請求到了調用accept處理請求 Python Web服務器網關接口&#xff08;Python Web Server Gateway Interface&#xff0c;簡稱“WSGI”&…

concurrency 方面的books

http://joeduffyblog.com/2016/11/30/15-years-of-concurrency/轉載于:https://www.cnblogs.com/WCFGROUP/p/6566150.html

Spring 3.1緩存和配置

我最近在博客中談論有關Spring 3.1及其新的緩存注釋Cacheable和CacheEvict 。 與所有Spring功能一樣&#xff0c;您需要進行一定數量的設置&#xff0c;并且通常使用Spring的XML配置文件來完成。 在緩存的情況下&#xff0c;打開Cacheable和CacheEvict并不容易&#xff0c;因為…

按條件分類_保稅倉儲企業能否同時存儲非保貨物?“倉儲貨物安裝臺分類監管”如何申請?...

保稅倉儲企業能否同時存儲非保貨物呢&#xff1f;保稅和非保貨物是不是真的不能同在一個“屋檐下”呢&#xff1f;哪些企業可以開展“倉儲貨物按狀態分類監管”業務&#xff1f;企業又該如何申請該項業務&#xff1f;本文就對這些問題進行一下梳理。什么是“倉儲貨物按狀態分類…

ZooKeeper的原理(轉)

一、ZooKeeper的角色 領導者&#xff08;Leader&#xff09;&#xff0c;負責進行投票的發起和決議&#xff0c;更新系統狀態。 學習者&#xff08;Learner&#xff09;&#xff0c;包括跟隨者&#xff08;Follower&#xff09;和觀察者&#xff08;Observer&#xff09;&#…

java課堂筆記

轉載于:https://www.cnblogs.com/16-C-kai/p/6567042.html

Spring– DAO和服務層

歡迎來到Spring教程的第三部分。 在這一部分中&#xff0c;我們將繼續編寫Timesheet應用程序&#xff0c;這次我們將實現DAO層&#xff0c;業務服務并編寫一些測試。 在上一部分中&#xff0c;我們定義了GenericDao接口&#xff0c;該接口告訴我們需要對實體執行哪些操作。 現在…

51nod 1907(多項式乘法啟發式合并)

題目&#xff1a; 分析&#xff1a; 對于一個確定的生成子圖&#xff0c;很明顯是在一個連通塊上走&#xff0c;走完了再跳到另一個連通塊上&#xff0c;假設連通塊個數為cnt&#xff0c;那么答案一定是$min(a_{cnt-1},a_cnt,..,a_{n-1})$ 那現在的問題就是如何求出對于原圖而言…

煮飯的機器人作文_公示|“筆隨我心、心由筆動”作文大賽獲獎名單

卡士大昌杯“筆隨我心、心由筆動”獲獎作品開平的咸湯圓滑輪記&#xff0f;我的宅家成長記折疊式小屋&#xff0f;夕陽&#xff0f;包粽子在過去的卡士大昌杯“筆隨我心、心由筆動”作文活動中我們收到了許多優秀投稿經過專業團隊評選得出獲獎選手作品如下主辦方協辦方一等獎《…

BZOJ 4491: 我也不知道題目名字是什么

4491: 我也不知道題目名字是什么 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 278 Solved: 154[Submit][Status][Discuss]Description 給定一個序列A[i]&#xff0c;每次詢問l,r&#xff0c;求[l,r]內最長子串&#xff0c;使得該子串為不上升子串或不下降子串 Input 第一…

Spring-boot中讀取config配置文件的兩種方式

了解過spring-Boot這個技術的&#xff0c;應該知道Spring-Boot的核心配置文件application.properties&#xff0c;當然也可以通過注解自定義配置文件的信息。 Spring-Boot讀取配置文件的方式&#xff1a; 一.讀取核心配置文件信息application.properties的內容 核心配置文件是指…

JavaFX 2 GameTutorial第5部分

介紹 這是與JavaFX 2 Game Tutorial相關的六部分系列的第五部分。 我知道自從我寫關于游戲的博客以來已經很長時間了&#xff0c;但希望您仍然與我在一起。 如果您想回顧一下&#xff0c;請閱讀第1部分 &#xff0c; 第2 部分 &#xff0c; 第3 部分和第4 部分 &#xff0c;以了…

h5是可以一鍵打包小程序的_H5手機網站封裝打包微信小程序并實現分享及微信支付...

手機網站打包小程序教程&#xff0c;生成小程序&#xff0c;網頁版小程序 打包微信小程序&#xff0c;H5封裝成微信小程序。微信小程序開發一般分為2種方式&#xff0c;一種就是原生開發小程序&#xff0c;一種是將手機網站打包成小程序。原生開發小程序成本較高&#xff0c;技…

Hive中的數據庫、表、數據與HDFS的對應關系

1、hive數據庫 我們在hive終端&#xff0c;查看數據庫信息&#xff0c;可以看出hive有一個默認的數據庫default&#xff0c;而且我們還知道hive數據庫對應的是hdfs上面的一個目錄&#xff0c;那么默認的數據庫default到底對應哪一個目錄呢&#xff1f;我們可以通過hive配置文件…

軟件工程概論作業3

轉載于:https://www.cnblogs.com/clueless/p/6568351.html