Announcement Announcement Module
Collapse
No announcement yet.
Failed to convert property value of type [net.sf.ehcache.Cache] to required type Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Failed to convert property value of type [net.sf.ehcache.Cache] to required type

    Hi,

    We are configuring ehcache in our project using AOP and EhCache. The code is below :

    The file ehcache.xml is below:
    <ehcache>
    <diskStore path="java.io.tmpdir" />
    <defaultCache maxElementsInMemory="100"
    eternal="false"
    timeToIdleSeconds="500"
    timeToLiveSeconds="500"
    overflowToDisk="true"
    diskPersistent="false"
    diskExpiryThreadIntervalSeconds="500"
    />
    <cache name="org.sample.cache.LOOKUP_CACHE"
    maxElementsInMemory="300"
    overflowToDisk="true"
    eternal="false"
    timeToLiveSeconds="500"
    timeToIdleSeconds="0"
    diskPersistent="false"
    diskExpiryThreadIntervalSeconds="500"/>
    </ehcache>


    It is configured in the spring.xml as below :

    <bean id="cacheManager"
    class="org.springframework.cache.ehcache.EhCacheMa nagerFactoryBean">
    <property name="configLocation">
    <value>classpath:WEB-INF/ehcache.xml</value>
    </property>
    </bean>

    <bean id="methodCache"
    class="org.springframework.cache.ehcache.EhCacheFa ctoryBean">
    <property name="cacheManager">
    <ref local="cacheManager" />
    </property>
    <property name="cacheName">
    <value>org.sample.cache.LOOKUP_CACHE</value>
    </property>
    </bean>

    <bean id="methodCacheInterceptor"
    class="com.lookup.MethodCacheInterceptor">
    <property name="cache">
    <ref local="methodCache" />
    </property>
    <property name="caching">
    <value>true</value>
    </property>
    </bean>


    The java file MethodCacheInteceptor is below :

    public class MethodCacheInterceptor implements MethodInterceptor
    , InitializingBean {
    private Logger log = LoggerFactory.getLogger(MethodCacheInterceptor.cla ss);

    private Cache cache;

    /** * sets cache name to be used */
    public void setCache(Cache cache) {
    this.cache = cache;
    }

    public void afterPropertiesSet() throws Exception {
    Assert.notNull(cache,
    "A cache is required. Use setCache(Cache) to provide one.");
    }

    public Object invoke(MethodInvocation invocation) throws Throwable {
    String targetName = invocation.getThis().getClass().getName();
    String methodName = invocation.getMethod().getName();
    Object[] arguments = invocation.getArguments();
    Object result;
    log.debug("looking for method result in cache");
    String cacheKey = getCacheKey(targetName, methodName, arguments);
    Element element = cache.get(cacheKey);
    if (element == null) {
    // call target/sub-interceptor
    log.debug("calling intercepted method");
    result = invocation.proceed();
    // cache method result
    log.debug("caching result");
    element = new Element(cacheKey, (Serializable) result);
    cache.put(element);
    }
    return element.getValue();


    }

    /*** creates cache key: targetName.methodName.argument0.argument1... */
    private String getCacheKey(String targetName, String methodName, Object[] arguments) {
    StringBuffer sb = new StringBuffer();
    sb.append(targetName).append(".").append(methodNam e);
    if ((arguments != null) && (arguments.length != 0)) {
    for (int i = 0; i < arguments.length; i++) {
    sb.append(".").append(arguments[i]);
    }
    }
    return sb.toString();
    }
    }




    When I tried to run this,we are getting the following exception.


    org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'methodCacheInterceptor' defined in ServletContext resource [/WEB-INF/spring-ws-servlet.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [net.sf.ehcache.Cache] to required type [net.sf.ehcache.Cache] for property 'cache'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [net.sf.ehcache.Cache] to required type [net.sf.ehcache.Cache] for property 'cache': no matching editors or conversion strategy found
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:480)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory$1.run(AbstractAutowireC apableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(Access Controller.java:214)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:380)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 64)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:221)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:261 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:429)
    at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:729)
    at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:381)
    at org.springframework.web.servlet.FrameworkServlet.c reateWebApplicationContext(FrameworkServlet.java:4 02)
    at org.springframework.web.servlet.FrameworkServlet.i nitWebApplicationContext(FrameworkServlet.java:316 )
    at org.springframework.web.servlet.FrameworkServlet.i nitServletBean(FrameworkServlet.java:282)
    at org.springframework.web.servlet.HttpServletBean.in it(HttpServletBean.java:126)
    at javax.servlet.GenericServlet.init(GenericServlet.j ava:256)


    Any help is greatly appreciated.

  • #2
    Please use [ code][/code ] tags when posting code, that way it remains readable.

    Regarding your issue, check your classpath for duplicate classes/jar files.

    Comment


    • #3
      Hi,

      I checked for duplicate classpath variables. There are no duplicate classpath variables. But still the same exception is thrown again.

      Comment


      • #4
        I wasn't mentioning variables i was mentioning classes and jars... A class can be very well in a different jar file somewhere else on the classpath then you get classloading issues.

        Comment


        • #5
          Hi,

          I am duplicate jars with different versions in my project. It is working fine now.
          Thanks for your help.

          Comment

          Working...
          X