问题描述
刚接触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"><beans> <!-- 引用ehCache的配置 --> <bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation"> <value>ehcache.xml</value> </property> </bean> <!-- 定义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> --></beans>上面配置中的两个拦截器 在工具类中都写有相关实体 beanMethodCacheInterceptor.javaMethodCacheAfterAdvice.javaspringContext.xml(切入点配置):<import resource="cacheContext.xml"/><bean id="testServiceTarget" class="hqhop.scada.daos.res.impl.ResDaoImpl"/> <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 (当然还有其他错,但是我觉得 这个是罪魁祸首) 问题补充:加了这个jar 又出另外一个 出错了。。Cannot convert value of type [$Proxy5 implementing net.sf.ehcache.terracotta.InternalEhcache,net.sf.ehcache.store.StoreListener,org.springframework.aop.SpringProxy,org.springframework.aop.framework.Advised] to required type [net.sf.ehcache.Cache] for property 'cache': no matching editors or conversion strategy found
解决方案
Could not instantiate bean class [org.springframework.cache.ehcache.EhCacheFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory 缺少slf4j的jar包
解决方案二:
送你一个配置参考;http://blog.csdn.net/linfanhehe/article/details/7690684http://blog.csdn.net/soleghost/article/details/640562下载http://download.csdn.net/detail/charles0902/3455916
解决方案三:
slf4j的jar包在路径中么