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中的配置

 

 <?xml version="1.0" encoding="UTF-8"?>     <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://www.springframework.org/schema/beans          http://www.springframework.org/schema/beans/spring-beans-2.5.xsd          ">                  <!-- 使用EhcacheManager -->         <bean id="cacheManager"             class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">         <property name="configLocation" value="classpath:apEhcache.xml"/>         </bean>          <bean id="cacheProviderFacade" class="org.springmodules.cache.provider.ehcache.EhCacheFacade">             <property name="cacheManager" ref="cacheManager"/>         </bean>                  <!-- 配置方法拦截器 -->         <!-- 缓存拦截器 -->         <bean id="cachingInterceptor"  class="org.springmodules.cache.interceptor.caching.MethodMapCachingInterceptor">                  <property name="cacheProviderFacade" ref="cacheProviderFacade"/>         <property name="cachingModels">             <props>                 <!-- 所有StudentService对象中,以get开头的方法都将进行缓存 -->                 <prop key="com.service.StudentService.get*">                     cacheName=testCache                 </prop>             </props>         </property>         </bean>         <!-- 缓存刷新拦截器 -->         <bean id="fulshingInterceptor" class="org.springmodules.cache.interceptor.flush.MethodMapFlushingInterceptor">               <property name="cacheProviderFacade" ref="cacheProviderFacade"/>               <property name="flushingModels">                     <!-- 进行cache刷新(清除) -->                     <props>                         <prop key="com.service.StudentService.set*">                             cacheNames=testCache                         </prop>                     </props>               </property>         </bean>                  <!-- 配置 基于BeanName规则的动态代理封装 -->         <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">             <property name="beanNames">                 <list>                     <value>studentService</value>                 </list>             </property>             <property name="interceptorNames">                 <list>                     <value>cachingInterceptor</value>                     <value>fulshingInterceptor</value>                 </list>             </property>         </bean>                  <bean id="studentService" class="com.service.StudentService"></bean>     </beans>

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

 

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

4、测试Service

 

 package com.service;          public class StudentService {                  private String name = "matthew";              public String getName() {                 return name;             }              public String getName(String salution) {                 return salution + " " + name;             }              public void setName(String name) {                 this.name = name;             }              public void changeNameAndNotTellCache(String name) {                 this.name = name;             }          }

5、使用方法Clent

 

 package com.service;      import org.springframework.context.support.AbstractApplicationContext;     import org.springframework.context.support.ClassPathXmlApplicationContext;      public class TestCache {         public static void main(String[] args) {                   AbstractApplicationContext context;                   context = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");                   context.start();                                   StudentService ss = (StudentService) context.getBean("studentService");               n(name);                  //update cache                  System.out.println("清除缓存后,再次访问 ");                  ss.setName("Michael");                  name = ss.getName();                  System.out.println(name);                                   name = ss.getName("Mr");                  System.out.println(name);                                    context.close();                  }     }

 

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