當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring2..5整合Ehacahe
生活随笔
收集整理的這篇文章主要介紹了
Spring2..5整合Ehacahe
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
?在整合的過程中真是報各種各樣的錯誤,其中最主要的就是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();??????????????????}?????}?
轉載于:https://my.oschina.net/u/2457218/blog/534764
總結
以上是生活随笔為你收集整理的Spring2..5整合Ehacahe的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RDBMS vs. NoSQL C
- 下一篇: 《你不常用的c#之二》:略谈GCHand