首先介绍下 项目想达成的目的:
项目是SSH架构,对外提供3个webService接口BService、RService 、AService, 我Ehcache
框架对webService接口调用的结果进行缓存,大概意思如下:
如果调用 BService 、AService 两个接口中的方法 就对缓存进行移除
如果调用RService 接口的方法 ,判断缓存中是否有该方法返回的结果,如果有就直接从
Ehcache中返回,没有,就把结果放进Ehcache中,然后返回。
- 下面贴出来配置代码:
ehcache.xml
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true" monitoring="autodetect"
dynamicConfig="true">
<defaultCache
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false">
</defaultCache>
<cache name="testcache"
maxElementsInMemory="100"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
overflowToDisk="false"
/>
</ehcache>
ehcache-config.xml
<beans>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation">
<value>classpath:ehcache.xml</value>
</property>
</bean>
<bean id="methodCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="cacheManager"/>
</property>
<property name="cacheName">
<value>testcache</value>
</property>
</bean>
<!-- 创建cache拦截器 -->
<bean id="methodCacheInterceptor" class="com.zkasoft.referral.ehcache.filter.MethodCacheInterceptor">
<property name="cache">
<ref local="methodCache"/>
</property>
</bean>
<!-- 销毁cache拦截器 -->
<bean id="methodCacheAfterAdvice" class="com.zkasoft.referral.ehcache.filter.MethodCacheAfterAdvice">
<property name="cache">
<ref local="methodCache"/>
</property>
</bean>
<!-- 切点配置 -->
<bean id="methodCachePontCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="methodCacheInterceptor"/>
</property>
<property name="patterns">
<list>
<value>.*ReferralServiceI.referralRetrieve</value>
</list>
</property>
</bean>
<bean id="methodCacheDestory" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="methodCacheAfterAdvice"/>
</property>
<property name="patterns">
<list>
<value>.*BusinessAcceptedServiceI.businessAccepted</value>
</list>
</property>
</bean>
</beans>
然后是applicationContext-*.xml中 配置
<!--BusinessAcceptService是BService接口的实现类-->
<bean id="businessAcceptedService" class="org.springframework.aop.framework.ProxyFactoryBean" >
<property name="target">
<bean class="com.zkasoft.referral.ws.service.BusinessAcceptedServiceI">
<property name="commonDao">
<ref bean="commonDao"/>
</property>
</bean>
</property>
<property name="interceptorNames">
<list>
<value>methodCacheDestory</value>
</list>
</property>
</bean>
<!--referralAcceptedService是AService接口的实现类-->
<bean id="referralAcceptedService" class="org.springframework.aop.framework.ProxyFactoryBean" >
<property name="target">
<bean class="com.zkasoft.referral.ws.service.ReferralAcceptedServiceI">
<property name="commonDao">
<ref bean="commonDao"/>
</property>
</bean>
</property>
<property name="interceptorNames">
<list>
<value>methodCacheDestory</value>
</list>
</property>
</bean>
<!--referralAcceptedService是AService接口的实现类-->
<bean id="referralService" class="org.springframework.aop.framework.ProxyFactoryBean" >
<property name="target">
<bean class="com.zkasoft.referral.ws.service.ReferralServiceI">
<property name="commonDao">
<ref bean="commonDao"/>
</property>
</bean>
</property>
<property name="interceptorNames">
<list>
<value>methodCachePontCut</value>
</list>
</property>
</bean>
以上就是 基本的配置,然后我通过soap调用 BusinessAcceptService的方法,但是拦截器始终都不起作用。
恩恩 ,解决了,主要是我吧事物管理和 拦截代理放在一块了 封开就好了 如下: