Spring mvc和memcache

时间:2014-10-27 08:26:33

标签: spring spring-mvc memcached simple-spring-memcached

我正在使用memcached按照他们在地址中所说的内容行事:https://code.google.com/p/simple-spring-memcached/wiki/Getting_Started。我已经安装了服务器上的内存缓存。应该要求图书馆拥有您的项目。但是当运行时会出现输出。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name  'defaultCache' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]:   Instantiation of bean failed; nested exception is  org.springframework.beans.BeanInstantiationException: Could not instantiate bean class  [com.google.code.ssm.CacheFactory]: Constructor threw exception; nested exception is  java.lang.NoClassDefFoundError: org/codehaus/jackson/Versioned
 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBea         n(AbstractAutowireCapableBeanFactory.java:965)


org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.google.code.ssm.CacheFactory]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/codehaus/jackson/Versioned

我使用缓存这样的方法

  @ReadThroughSingleCache(namespace = "CplxObj", expiration = 0)
public List<Answer> showAnswer(int id){

并在dispatcher-servlet.xml中添加此

  <cache:annotation-driven />

 <bean name="cacheManager" class="com.google.code.ssm.spring.SSMCacheManager">
 <property name="caches">
   <set>
     <bean class="com.google.code.ssm.spring.SSMCache">
       <constructor-arg name="cache" index="0" ref="defaultCache" />
       <!-- 5 minutes -->
       <constructor-arg name="expiration" index="1" value="300" />
      <!-- @CacheEvict(..., "allEntries" = true) won't work because allowClear is  false, 
        so we won't flush accidentally all entries from memcached instance -->
       <constructor-arg name="allowClear" index="2" value="false" />
     </bean>
   </set>
 </property>

  <bean name="defaultCache" class="com.google.code.ssm.CacheFactory">
   <property name="cacheName" value="defaultCache" />
  <property name="cacheClientFactory">
    <bean name="cacheClientFactory"  class="com.google.code.ssm.providers.xmemcached.MemcacheClientFactoryImpl" />
  </property>
  <property name="addressProvider">
   <bean class="com.google.code.ssm.config.DefaultAddressProvider">
     <property name="address" value="199.26.84.24:11211" />
   </bean>
 </property>
 <property name="configuration">
   <bean class="com.google.code.ssm.providers.CacheConfiguration">
     <property name="consistentHashing" value="true" />
   </bean>
 </property>
 </bean>

为什么这是一个问题?

*************************更改后发生此错误***************** *************

Error creating bean with name 'cacheManager' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Cannot create inner bean 'com.google.code.ssm.spring.SSMCache#b53b32' of type  [com.google.code.ssm.spring.SSMCache]  while setting bean property 'caches' with key [0];   
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.google.code.ssm.spring.SSMCache#b53b32' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Cannot resolve reference to bean 'defaultCache' while setting constructor argument; 
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'defaultCache' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Cannot create inner bean 'cacheClientFactory' of type [com.google.code.ssm.providers.spymemcached.MemcacheClientFactoryImpl] while setting bean property 'cacheClientFactory'; 
nested exception is org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [com.google.code.ssm.providers.spymemcached.MemcacheClientFactoryImpl] for bean with name 'cacheClientFactory' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]; 
nested exception is java.lang.ClassNotFoundException: com.google.code.ssm.providers.spymemcached.MemcacheClientFactoryImpl

Error creating bean with name 'com.google.code.ssm.spring.SSMCache#b53b32' defined  in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Cannot resolve reference to bean 'defaultCache' while setting constructor argument;

2 个答案:

答案 0 :(得分:0)

我不使用com.google.code.ssm.CacheFactory,但错误上写着:

NoClassDefFoundError: org/codehaus/jackson/Versioned

它说杰克逊json库1.x是依赖的,并且在类路径中没有。尝试添加Jackson 1.x库,看看会发生什么......

答案 1 :(得分:0)

您使用的是什么版本的Simple Spring Memcached(SSM)?最新版本需要Jackson 2.x,所以只需将SSM更新为 3.5.0

如果您使用maven来管理您的依赖项,则会自动下载所需的工件。

BTW要缓存showAnswer方法,您必须使用@ParameterValueKeyProvider注释其中一个方法参数:

@ReadThroughSingleCache(namespace = "answers", expiration = 0)
public List<Answer> showAnswer(@ParameterValueKeyProvider int id){

并确保此方法由另一个spring bean调用,自我调用(通过this)无法工作(不会被截获/缓存)。