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

huangapple go评论76阅读模式
英文:

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
英文:

pom.xml

 &lt;properties&gt;
&lt;jdk.version&gt;1.8&lt;/jdk.version&gt;
&lt;spring.framework.version&gt;5.1.6.RELEASE&lt;/spring.framework.version&gt;
&lt;spring.security.version&gt;3.2.9.RELEASE&lt;/spring.security.version&gt;
&lt;spring.integration.version&gt;5.1.4.RELEASE&lt;/spring.integration.version&gt;
&lt;/properties&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-context&lt;/artifactId&gt;
&lt;version&gt;${spring.security.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.springframework&lt;/groupId&gt;
&lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
&lt;version&gt;${spring.security.version}&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;net.sf.ehcache&lt;/groupId&gt;
&lt;artifactId&gt;ehcache&lt;/artifactId&gt;
&lt;version&gt;2.10.6&lt;/version&gt;
&lt;/dependency&gt;

ehcache.xml

&lt;cache name=&quot;myList&quot; 
overflowToDisk=&quot;false&quot;  statistics=&quot;true&quot;
eternal=&quot;false&quot;
maxElementsInMemory=&quot;10000&quot;
timeToIdleSeconds=&quot;0&quot;
timeToLiveSeconds=&quot;300&quot;
memoryStoreEvictionPolicy=&quot;LFU&quot; /&gt;

Java file

     public class MyClass
{
public final Map&lt;String, String&gt; getDepartmentsList(String userid) {
System.out.println(&quot;I am getting the dept&quot;);
Map&lt;String, String&gt; deptMap = myService.getAuthorizedDept(userid);
return deptMap;
}
}
@Service 
import org.springframework.cache.annotation.Cacheable;
public class Myservice
{
@Cacheable(value = &quot;myList&quot;, keyGenerator = &quot;cacheKeyGenerator&quot; )
public Map&lt;String, String&gt; 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.

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:cache=&quot;http://www.springframework.org/schema/cache&quot;
xsi:schemaLocation=&quot;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">
&lt;cache:annotation-driven /&gt;
&lt;bean id=&quot;cacheManager&quot;
class=&quot;org.springframework.cache.ehcache.EhCacheManagerFactoryBean&quot;&gt;
&lt;property name=&quot;shared&quot; value=&quot;true&quot; /&gt;
&lt;/bean&gt;
&lt;bean id=&quot;cacheKeyGenerator&quot;
class=&quot;test.util.StringArgumentCacheKeyGenerator&quot; /&gt;
&lt;/beans&gt;

ERROR Stack:

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

答案1

得分: 0

以下的 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" />

英文:

The following cache-service.xml worked for me.

&lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot; xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
xmlns:cache=&quot;http://www.springframework.org/schema/cache&quot; xmlns:p=&quot;http://www.springframework.org/schema/p&quot;
xsi:schemaLocation=&quot;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">
&lt;cache:annotation-driven /&gt;
&lt;bean id=&quot;cacheManager&quot; class=&quot;org.springframework.cache.ehcache.EhCacheCacheManager&quot;&gt;
&lt;property name=&quot;cacheManager&quot;&gt;
&lt;ref local=&quot;ehcache&quot; /&gt;
&lt;/property&gt;
&lt;/bean&gt;
&lt;bean id=&quot;ehcache&quot;
class=&quot;org.springframework.cache.ehcache.EhCacheManagerFactoryBean&quot;
p:configLocation=&quot;classpath:ehcache.xml&quot; /&gt;

</beans>

huangapple
  • 本文由 发表于 2020年10月4日 10:05:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64190564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定