Spring mvc and memcache

1.5k Views Asked by At

I am using memcached to act according to what they've said in the address: https://code.google.com/p/simple-spring-memcached/wiki/Getting_Started . The memcache on the server where it is done , I've installed . The library should be required to have your project . But when running the output appears.

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

i use caching for method such this

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

and in dispatcher-servlet.xml add this

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

Why is this a problem ?

************************* after change this error occur ******************************

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

There are 2 best solutions below

0
On

I do not use com.google.code.ssm.CacheFactory but the error says :

NoClassDefFoundError: org/codehaus/jackson/Versioned

It says there is a dependance to Jackson json library 1.x, and there is none in classpath. Try to add Jackson 1.x library and see what happens ...

16
On

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