在整合的過程當中真是報各類各樣的錯誤,其中最主要的就是jar包衝突以及不全,因此此文檔中把所須要的必須jar都列了出來java
1,必須的jarnode
spring.jar spring-modules-cache.jar ehcache-core-2.4.6.jarspring
slf4j-api-1.5.6.jar slf4j-jdk-1.5.2.jar slf4-log4j-1.5.6.jarapi
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.jarapp
2,applicationContext.xml中的配置ide
<?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>
四、測試Servicethis
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; } }
五、使用方法Clentspa
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(); } }