1

我正在使用 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;
4

2 回答 2

0

What version of Simple Spring Memcached (SSM) do you use? Latest versions require Jackson 2.x so just update SSM to 3.5.0.

If you use maven to manage your dependencies are required artifacts will be downloaded automatically.

BTW To cache showAnswer method you have to annotate one of method parameters with @ParameterValueKeyProvider:

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

And make sure that this method is invoked by another spring bean, self invocations (through this) don't work (are not intercepted / cached).

于 2014-10-27T19:28:42.597 回答
0

我不使用com.google.code.ssm.CacheFactory,但错误说:

NoClassDefFoundError: org/codehaus/jackson/Versioned

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

于 2014-10-27T09:02:55.670 回答