Spring基于注解的缓存配置--EHCache AND OSCache

阅读更多

本文将构建一个普通工程来说明spring注解缓存的使用方式,关于如何在web应用中使用注解缓存,请参见:

Spring基于注解的缓存配置--web应用实例

 

一.简介 
在spring的modules包中提供对许多第三方缓存方案的支持,包括: 
EHCache 
OSCache(OpenSymphony) 
JCS 
GigaSpaces 
JBoss Cache 
等等。 
将这些第三方缓存方案配置在spring中很简单,网上有许多介绍,这里只重点介绍如何配置基于注解的缓存配置。 
本文将通过例举EHCache和OSCache详细介绍如何使用spring配置基于注解的缓存配置,注意这里的缓存是方法级的。 

二.依赖 
在开始介绍如何进行缓存配置前先介绍一下EHCache和OSCache的jar依赖。 
EHCache: 
ehcache-core-1.7.2.jar 
jakarta-oro-2.0.8.jar 
slf4j-api-1.5.8.jar 
slf4j-jdk14-1.5.8.jar 

OSCache: 
oscache-2.4.1.jar 

此外,两者都需要的jar如下: 
cglib-nodep-2.1_3.jar 
commons-logging.jar 
log4j-1.2.15.jar 
spring-modules-cache.jar 
spring.jar 

三.配置

两种缓存在spring配置文件中都可以使用两种配置方式,一种是spring2.0以前的完全基于bean的复杂配置,一种是使用后来的基于命名空间的简单配置,两种配置效果相同,分别介绍如下:


EHCache: 
1)普通配置

 

[xhtml]  view plain copy
 
  1. xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans      
  5.   http://www.springframework.org/schema/beans/spring-beans.xsd">    
  6.     
  7.         
  8.     <bean id="autoproxy"    
  9.         class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />    
  10.     
  11.          
  12.         
  13.     <bean id="cacheManager"    
  14.         class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">    
  15.         <property name="configLocation" value="classpath:ehcache.xml" />    
  16.     bean>    
  17.     <bean id="cacheProviderFacade" class="org.springmodules.cache.provider.ehcache.EhCacheFacade">    
  18.         <property name="cacheManager" ref="cacheManager" />    
  19.     bean>    
  20.     
  21.     
  22.         
  23.     <bean id="cachingAttributeSource"    
  24.         class="org.springmodules.cache.annotations.AnnotationCachingAttributeSource">bean>    
  25.         
  26.     <bean id="cachingInterceptor"    
  27.         class="org.springmodules.cache.interceptor.caching.MetadataCachingInterceptor">    
  28.         <property name="cacheProviderFacade" ref="cacheProviderFacade" />    
  29.         <property name="cachingAttributeSource" ref="cachingAttributeSource" />    
  30.         <property name="cachingModels">    
  31.             <props>    
  32.                 <prop key="testCaching">cacheName=testCacheprop>    
  33.             props>    
  34.         property>    
  35.     bean>    
  36.     
  37.         
  38.     <bean id="cachingAttributeSourceAdvisor"    
  39.         class="org.springmodules.cache.interceptor.caching.CachingAttributeSourceAdvisor">    
  40.         <constructor-arg ref="cachingInterceptor" />    
  41.     bean>    
  42.     
  43.         
  44.     <bean id="flushingAttributeSource"    
  45.         class="org.springmodules.cache.annotations.AnnotationFlushingAttributeSource">bean>    
  46.     
  47.         
  48.    <bean id="flushingInterceptor"    
  49.         class="org.springmodules.cache.interceptor.flush.MetadataFlushingInterceptor">    
  50.         <property name="cacheProviderFacade" ref="cacheProviderFacade" />    
  51.         <property name="flushingAttributeSource" ref="flushingAttributeSource" />    
  52.         <property name="flushingModels">    
  53.             <map>    
  54.                 <entry key="testFlushing">    
  55.                     <bean    
  56.                         class="org.springmodules.cache.provider.ehcache.EhCacheFlushingModel">    
  57.                              
  58.                         <property name="cacheNames">    
  59.                             <list>    
  60.                                 <value>testCachevalue>    
  61.                             list>    
  62.                         property>    
  63.                               
  64.                              
  65.     <bean id="flushingAttributeSourceAdvisor"    
  66.         class="org.springmodules.cache.interceptor.flush.FlushingAttributeSourceAdvisor">    
  67.         <constructor-arg ref="flushingInterceptor" />    
  68.     bean>    
  69.     
  70.         
  71.     <bean id="testCache" class="com.TestCache"/>    
  72.     
  73. beans>  

 

 

