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

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

Spring Ehcache configuration : java.lang.IllegalStateException: error

问题

  1. pom.xml
  2. <properties>
  3. <jdk.version>1.8</jdk.version>
  4. <spring.framework.version>5.1.6.RELEASE</spring.framework.version>
  5. <spring.security.version>3.2.9.RELEASE</spring.security.version>
  6. <spring.integration.version>5.1.4.RELEASE</spring.integration.version>
  7. </properties>
  8. <dependency>
  9. <groupId>org.springframework</groupId>
  10. <artifactId>spring-context</artifactId>
  11. <version>${spring.security.version}</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.springframework</groupId>
  15. <artifactId>spring-context-support</artifactId>
  16. <version>${spring.security.version}</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>net.sf.ehcache</groupId>
  20. <artifactId>ehcache</artifactId>
  21. <version>2.10.6</version>
  22. </dependency>
  23. ehcache.xml
  24. <cache name="myList"
  25. overflowToDisk="false" statistics="true"
  26. eternal="false"
  27. maxElementsInMemory="10000"
  28. timeToIdleSeconds="0"
  29. timeToLiveSeconds="300"
  30. memoryStoreEvictionPolicy="LFU" />
  31. Java file
  32. public class MyClass
  33. {
  34. public final Map<String, String> getDepartmentsList(String userid) {
  35. System.out.println("I am getting the dept");
  36. Map<String, String> deptMap = myService.getAuthorizedDept(userid);
  37. return deptMap;
  38. }
  39. }
  40. @Service
  41. import org.springframework.cache.annotation.Cacheable;
  42. public class Myservice
  43. {
  44. @Cacheable(value = "myList", keyGenerator = "cacheKeyGenerator" )
  45. public Map<String, String> getDepartmentsList(String userid){
  46. }
  47. }
  48. cache-service.xml
  49. cacheManager configuration. I think mine is not correct and that's why I am getting the error. I am not sure what is wrong.
  50. <?xml version="1.0" encoding="UTF-8"?>
  51. <beans xmlns="http://www.springframework.org/schema/beans"
  52. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
  53. xsi:schemaLocation="http://www.springframework.org/schema/beans
  54. http://www.springframework.org/schema/beans/spring-beans.xsd
  55. http://www.springframework.org/schema/cache
  56. http://www.springframework.org/schema/cache/spring-cache.xsd">
  57. <cache:annotation-driven />
  58. <bean id="cacheManager"
  59. class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  60. <property name="shared" value="true" />
  61. </bean>
  62. <bean id="cacheKeyGenerator"
  63. class="test.util.StringArgumentCacheKeyGenerator" />
  64. </beans>
  65. ERROR Stack:
  66. Caused by: java.lang.IllegalStateException:Cannot convert value of type 'net.sf.ehcache.CacheManager' to required type 'org.springframework.cache.CacheManager' for property 'cacheManager':
  67. no matching editors or conversion strategy found
  68. org.springframework.beans.factory.BeanCreationException:
  69. Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0':
  70. Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException:
  71. 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

  1. &lt;properties&gt;
  2. &lt;jdk.version&gt;1.8&lt;/jdk.version&gt;
  3. &lt;spring.framework.version&gt;5.1.6.RELEASE&lt;/spring.framework.version&gt;
  4. &lt;spring.security.version&gt;3.2.9.RELEASE&lt;/spring.security.version&gt;
  5. &lt;spring.integration.version&gt;5.1.4.RELEASE&lt;/spring.integration.version&gt;
  6. &lt;/properties&gt;
  7. &lt;dependency&gt;
  8. &lt;groupId&gt;org.springframework&lt;/groupId&gt;
  9. &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
  10. &lt;version&gt;${spring.security.version}&lt;/version&gt;
  11. &lt;/dependency&gt;
  12. &lt;dependency&gt;
  13. &lt;groupId&gt;org.springframework&lt;/groupId&gt;
  14. &lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
  15. &lt;version&gt;${spring.security.version}&lt;/version&gt;
  16. &lt;/dependency&gt;
  17. &lt;dependency&gt;
  18. &lt;groupId&gt;net.sf.ehcache&lt;/groupId&gt;
  19. &lt;artifactId&gt;ehcache&lt;/artifactId&gt;
  20. &lt;version&gt;2.10.6&lt;/version&gt;
  21. &lt;/dependency&gt;

