Spring Ehcache配置:java.lang.IllegalStateException:错误

问题描述

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

     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配置。我认为我的说法不正确,这就是为什么我得到错误。我不知道怎么了。

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

错误堆栈:

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 

解决方法

以下 cache-service.xml 对我有用。

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...