配置每一项的详细作用不再详细解释,有兴趣的请google下 ,这里需要注意一点defaultCache标签定义了一个默认的Cache,这个Cache是不能删除的,否则会抛出No default cache is configured异常。另外,由于使用拦截器来刷新Cache内容,因此在定义cache生命周期时可以定义较大的数值,timeToIdleSeconds="300000" timeToLiveSeconds="600000",好像还不够大?

然后,在将Cache和两个拦截器配置到Spring,这里没有使用2.0里面AOP的标签。 
cacheContext.xml

Java代码 
<?xml version="1.0" encoding="UTF-8"?>    
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
http://www.springframework.org/dtd/spring-beans.dtd">    
<beans>    
    <!-- 引用ehCache的配置 -->    
    <bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">    
      <property name="configLocation">    
        <value>ehcache.xml</value>    
      </property>    
    </bean>    
        
    <!-- 定义ehCache的工厂,并设置所使用的Cache name -->    
    <bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">    
      <property name="cacheManager">    
        <ref local="defaultCacheManager"/>    
      </property>    
      <property name="cacheName">    
          <value>DEFAULT_CACHE</value>    
      </property>    
    </bean>    
   
    <!-- find/create cache拦截器 -->    
    <bean id="methodCacheInterceptor" class="com.co.cache.ehcache.MethodCacheInterceptor">    
      <property name="cache">    
        <ref local="ehCache" />    
      </property>    
    </bean>    
    <!-- flush cache拦截器 -->    
    <bean id="methodCacheAfterAdvice" class="com.co.cache.ehcache.MethodCacheAfterAdvice">    
      <property name="cache">    
        <ref local="ehCache" />    
      </property>    
    </bean>    
        
    <bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">    
      <property name="advice">    
        <ref local="methodCacheInterceptor"/>    
      </property>    
      <property name="patterns">    
        <list>    
            <value>.*find.*</value>    
            <value>.*get.*</value>    
        </list>    
      </property>    
    </bean>    
    <bean id="methodCachePointCutAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">    
      <property name="advice">    
        <ref local="methodCacheAfterAdvice"/>    
      </property>    
      <property name="patterns">    
        <list>    
          <value>.*create.*</value>    
          <value>.*update.*</value>    
          <value>.*delete.*</value>    
        </list>    
      </property>    
    </bean>    
</beans> 

上面的代码最终创建了两个"切入点",methodCachePointCut和methodCachePointCutAdvice,分别用于拦截不同方法名的方法,可以根据需要任意增加所需要拦截方法的名称。 
需要注意的是

Java代码 
<bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">    
      <property name="cacheManager">    
        <ref local="defaultCacheManager"/>    
      </property>    
      <property name="cacheName">    
          <value>DEFAULT_CACHE</value>    
      </property>    
    </bean> 
<bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
   <property name="cacheManager">
   <ref local="defaultCacheManager"/>
   </property>
   <property name="cacheName">
    <value>DEFAULT_CACHE</value>
   </property>
</bean>

如果cacheName属性内设置的name在ehCache.xml中无法找到,那么将使用默认的cache(defaultCache标签定义).

事实上到了这里,一个简单的Spring + ehCache Framework基本完成了,为了测试效果,举一个实际应用的例子,定义一个TestService和它的实现类TestServiceImpl,里面包含

两个方法getAllObject()和updateObject(Object Object),具体代码如下 
TestService.java

Java代码 
package com.co.cache.test;    
   
import java.util.List;    
   
public interface TestService {    
    public List getAllObject();    
   
    public void updateObject(Object Object);    
}

TestServiceImpl.java

Java代码 
package com.co.cache.test;    
   
import java.util.List;    
   
public class TestServiceImpl implements TestService    
{    
    public List getAllObject() {    
        System.out.println("---TestService:Cache内不存在该element,查找并放入Cache!");    
        return null;    
    }    
   
    public void updateObject(Object Object) {    
        System.out.println("---TestService:更新了对象,这个Class产生的cache都将被remove!");    
    }    
}

使用Spring提供的AOP进行配置 
applicationContext.xml

Java代码 
<?xml version="1.0" encoding="UTF-8"?>    
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "
http://www.springframework.org/dtd/spring-beans.dtd">    
   
<beans>    
    <import resource="cacheContext.xml"/>    
        
    <bean id="testServiceTarget" class="com.co.cache.test.TestServiceImpl"/>    
        
    <bean id="testService" class="org.springframework.aop.framework.ProxyFactoryBean">    
      <property name="target">    
          <ref local="testServiceTarget"/>    
      </property>    
      <property name="interceptorNames">    
        <list>    
          <value>methodCachePointCut</value>    
          <value>methodCachePointCutAdvice</value>    
        </list>    
      </property>    
    </bean>    
</beans> 

这里一定不能忘记import cacheContext.xml文件,不然定义的两个拦截器就没办法使用了。

最后,写一个测试的代码 
MainTest.java

Java代码 
package com.co.cache.test;    
   
import org.springframework.context.ApplicationContext;    
import org.springframework.context.support.ClassPathXmlApplicationContext;    
   
public class MainTest{    
    public static void main(String args[]){    
        String DEFAULT_CONTEXT_FILE = "/applicationContext.xml";    
        ApplicationContext context = new ClassPathXmlApplicationContext(DEFAULT_CONTEXT_FILE);    
        TestService testService = (TestService)context.getBean("testService");    
   
        System.out.println("1--第一次查找并创建cache");    
        testService.getAllObject();    
            
        System.out.println("2--在cache中查找");    
        testService.getAllObject();    
            
        System.out.println("3--remove cache");    
        testService.updateObject(null);    
            
        System.out.println("4--需要重新查找并创建cache");    
        testService.getAllObject();    
    }       
}

运行,结果如下

Java代码 
1--第一次查找并创建cache    
---TestService:Cache内不存在该element,查找并放入Cache!    
2--在cache中查找    
3--remove cache    
---TestService:更新了对象,这个Class产生的cache都将被remove!    
4--需要重新查找并创建cache    
---TestService:Cache内不存在该element,查找并放入Cache!

可以看到,第一步执行getAllObject(),执行TestServiceImpl内的方法,并创建了cache,在第二次执行getAllObject()方法时,由于cache有该方法的缓存,直接从cache中get出方法的结果,所以没有打印出TestServiceImpl中的内容,而第三步,调用了updateObject方法,和TestServiceImpl相关的cache被remove,所以在第四步执行时,又执行TestServiceImpl中的方法,创建Cache。

注意的问题

我们知道,Cache为ehcache.XML配置文件里面所定义的缓存类别,获取某一特定的缓存类别的方法如下:
Cache cache= cacheManager.getCache(cacheName);
cacheName为想获取的缓存类别名。然后象下面方法把某一对象放入上面定义的缓存:
cache.put(new Element(key,(Serializable)value));
key为 放入该缓存中的对象的索引值,value为放入该缓存中key所对应的对象。我们看到,放入缓存中的value必须序列化,Java原生类型 char、int ,原生类型的包装类String、Character、Integer、Number...和集合List的实现类ArrayList都已经实现了Serializable接口,它们都可以直接放到缓存中。这里要注意的是,在方法返回值中经常用到的Iterator,并没有实现Serializable接口,所以Cache 不能缓存返回值类型为Iterator的方法。
    还有一点要注意的是,如果我们要做缓存的方法是在bean的生命周期的初始化阶段调用的(例如setter,init),此时方法缓存拦截器还没被调用执行,那么缓存将不起作用,如做了下面的配置:
<bean id="OrganizationManagerMethodCache" class="org.springFramework.aop.framework.ProxyFactoryBean">
    <property name="target">
        <bean class="com.wzj.rbac.ServiceFacade.OrganizationManager" init-method="init" autowire="byName"/>     
    </property>
    <property name="interceptorNames">
       <list>
            <value>methodCachePointCut</value>
        </list>
     </property>
</bean>
在init初始方法里面调用的缓存方法将失效。