ehcache.xml

  1. &lt;cache name=&quot;myList&quot;
  2. overflowToDisk=&quot;false&quot; statistics=&quot;true&quot;
  3. eternal=&quot;false&quot;
  4. maxElementsInMemory=&quot;10000&quot;
  5. timeToIdleSeconds=&quot;0&quot;
  6. timeToLiveSeconds=&quot;300&quot;
  7. memoryStoreEvictionPolicy=&quot;LFU&quot; /&gt;

Java file

  1. public class MyClass
  2. {
  3. public final Map&lt;String, String&gt; getDepartmentsList(String userid) {
  4. System.out.println(&quot;I am getting the dept&quot;);
  5. Map&lt;String, String&gt; deptMap = myService.getAuthorizedDept(userid);
  6. return deptMap;
  7. }
  8. }
  9. @Service
  10. import org.springframework.cache.annotation.Cacheable;
  11. public class Myservice
  12. {
  13. @Cacheable(value = &quot;myList&quot;, keyGenerator = &quot;cacheKeyGenerator&quot; )
  14. public Map&lt;String, String&gt; getDepartmentsList(String userid){
  15. }
  16. }

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.

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  2. &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
  3. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:cache=&quot;http://www.springframework.org/schema/cache&quot;
  4. xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/cache
  7. http://www.springframework.org/schema/cache/spring-cache.xsd">
  8. &lt;cache:annotation-driven /&gt;
  9. &lt;bean id=&quot;cacheManager&quot;
  10. class=&quot;org.springframework.cache.ehcache.EhCacheManagerFactoryBean&quot;&gt;
  11. &lt;property name=&quot;shared&quot; value=&quot;true&quot; /&gt;
  12. &lt;/bean&gt;
  13. &lt;bean id=&quot;cacheKeyGenerator&quot;
  14. class=&quot;test.util.StringArgumentCacheKeyGenerator&quot; /&gt;
  15. &lt;/beans&gt;

ERROR Stack:

  1. 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;:
  2. no matching editors or conversion strategy found
  3. org.springframework.beans.factory.BeanCreationException:
  4. Error creating bean with name &#39;org.springframework.cache.interceptor.CacheInterceptor#0&#39;:
  5. Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException:
  6. 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 对我有用。

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/tx
  8. http://www.springframework.org/schema/tx/spring-tx.xsd
  9. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context.xsd
  13. http://www.springframework.org/schema/cache
  14. http://www.springframework.org/schema/cache/spring-cache.xsd">
  15. <cache:annotation-driven />
  16. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
  17. <property name="cacheManager">
  18. <ref local="ehcache" />
  19. </property>
  20. </bean>
  21. <bean id="ehcache"
  22. class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
  23. p:configLocation="classpath:ehcache.xml" />

英文:

The following cache-service.xml worked for me.

  1. &lt;beans xmlns=&quot;http://www.springframework.org/schema/beans&quot;
  2. xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
  3. xmlns:mvc=&quot;http://www.springframework.org/schema/mvc&quot; xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
  4. xmlns:cache=&quot;http://www.springframework.org/schema/cache&quot; xmlns:p=&quot;http://www.springframework.org/schema/p&quot;
  5. xsi:schemaLocation=&quot;http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/tx
  8. http://www.springframework.org/schema/tx/spring-tx.xsd
  9. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context.xsd
  13. http://www.springframework.org/schema/cache
  14. http://www.springframework.org/schema/cache/spring-cache.xsd">
  15. &lt;cache:annotation-driven /&gt;
  16. &lt;bean id=&quot;cacheManager&quot; class=&quot;org.springframework.cache.ehcache.EhCacheCacheManager&quot;&gt;
  17. &lt;property name=&quot;cacheManager&quot;&gt;
  18. &lt;ref local=&quot;ehcache&quot; /&gt;
  19. &lt;/property&gt;
  20. &lt;/bean&gt;
  21. &lt;bean id=&quot;ehcache&quot;
  22. class=&quot;org.springframework.cache.ehcache.EhCacheManagerFactoryBean&quot;
  23. 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:

确定