ctdsy520 2013-04-12 15:10
浏览 354
已采纳

ehcache+springAOP配置不成功?

刚接触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 (当然还有其他错,但是我觉得 这个是罪魁祸首)

  • 写回答

3条回答 默认 最新

  • jinnianshilongnian 2013-04-12 15:13
    关注

    Could not instantiate bean class [org.springframework.cache.ehcache.EhCacheFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

    缺少slf4j的jar包

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程