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条)

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)