1

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;
ragnor
  • 2,498
  • 1
  • 22
  • 25
Hadi J
  • 16,989
  • 4
  • 36
  • 62

2 Answers2

0

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

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
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).

ragnor
  • 2,498
  • 1
  • 22
  • 25
  • i changed my code and add SSM:3.5.0. to lib.now this error occur!! – Hadi J Oct 28 '14 at 06:17
  • Do you use maven? It seams that you don't use maven and didn't add all required dependencies because `ClassNotFoundException` occurred. To fix it add `spymemcached-provider` and other dependencies. – ragnor Oct 28 '14 at 06:53
  • no i don't use maven.but i add all required dependencies. – Hadi J Oct 28 '14 at 08:35
  • All errors that have already occurred , it will not appear . But it seems that no data is cached . To know or not to cache data using telnet Krdm.bh your opinion, what could be the reason ? – Hadi J Oct 28 '14 at 20:31
  • How do you invoke `showAnswer`? Do you call it using bean object e.g. `myBean.showAnswer(..)` or simply by `this.showAnswer(..)` or `showAnswer(...)` ? – ragnor Oct 29 '14 at 06:00
  • I invoke in this way: private ShowAnswerService showAnswerService; @Autowired public void setShowAnswerService(ShowAnswerService showAnswerService) { this.showAnswerService = showAnswerService; } showAnswerService.showAnswer(id); – Hadi J Oct 29 '14 at 07:57
  • It's a correct way of invoking the method. How have you verified that the method is not cached? – ragnor Oct 29 '14 at 08:08
  • I've checked with telnet. By command get key. For example, the method (showAnswer (id = 50 I checked. Get 50. But there is no output. – Hadi J Oct 29 '14 at 08:18
  • The data relating to an id in the database have changed and back method on the called id, but the previous data appears at the output. We do know that the show uses information from the browser cache or not? – Hadi J Oct 29 '14 at 08:32
  • Invoking get 50 on memcached to check if value is cached it's not correct because 50 it's not a key that is used to cache result of the method. Created key is more complicated and consist of namespace and some delimiters. An easy way to check if method is invoked only once or multiple time is to add System.out.println("In showAnswer method with arg " + id) inside showAnswer method and check you logs. – ragnor Oct 29 '14 at 08:37
  • If the cache is not what could be the reason? – Hadi J Oct 29 '14 at 14:06
  • my problem not solved. please help me. i dont know do.I handle the way that you tell and caching not done. – Hadi J Oct 29 '14 at 17:25
  • If the cache is used, cache ID should not be re-run and the data is extracted from the cache.But every time I want to read data that has been cached, the method runs again.Is my reasoning is correct? – Hadi J Oct 29 '14 at 18:53
  • If the cache is used, cache ID should not be re-run and the data is extracted from the cache.But every time I want to read data that has been cached, the method runs again.Is my reasoning is correct? – Hadi J Oct 29 '14 at 19:57
  • Yes, it is. If you call `showAnswerService.showAnswer(17)` and then again `showAnswerService.showAnswer(17)` then second call shouldn't invoke body of the method but return data from cache. If you use other id than `17` e.g. `129` then the method will be invoked again. To see how SSM is working enable log4j logging by setting `INFO` level on `com.google.code.ssm.aop` – ragnor Oct 30 '14 at 06:20
  • I don't know do really. my problem not solved yet. please help me! – Hadi J Nov 03 '14 at 11:58