昌邑建设局网站,机关网站制度建设,上海外贸网站开发,如何做自己网站的seo我最近在博客中谈论有关Spring 3.1及其新的缓存注释Cacheable和CacheEvict 。 与所有Spring功能一样#xff0c;您需要进行一定数量的设置#xff0c;并且通常使用Spring的XML配置文件来完成。 在缓存的情况下#xff0c;打开Cacheable和CacheEvict并不容易#xff0c;因为… 我最近在博客中谈论有关Spring 3.1及其新的缓存注释Cacheable和CacheEvict 。 与所有Spring功能一样您需要进行一定数量的设置并且通常使用Spring的XML配置文件来完成。 在缓存的情况下打开Cacheable和CacheEvict并不容易因为您要做的就是将以下内容添加到Spring配置文件中 cache:annotation-driven / …以及您的beans XML元素声明中的适当模式定义 beans xmlnshttp://www.springframework.org/schema/beans xmlns:phttp://www.springframework.org/schema/pxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:cachehttp://www.springframework.org/schema/cache xmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache.xsd …的主要特点是 xmlns:cachehttp://www.springframework.org/schema/cache …和 http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd 但是这还不是故事的结局因为您还需要指定一个缓存管理器和一个缓存实现。 好消息是如果您熟悉其他Spring组件例如数据库事务管理器的设置那么这样做的方式就不足为奇了。 缓存管理器类似乎是实现Spring的org.springframework.cache.CacheManager接口的任何类。 它负责管理一个或多个缓存实施其中缓存实施实例负责实际缓存数据。 下面的XML示例摘自我最近两个博客中使用的示例代码。 bean idcacheManager classorg.springframework.cache.support.SimpleCacheManagerproperty namecachessetbeanclassorg.springframework.cache.concurrent.ConcurrentMapCacheFactoryBeanp:nameemployee/!-- TODO Add other cache instances in here--/set/property
/bean 在上面的配置中我使用Spring的SimpleCacheManager来管理其ConcurrentMapCacheFactoryBean实例该实例的缓存实现名为“ employee ”。 需要注意的重要一点是您的缓存管理器必须具有cacheManager 。 如果您弄错了那么将得到以下异常 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.NoSuchBeanDefinitionException: No bean named cacheManager is definedat 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)
:
: trace details removed for clarity
:at 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.NoSuchBeanDefinitionException:
No bean named cacheManager is definedat org.springframework.beans.factory.support.DefaultListableBeanFactory.
getBeanDefinition(DefaultListableBeanFactory.java:553)at org.springframework.beans.factory.support.AbstractBeanFactory.
getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)at org.springframework.beans.factory.support.AbstractBeanFactory.
doGetBean(AbstractBeanFactory.java:277)at org.springframework.beans.factory.support.AbstractBeanFactory.
getBean(AbstractBeanFactory.java:193)at org.springframework.beans.factory.support.BeanDefinitionValueResolver.
resolveReference(BeanDefinitionValueResolver.java:322) 就像我在上面说的那样在我的简单配置中整个工作由SimpleCacheManager协调。 根据文档这通常是“用于测试或简单的缓存声明”。 尽管您可以编写自己的CacheManager实现但Spring的专家们为不同情况提供了其他缓存管理器 SimpleCacheManager –参见上文。 NoOpCacheManager –用于测试因为它实际上并不缓存任何内容尽管在这里要小心因为在不进行缓存的情况下测试代码可能会在打开缓存时使您绊倒。 CompositeCacheManager –允许在单个应用程序中使用多个缓存管理器。 EhCacheCacheManager –包装ehCache实例的缓存管理器。 见http://ehcache.org 对于Spring Profile选择在任何给定环境中使用哪个缓存管理器似乎是一个很好的用途。 看到 在XML Config中使用Spring配置文件 使用Spring Profiles和Java配置 而且尽管只是为了完整起见但这只是将内容整理一下下面是我前两个博客中使用的完整配置文件 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beans xmlns:phttp://www.springframework.org/schema/pxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexmlns:cachehttp://www.springframework.org/schema/cache xmlns:contexthttp://www.springframework.org/schema/contextxsi:schemaLocationhttp://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-packagecaching /!-- simple cache manager --bean idcacheManager classorg.springframework.cache.support.SimpleCacheManagerproperty namecachessetbean classorg.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean p:nameemployee/!-- TODO Add other cache instances in here--/set/property/bean/beans 正如哥伦波中尉喜欢说“还有一件事你知道让我为这个案件烦恼……” 好吧关于缓存管理器有几件事让我感到困扰例如 在谈论SimpleCacheManager时Spring的家伙们所说的“对测试或简单的缓存声明有用”是什么意思 您究竟应该何时愤怒地使用它而不是进行测试 最好编写自己的CacheManager实现甚至是Cache实现吗 使用EhCacheCacheManager确切优势是什么 您真正需要多少时间CompositeCacheManager 我将来可能会研究所有这些…… 祝您编程愉快别忘了分享 参考来自Captain Debugs Blog博客的JCG合作伙伴 Roger Hughes的Spring 3.1 Caching and Config 。 翻译自: https://www.javacodegeeks.com/2012/09/spring-31-caching-and-config.html