Search code examples
javaspringehcache

Spring Ehcache configuration : java.lang.IllegalStateException: error


pom.xml

 <properties>
    <jdk.version>1.8</jdk.version>
    <spring.framework.version>5.1.6.RELEASE</spring.framework.version>
    <spring.security.version>3.2.9.RELEASE</spring.security.version>
    <spring.integration.version>5.1.4.RELEASE</spring.integration.version>
</properties>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.security.version}</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>${spring.security.version}</version>
</dependency>

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.10.6</version>
</dependency>

ehcache.xml

<cache name="myList" 
    overflowToDisk="false"  statistics="true"
    eternal="false"
    maxElementsInMemory="10000"
    timeToIdleSeconds="0"
    timeToLiveSeconds="300"
    memoryStoreEvictionPolicy="LFU" />

Java file

     public class MyClass
  {
    
    public final Map<String, String> getDepartmentsList(String userid) {
        System.out.println("I am getting the dept");
        Map<String, String> deptMap = myService.getAuthorizedDept(userid);
        return deptMap;
    }
 }
 
@Service 
import org.springframework.cache.annotation.Cacheable;
public class Myservice
{
@Cacheable(value = "myList", keyGenerator = "cacheKeyGenerator" )
    public Map<String, String> getDepartmentsList(String userid){
    }
}

cache-service.xml cacheManager configuration. I think mine is not correct and that's why I am getting the error. I am not sure what is wrong.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/cache 
        http://www.springframework.org/schema/cache/spring-cache.xsd">
    <cache:annotation-driven />

    <bean id="cacheManager"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="shared" value="true" />
    </bean>

    <bean id="cacheKeyGenerator"
        class="test.util.StringArgumentCacheKeyGenerator" />
</beans>

ERROR Stack:

Caused by: java.lang.IllegalStateException:Cannot convert value of type 'net.sf.ehcache.CacheManager' to required type 'org.springframework.cache.CacheManager' for property 'cacheManager':
no matching editors or conversion strategy found
org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0':
 Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException:
 Failed to convert property value of type 'net.sf.ehcache.CacheManager' to required type 'org.springframework.cache.CacheManager' for property 'cacheManager';nested exception is java.lang.IllegalStateException: Cannot convert value of type 

Solution

  • The following cache-service.xml worked for me.

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
              http://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/tx 
              http://www.springframework.org/schema/tx/spring-tx.xsd
              http://www.springframework.org/schema/mvc 
              http://www.springframework.org/schema/mvc/spring-mvc.xsd
              http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context.xsd
              http://www.springframework.org/schema/cache
              http://www.springframework.org/schema/cache/spring-cache.xsd">
    
    <cache:annotation-driven />
    
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager">
            <ref local="ehcache" />
        </property>
    </bean>
    
    <bean id="ehcache"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
        p:configLocation="classpath:ehcache.xml" />