刚接触ehcache缓存框架,在网上看了一些例子(不过发现所有的例子都是那么一个,只是在不停地转载。真不知道那些转的人到底有没有实际操作过?)springAOP 拦截注入的时候报错,一时 不知道 怎么解决,大家来看看,交流下!
开始使用 springMVC注解的方式配置ehcache,发现虽然配置成功但是缓存不起作用,后面想想 还是用springAOP来试试。相关配置如下:
cacheContext.xml(拦截器配置):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<!-- 引用ehCache的配置 -->
ehcache.xml
<!-- 定义ehCache的工厂,并设置所使用的Cache name -->
<bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
<property name="cacheManager">
<ref local="defaultCacheManager"/>
</property>
<property name="cacheName">
<value>DEFAULT_CACHE</value>
</property>
</bean>
<!-- find/create cache拦截器 -->
<bean id="methodCacheInterceptor" class="ehcache.scada.utils.MethodCacheInterceptor">
<property name="cache">
<ref local="ehCache" />
</property>
</bean>
<!-- flush cache拦截器
<bean id="methodCacheAfterAdvice" class="ehcache.scada.utils.MethodCacheAfterAdvice">
<property name="cache">
<ref local="ehCache" />
</property>
</bean>-->
<bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="methodCacheInterceptor"/>
</property>
<property name="patterns">
<list>
<value>.*find.*</value>
<value>.*get.*</value>
</list>
</property>
</bean>
<!--
<bean id="methodCachePointCutAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice">
<ref local="methodCacheAfterAdvice"/>
</property>
<property name="patterns">
<list>
<value>.*save.*</value>
<value>.*insert.*</value>
<value>.*update.*</value>
<value>.*delete.*</value>
<value>.*remove.*</value>
</list>
</property>
</bean>
-->
上面配置中的两个拦截器 在工具类中都写有相关实体 bean
MethodCacheInterceptor.java
MethodCacheAfterAdvice.java
springContext.xml(切入点配置):
<bean id="testService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target">
<ref local="testServiceTarget"/>
</property>
<property name="interceptorNames">
<list>
<value>methodCachePointCut</value>
<!--<value>methodCachePointCutAdvice</value>-->
</list>
</property>
</bean>
并在web.xml中加载了 springContext.xml
也有ehcache相关的jar
最后启动项目 报错:
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.cache.ehcache.EhCacheFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory (当然还有其他错,但是我觉得 这个是罪魁祸首)