转Spring+Hibernate+EHcache配置(三)的更多相关文章

  1. 转Spring+Hibernate+EHcache配置(二)

    Spring AOP+EHCache简单缓存系统解决方案 需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系 ...

  2. 【转】Spring+Hibernate+EHcache配置(一)

    大量数据流动是web应用性能问题常见的原因,而缓存被广泛的用于优化数据库应用.cache被设计为通过保存从数据库里load的数据来减少应用和数据库之间的数据流动.数据库访问只有当检索的数据不在cach ...

  3. Spring+Hibernate整合配置 --- 比较完整的spring、hibernate 配置

    Spring+Hibernate整合配置 分类: J2EE2010-11-25 17:21 16667人阅读 评论(1) 收藏 举报 springhibernateclassactionservlet ...

  4. Maven 工程下 Spring MVC 站点配置 (三) C3P0连接池与@Autowired的应用

    Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 前两篇文章主要是对站点和数据库操作配置进行了演示,如果单 ...

  5. Spring Security认证配置(三)

    学习本章之前,可以先了解下上篇Spring Security认证配置(二) 本篇想要达到这样几个目的: 1.登录成功处理 2.登录失败处理 3.调用方自定义登录后处理类型 具体配置代码如下: spri ...

  6. Hibernate+EhCache配置二级缓存

    步骤: 第一步:加入ehcache.jar 第二步: 在src目录下新建一个文件,名为:ehcache.xml 第三步:在hibernate配置文件的<session-factory>下配 ...

  7. SSH(Struts+spring+hibernate)配置

    1.spring和struts 1)web.xml 配置spring的ContextLoaderListener(监听器) 配置Struts的StrutsPrepareAndExecuteFilter ...

  8. Hibernate4+EhCache配置二级缓存

    本文主要讲一讲Hibernate+EhCache配置二级缓存的基本使用方法 (有关EhCache的基础介绍可参见:http://sjsky.iteye.com/blog/1288257 ) Cache ...

  9. SSH框架简化(struts2+spring+hibernate)

    目的: 通过对ssh框架有了基础性的学习,本文主要是使用注解的方式来简化ssh框架的代码编写. 注意事项: 1.运行环境:Windows 8-64位,Eclipse(开发工具),jdk1.8.0_91 ...

随机推荐

  1. 合理使用mysql中的load data infile导入数据

    基本语法: load data  [low_priority] [local] infile 'file_name txt' [replace | ignore]into table tbl_name ...

  2. PHP获取本周第一天和最后一天

    //本周的第一天和最后一天  代码如下 复制代码 $date=new DateTime();$date->modify('this week');$first_day_of_week=$date ...

  3. Bresenham算法画填充圆及SDL代码实现

    画圆是计算机图形操作中一个非常重要的需求.普通的画圆算法需要大量的浮点数参与运算,而众所周知,浮点数的运算速度远低于整形数.而最终屏幕上影射的像素的坐标均为整形,不可能是连续的线,所以浮点数运算其实纯 ...

  4. echarts标准饼图解读(一)——提示框(tooltip)配置

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 【leetcode】352. Data Stream as Disjoint Intervals

    问题描述: Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers ...

  6. blazeDS集成spring的remote访问

     欢迎交流转载,请注明出处:http://www.cnblogs.com/shizhongtao/p/3490037.html 上一篇我只是简单实用blazeds创建了一个实例,大多数开发中,都是结合 ...

  7. 暑假集训(4)第六弹——— 组合(poj1067)

    题意概括:上一次,你成功甩掉了fff机械兵.不过,你们也浪费了相当多的时间.fff团已经将你们团团包围,并且逐步 逼近你们的所在地.面对如此危机,你不由得悲观地想:难道这acm之路就要从此中断?虽然走 ...

  8. open_clientfd(char* hostname,int port)和open_listenfd(int port)

    #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h&g ...

  9. thinkphp 前后版本ajaxReturn方法的分别

    之前用的是thinkphp2的版本现在改到thinkphp3.2已上的版本,发现ajaxReturn这个方法返回的数据不一样了,现在做下记录 thinkphp2的ajaxReturn的实现原码 pro ...

  10. Linux C 程序指针和指针数组(NIGH)

    指针和指针数组 #include<stdio.h> int main() { , b = ; int *p1 = &a , *p2 = &b ; printf(" ...