`
chengyue2007
  • 浏览: 1497526 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

Spring Module下配置缓存的两种方法

阅读更多

以EhCache为例说明缓存的配置方法:

第一种方法,配置Spring配置文件,使用AOP处理缓存

1)添加Provider,初始化CacheManager

Xml代码
<!--  
Thecreatedcachemanagerisaninstanceofnet.sf.ehcache.CacheManager  
-->  
<beanidbeanid="cacheManager"  
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">  
</bean>  
<beanidbeanid="cacheProviderFacade"  
class="org.springmodules.cache.provider.ehcache.EhCacheFacade">  
<propertynamepropertyname="cacheManager"ref="cacheManager"/>  
</bean>  
<!--
Thecreatedcachemanagerisaninstanceofnet.sf.ehcache.CacheManager
-->
<beanid="cacheManager"
class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
</bean>
<beanid="cacheProviderFacade"
class="org.springmodules.cache.provider.ehcache.EhCacheFacade">
<propertyname="cacheManager"ref="cacheManager"/>
</bean> 2)在ehcache.xml增加一个缓存

增加一个name为testCache的缓存。

Xml代码
<cache name="testCache"  
           maxElementsInMemory="20000"  
           maxElementsOnDisk="1000"  
           eternal="true"  
           overflowToDisk="true"  
           memoryStoreEvictionPolicy="LFU"  
    />  
<cache name="testCache"
           maxElementsInMemory="20000"
           maxElementsOnDisk="1000"
           eternal="true"
           overflowToDisk="true"
           memoryStoreEvictionPolicy="LFU"
    />3)配置缓存

Xml代码
<bean id="testService"  
 class="org.springmodules.cache.interceptor.proxy.CacheProxyFactoryBean">  
 <property name="cacheProviderFacade" ref="cacheProviderFacade" />  
 <property name="cachingModels">  
  <props>  
   <prop key="find*">cacheName=<STRONG>testCache(a)</STRONG></prop>  
   <prop key="is*">cacheName=testCache</prop>  
  </props>  
 </property>  
 <property name="flushingModels">  
  <props>  
   <prop key="insert*">cacheNames=testCache</prop>  
   <prop key="delete*">cacheNames=testCache</prop>  
   <prop key="update*">cacheNames=testCache</prop>  
  </props>  
 </property>  
 <property name="target" ref="<STRONG>testServiceTarget(b)</STRONG>" />  
</bean>  
 <bean id="testService"
  class="org.springmodules.cache.interceptor.proxy.CacheProxyFactoryBean">
  <property name="cacheProviderFacade" ref="cacheProviderFacade" />
  <property name="cachingModels">
   <props>
    <prop key="find*">cacheName=testCache(a)</prop>
    <prop key="is*">cacheName=testCache</prop>
   </props>
  </property>
  <property name="flushingModels">
   <props>
    <prop key="insert*">cacheNames=testCache</prop>
    <prop key="delete*">cacheNames=testCache</prop>
    <prop key="update*">cacheNames=testCache</prop>
   </props>
  </property>
  <property name="target" ref="testServiceTarget(b)" />
 </bean>

其中(a)为第2步定义的cache的名字,(b)为需要缓存的bean的id。

这样,第一种方式就配置好了。

第二种方法,使用Annotations的配置方式。

1)同第一种方法1)。

2)同第二种方法2)。

3)在需要处理缓存的方法前面配置Annotion

@Cacheable(modelId="testCache")//写在方法前面就会缓存方法的返回结果

@CacheFlush(modelId="testFlushing")//写在方法前面,就会在执行该函数时清除缓存里面的数据。

这样第二种方法也完成了。

第一种方法在flush的时候可以同时flush多个缓存。第二种方法只能flush一个缓存,而且第一种方法只需要修改Spring配置文件即可。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/pengchua/archive/2009/08/02/4401136.aspx

分享到:
评论

相关推荐

    spring2+hibernate3典型配置

    本文主要探讨在 Spring2 和 Hibernate3 的框架下进行典型配置的方法与实践。 #### 二、配置文件详解 在本实例中,主要包括以下四个核心配置文件:`applicationContext-resources.xml`、`applicationContext-...

    JAVA的配置

    - Maven和Gradle是两种流行的构建工具,它们有自己的配置文件(`pom.xml` 和 `build.gradle`),用于管理依赖、构建过程和项目设置。 8. **Spring框架配置**: - Spring框架的XML配置或基于注解的配置,用于定义...

    thymeleaf语法入门.pdf

    Thymeleaf也提供了标准和Spring标准两种方针,可以直接套用模板实现JSTL、OGNL表达式效果,避免每天套模板、改JSTL、改标签的困扰。同时开发人员也可以扩展和创建自定义的方针。 Thymeleaf支持多种方言,提供Spring...

    springmvc+angularjs springmvc 整合angularjs demo

    1. **创建SpringMVC项目**:设置SpringMVC环境,包括web.xml配置,Spring的配置文件(如:servlet-context.xml),以及相关的Java配置类。 2. **编写Controller**:创建一个Controller,定义一个返回JSON数据的方法...

    Dubbo面试40问及参考答案.docx

    - **同步与异步**:Dubbo支持同步和异步两种调用模式,默认情况下采用同步调用模式。 - **异步调用流程**:异步调用时,Dubbo会返回一个Future对象,用于获取最终的结果。 #### 21. 分布式事务 - **事务支持**:...

    WebService大讲堂之Axis2

    发布 WebService 到 Axis2 可以通过两种方式:一种是将服务类打包到一个 AAR(Axis Archive)文件中,然后部署到 Axis2;另一种是直接使用 Axis2 提供的命令行工具或者编程接口动态发布。在 Axis2 的 war 版本中,...

    RedisWindows安装文件5.0.10.rar

    4. **启动Redis服务**:有两种启动方式,一是通过命令行运行`redis-server.exe`(位于`C:\Program Files\Redis`目录下),二是将Redis作为Windows服务安装。若选择后者,需要在命令行中输入`redis-server --install-...

    SpringBoot开发非常美观的java博客系统(包含后台管理功能).docx

    编辑器部分,采用了UEditor和Markdown,为用户提供富文本和纯文本两种编辑体验。字体图标则选择了font-Awesome,提供了丰富的图形资源。 在准备工作阶段,开发者需要安装JDK8和Maven,选择合适的IDE进行项目导入。...

    Axis2,CXF版本学习笔记

    2. **模块(Module)**:Axis2的模块化架构允许你插入自定义行为,如安全、缓存等,以增强服务功能。 3. **消息处理器和服务部署**:了解如何配置和部署服务,以及如何处理入站和出站消息,是Axis2学习的重要部分。...

Global site tag (gtag.js) - Google Analytics