如果在网上查找使用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

Spring 3.1:缓存和EhCache相关推荐

  1. 在Spring、Hibernate中使用Ehcache缓存

    前一篇http://blog.csdn.net/ibm_hoojo/article/details/7739181介绍了Ehcache整合Spring缓存,使用页面.对象缓存:这里将介绍在Hibern ...

  2. Spring Boot 揭秘与实战(二) 数据缓存篇 - EhCache

    文章目录 1. EhCache 集成 2. 源代码 本文,讲解 Spring Boot 如何集成 EhCache,实现缓存. 在阅读「Spring Boot 揭秘与实战(二) 数据缓存篇 - 快速入门 ...

  3. 基于Spring的Web缓存

    缓存的基本思想其实是以空间换时间.我们知道,IO的读写速度相对内存来说是非常比较慢的,通常一个web应用的瓶颈就出现在磁盘IO的读写上.那么,如果我们在内存中建立一个存储区,将数据缓存起来,当浏览器端 ...

  4. 如何在spring中使用缓存

    Spring Cache 缓存是实际工作中非常常用的一种提高性能的方法, 我们会在许多场景下来使用缓存. 本文通过一个简单的例子进行展开,通过对比我们原来的自定义缓存和 spring 的基于注释的 c ...

  5. 从零开始学 Java - Spring 集成 Memcached 缓存配置(二)

    Memcached 客户端选择 上一篇文章 从零开始学 Java - Spring 集成 Memcached 缓存配置(一)中我们讲到这篇要谈客户端的选择,在 Java 中一般常用的有三个: Memc ...

  6. 三大缓存框架ehcache、memcache和redis的介绍

    三大缓存框架ehcache.memcache和redis的介绍 2016-04-12 架构说 4964 阅读 最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存 ...

  7. Spring 3.1缓存和@Cacheable

    缓存在软件领域已经存在很长时间了. 它们是那些真正有用的东西之一,一旦您开始使用它们,您会想知道如果没有它们,您是如何相处的,所以似乎让Spring的家伙们只是在版本中向Spring核心添加缓存实现有 ...

  8. Spring Boot————默认缓存应用及原理

    引言 应用程序的数据除了可以放在配置文件中.数据库中以外,还会有相当一部分存储在计算机的内存中,这部分数据访问速度要快于数据库的访问,因此通常在做提升数据访问速度时,会将需要提升访问速度的数据放入到内 ...

  9. Spring Boot Redis缓存

    Spring Boot Redis缓存 目录[ 隐藏 ] 1 Spring Boot Redis缓存 1.1 Spring Boot Redis项目设置 1.2 Spring Boot Redis缓存 ...

最新文章

  1. boost::math模块使用指定宽度的浮点 typedef估中等复杂的数学函数的测试程序
  2. Android之实现多张图片点击预览(支持放缩)和滑动
  3. [码海拾贝 之JS] JS 之数组排序
  4. rootfs文件系统的制作(一)
  5. Mybatis的简单增删查改(CRUD)
  6. 最大矩形面积(C++实现)
  7. win10搭建无盘服务器配置,win10系统无盘安装教程
  8. 2022最新Android项目导入过程(以Android studio2021.2.1为例)
  9. 论文查重系统原理是什么?
  10. YGEV型系列电磁式明渠流速仪
  11. linux虚拟主机的三种方法
  12. VScode中出现提示Code 安装似乎损坏。请重新安装。
  13. C++ Primer Message和Folder类
  14. 如何防范计算机安全,计算机安全风险及防范措施
  15. svn提交变慢,svn update提示Node remains in conflict报错解决办法
  16. MIUI金凡回应用户反馈小米手机发热情况
  17. Application.streamingAssetsPath在android和ios和pc上的读取
  18. Yarn Web页面 8088 端口在Windows浏览器无法访问
  19. Unity 接入高德开放API - 天气查询
  20. android rtmp推流,使用MediaCodec和RTMP做直播推流

热门文章

  1. thinking-in-java(12)通过异常处理错误
  2. apache ignite_Kubernetes集群上的Apache Ignite和Spring第3部分:测试应用程序
  3. gradle配置_Gradle配置
  4. ibm liberty_使用Eclipse和Open Liberty的Java EE 8上的Java 9
  5. 设计模式示例_责任链设计模式示例
  6. 访问nfs_通过NFS访问编年引擎
  7. 您想了解的所有Throwable
  8. java8 默认方法_Java 8的默认方法:可以做什么和不能做什么?
  9. java 基础包的功能_Java 8的功能基础
  10. Java 9中的无限集