缓存之EHCache(第四个记录)

七、在Spring框架中使用EHCache缓存

    就是使用Spring提供的springmodules和EHCache来简化程序的开发,通过配置文件来完成提供缓存。参考springmodules的文档。

 1、配置ehcache.xml文件

 2、创建Spring EHCache的配置xml文件

配置文件代码示例(调试通过):

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
    http://www.springmodules.org/schema/ehcache http://www.springmodules.org/schema/cache/springmodules-ehcache.xsd">
 
    <bean id="EhCacheFacade"
       class="org.springmodules.cache.provider.ehcache.EhCacheFacade">
       <property name="failQuietlyEnabled" value="true" />
       <property name="cacheManager">
           <bean
              class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
              <property name="configLocation"
                  value="classpath:ehcache.xml">
              </property>
           </bean>
       </property>
    </bean>
 
    <bean id="cachingInterceptor"
       class="org.springmodules.cache.interceptor.caching.MethodMapCachingInterceptor">
       <property name="cacheProviderFacade" ref="EhCacheFacade" />
       <property name="cachingModels">
           <props>
              <prop key="com....test.Manager.get*">
                  cacheName=dictCache
              </prop>
           </props>
       </property>
    </bean>
   
    <bean id="flushingInterceptor"
       class="org.springmodules.cache.interceptor.flush.MethodMapFlushingInterceptor">
       <property name="cacheProviderFacade" ref="EhCacheFacade" />
       <property name="flushingModels">
           <props>
              <prop key="com....test.Manager.update*">
                  cacheNames=dictCache
              </prop>
           </props>
       </property>
    </bean>
   
    <bean
       class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
       <property name="beanNames">
           <value>*anager</value>
       </property>
       <property name="interceptorNames">
           <list>
              <value>flushingInterceptor</value>
              <value>cachingInterceptor</value>
           </list>
       </property>
    </bean>
    <bean id="manager" class="com...test.Manager"></bean>
</beans>


你可能感兴趣的:(ehcache)