Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为Spring3.1.1和Hibernate配置EHCache_Java_Spring_Hibernate_Ehcache - Fatal编程技术网

Java 为Spring3.1.1和Hibernate配置EHCache

Java 为Spring3.1.1和Hibernate配置EHCache,java,spring,hibernate,ehcache,Java,Spring,Hibernate,Ehcache,我试图在现有的Spring3.1.1应用程序和Hibernate3.5.5中启用对象缓存。我正在使用ehcache 2.2.0。在我的applicationContext中,我添加了使用EHCache打开缓存的配置 <cache:annotation-driven /> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager

我试图在现有的Spring3.1.1应用程序和Hibernate3.5.5中启用对象缓存。我正在使用ehcache 2.2.0。在我的applicationContext中,我添加了使用EHCache打开缓存的配置

<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
    p:cache-manager="ehcache" />
<bean id="ehcache"
    class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
    p:config-location="ehcache.xml" />

然后,我创建了ehcache.xml文件:

<diskStore path="java.io.tmpdir" />

<defaultCache 
    eternal="false" 
    maxElementsInMemory="1000"
    overflowToDisk="false" 
    diskPersistent="false" 
    timeToIdleSeconds="0"
    timeToLiveSeconds="0" 
    memoryStoreEvictionPolicy="LRU"/>

<cache name="studentCache" 
    eternal="false"
    maxElementsInMemory="10000" 
    overflowToDisk="false" 
    diskPersistent="false"
    timeToIdleSeconds="0" 
    timeToLiveSeconds="0"
    memoryStoreEvictionPolicy="LRU" /> 

我在pom.xml文件中为ehcache添加了必要的依赖项。但现在我得到了这个错误:

Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'net.sf.ehcache.CacheManager' for property 'cacheManager'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [net.sf.ehcache.CacheManager] for property 'cacheManager': no matching editors or conversion strategy found bean初始化失败; 嵌套异常为org.springframework.beans.ConversionNotSupportedException: 无法将“java.lang.String”类型的属性值转换为所需类型 属性“CacheManager”的“net.sf.ehcache.CacheManager”; 嵌套异常为java.lang.IllegalStateException: 无法将[java.lang.String]类型的值转换为所需类型 [net.sf.ehcache.CacheManager]对于属性“CacheManager”: 找不到匹配的编辑器或转换策略
有人知道这是什么原因吗?

您需要以不同的方式引用cacheManager属性。这就是我的工作原理:

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

@aweigold的答案很完美,但如果您使用“p:cacheManager ref”传递“ehcache”bean的引用,则可以获得更清晰的解决方案


与上一篇文章相同,只是属性名称没有错误:

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehcache" />

包括以下依赖项

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.3.1</version>
</dependency>

net.sf.ehcache
ehcache内核
2.3.1

你的应该可以。。。这是我的工作配置,如果有帮助的话:@aweigold谢谢。这似乎出于某种原因起作用。我缺少属性元素。你为什么不添加你的评论作为答案,这样我就可以接受了。谢谢@Bogdan,我已经纠正了错误。
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.3.1</version>
</dependency>