【问题标题】:Configuring EHCache for Spring3.1.1 and Hibernate为 Spring3.1.1 和 Hibernate 配置 EHCache
【发布时间】:2012-03-27 01:34:12
【问题描述】:

我正在尝试使用 Hibernate 3.5.5 在现有 Spring 3.1.1 应用程序中启用对象缓存。我正在使用 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 添加了必要的依赖项。但是现在我收到了这个错误:

bean 初始化失败;
嵌套异常是 org.springframework.beans.ConversionNotSupportedException:
无法将类型“java.lang.String”的属性值转换为所需类型
属性“cacheManager”的“net.sf.ehcache.CacheManager”;
嵌套异常是 java.lang.IllegalStateException:
无法将类型 [java.lang.String] 的值转换为所需类型
[net.sf.ehcache.CacheManager] 用于属性“cacheManager”:
未找到匹配的编辑器或转换策略

有人知道是什么原因造成的吗?

【问题讨论】:

  • 你的应该可以工作...如果有帮助,这是我的工作配置:
  • @aweigold 谢谢。这似乎出于某种原因。我错过了属性元素。您为什么不添加您的评论作为答案,以便我接受它。

标签: java spring hibernate ehcache


【解决方案1】:

您需要以不同的方式引用您的 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"/>

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      包含以下依赖项

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

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

        • 谢谢@Bogdan,我已经修正了错字。
        猜你喜欢
        • 2015-04-28
        • 1970-01-01
        • 2012-08-07
        • 2017-05-11
        • 1970-01-01
        • 2016-06-05
        • 2011-03-24
        • 2014-08-23
        相关资源
        最近更新 更多