2)命名空间配置

 

[xhtml]  view plain copy
 
  1. xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ehcache="http://www.springmodules.org/schema/ehcache"    
  4.     xsi:schemaLocation="     
  5.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd     
  6.             http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">    
  7.     
  8.     
  9.     <bean id="autoproxy"    
  10.         class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />    
  11.     
  12.         
  13.     <bean id="cacheManager"    
  14.         class="org.springmodules.cache.provider.oscache.OsCacheManagerFactoryBean">    
  15.         <property name="configLocation" value="classpath:oscache.properties" />    
  16.     bean>    
  17.         
  18.     <bean id="cacheProviderFacade" class="org.springmodules.cache.provider.oscache.OsCacheFacade">    
  19.         <property name="cacheManager" ref="cacheManager" />    
  20.     bean>    
  21.     
  22.     
  23.     
  24.         
  25.     <bean id="cachingAttributeSource"    
  26.         class="org.springmodules.cache.annotations.AnnotationCachingAttributeSource">bean>    
  27.     
  28.         
  29.    <bean id="cachingInterceptor"    
  30.         class="org.springmodules.cache.interceptor.caching.MetadataCachingInterceptor">    
  31.         <property name="cacheProviderFacade" ref="cacheProviderFacade" />    
  32.         <property name="cachingAttributeSource" ref="cachingAttributeSource" />    
  33.         <property name="cachingModels">    
  34.             <props>    
  35.                 <prop key="testCaching">refreshPeriod=86400;cronExpression=0 1 * * *;groups=pb_testprop>    
  36.             props>    
  37.         property>    
  38.     bean>    
  39.     
  40.         
  41.     <bean id="cachingAttributeSourceAdvisor"    
  42.         class="org.springmodules.cache.interceptor.caching.CachingAttributeSourceAdvisor">    
  43.         <constructor-arg ref="cachingInterceptor" />    
  44.     bean>    
  45.     
  46.         
  47.     <bean id="flushingAttributeSource"    
  48.         class="org.springmodules.cache.annotations.AnnotationFlushingAttributeSource">bean>    
  49.     
  50.         
  51.     <bean id="flushingInterceptor"    
  52.         class="org.springmodules.cache.interceptor.flush.MetadataFlushingInterceptor">    
  53.         <property name="cacheProviderFacade" ref="cacheProviderFacade" />    
  54.         <property name="flushingAttributeSource" ref="flushingAttributeSource" />    
  55.         <property name="flushingModels">    
  56.             <props>    
  57.                 <prop key="testFlushing">groups=pb_testprop>    
  58.             props>    
  59.         property>    
  60.     bean>    
  61.     
  62.         
  63.     <bean id="flushingAttributeSourceAdvisor"    
  64.         class="org.springmodules.cache.interceptor.flush.FlushingAttributeSourceAdvisor">    
  65.         <constructor-arg ref="flushingInterceptor" />    
  66.     bean>    
  67.     
  68.     <bean id="testCache" class="com.TestCache"/>    
  69.     
  70. beans>  

 

 

2)命名空间配置

 

[xhtml]  view plain copy
 
  1. xml version="1.0" encoding="UTF-8"?>    
  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:oscache="http://www.springmodules.org/schema/oscache"    
  4.     xsi:schemaLocation="     
  5.             http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd     
  6.             http://www.springmodules.org/schema/oscache http://www.springmodules.org/schema/cache/springmodules-oscache.xsd">    
  7.     

你可能感兴趣的:(Spring基于注解的缓存配置--EHCache AND OSCache)