Spring2..5整合Ehacahe

 在整合的过程中真是报各种各样的错误,其中最主要的就是jar包冲突以及不全,所以此文档中把所需要的必须jar都列了出来

1,必须的jar

spring.jar spring-modules-cache.jar ehcache-core-2.4.6.jar

slf4j-api-1.5.6.jar slf4j-jdk-1.5.2.jar slf4-log4j-1.5.6.jar

oro-2.0.8.jar log4j-1.2.9.jar commons-logging-1.0.4.jar cglib-nodep-2.1_3.jar

asm-util-2.2.3.jar asm-commons-2.2.3.jar asm-2.2.3.jar

 

2,applicationContext.xml中的配置

 

  
  
  
  
  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-2.5.xsd  
  6.         "> 
  7.          
  8.         <!-- 使用EhcacheManager --> 
  9.         <bean id="cacheManager" 
  10.             class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> 
  11.         <property name="configLocation" value="classpath:apEhcache.xml"/> 
  12.         </bean> 
  13.  
  14.         <bean id="cacheProviderFacade" class="org.springmodules.cache.provider.ehcache.EhCacheFacade"> 
  15.             <property name="cacheManager" ref="cacheManager"/> 
  16.         </bean> 
  17.          
  18.         <!-- 配置方法拦截器 --> 
  19.         <!-- 缓存拦截器 --> 
  20.         <bean id="cachingInterceptor"  class="org.springmodules.cache.interceptor.caching.MethodMapCachingInterceptor"> 
  21.          
  22.         <property name="cacheProviderFacade" ref="cacheProviderFacade"/> 
  23.         <property name="cachingModels"> 
  24.             <props> 
  25.                 <!-- 所有StudentService对象中,以get开头的方法都将进行缓存 --> 
  26.                 <prop key="com.service.StudentService.get*"> 
  27.                     cacheName=testCache 
  28.                 </prop> 
  29.             </props> 
  30.         </property> 
  31.         </bean> 
  32.         <!-- 缓存刷新拦截器 --> 
  33.         <bean id="fulshingInterceptor" class="org.springmodules.cache.interceptor.flush.MethodMapFlushingInterceptor"> 
  34.               <property name="cacheProviderFacade" ref="cacheProviderFacade"/> 
  35.               <property name="flushingModels"> 
  36.                     <!-- 进行cache刷新(清除) --> 
  37.                     <props> 
  38.                         <prop key="com.service.StudentService.set*"> 
  39.                             cacheNames=testCache 
  40.                         </prop> 
  41.                     </props> 
  42.               </property> 
  43.         </bean> 
  44.          
  45.         <!-- 配置 基于BeanName规则的动态代理封装 --> 
  46.         <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> 
  47.             <property name="beanNames"> 
  48.                 <list> 
  49.                     <value>studentService</value> 
  50.                 </list> 
  51.             </property> 
  52.             <property name="interceptorNames"> 
  53.                 <list> 
  54.                     <value>cachingInterceptor</value> 
  55.                     <value>fulshingInterceptor</value> 
  56.                 </list> 
  57.             </property> 
  58.         </bean> 
  59.          
  60.         <bean id="studentService" class="com.service.StudentService"></bean> 
  61.     </beans> 

3,对缓存的配置(apEhcache.xml)

 

  
  
  
  
  1. <?xml version="1.0" encoding="UTF-8"?> 
  2.     <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  3.         xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> 
  4.  
  5.         <diskStore path="java.io.tmpdir" /> 
  6.  
  7.         <defaultCache maxElementsInMemory="10000" eternal="false" 
  8.             timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" 
  9.             maxElementsOnDisk="10000000" diskPersistent="false" 
  10.             diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" /> 
  11.         <cache name="testCache" 
  12.             maxElementsInMemory="20000" maxElementsOnDisk="1000" eternal="true" 
  13.             overflowToDisk="true" memoryStoreEvictionPolicy="LFU" /> 
  14.     </ehcache> 

4、测试Service

 

  
  
  
  
  1. package com.service; 
  2.  
  3.         public class StudentService { 
  4.          
  5.         private String name = "matthew"
  6.  
  7.             public String getName() { 
  8.                 return name; 
  9.             } 
  10.  
  11.             public String getName(String salution) { 
  12.                 return salution + " " + name; 
  13.             } 
  14.  
  15.             public void setName(String name) { 
  16.                 this.name = name; 
  17.             } 
  18.  
  19.             public void changeNameAndNotTellCache(String name) { 
  20.                 this.name = name; 
  21.             } 
  22.  
  23.         } 

5、使用方法Clent

 

  
  
  
  
  1. package com.service; 
  2.  
  3.     import org.springframework.context.support.AbstractApplicationContext; 
  4.     import org.springframework.context.support.ClassPathXmlApplicationContext; 
  5.  
  6.     public class TestCache { 
  7.         public static void main(String[] args) { 
  8.                   AbstractApplicationContext context; 
  9.                   context = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml"); 
  10.                   context.start(); 
  11.                   
  12.                 StudentService ss = (StudentService) context.getBean("studentService"); 
  13.               n(name); 
  14.                  //update cache 
  15.                  System.out.println("清除缓存后,再次访问 "); 
  16.                  ss.setName("Michael"); 
  17.                  name = ss.getName(); 
  18.                  System.out.println(name); 
  19.                   
  20.                 name = ss.getName("Mr"); 
  21.                  System.out.println(name); 
  22.                   
  23.                  context.close(); 
  24.                  } 
  25.     } 

 

你可能感兴趣的:(spring,Ehacahe)