Ehcache作为JCache实现在Spring中配置

时间:2012-06-13 12:28:37

标签: java spring ehcache jcache

我正在尝试使用以下jcache-ehcache库作为包装器,以便我可以使用Ecache作为我的JCache实现。

这些是我的专家依赖:

    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
        <version>2.1.0</version>
        <type>pom</type>
    </dependency>

    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache-jcache</artifactId>
        <version>1.4.0-beta1</version>
    </dependency>

在我的Spring配置文件中,我有以下bean:

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="shared" value="true"/>
</bean>


<bean id="userCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
    <property name="cacheName" value="userCache"/>
    <property name="cacheManager" ref="cacheManager"/>
    <property name="diskPersistent" value="false"/>
</bean>

<bean id="jcacheUserCache" class="net.sf.ehcache.jcache.JCache">
    <constructor-arg index="0" ref="userCache"/>
</bean>

我的Ehcache.xml(在类路径根目录下)文件包含userCache区域定义:

  <cache name="userCache" maxElementsInMemory="10000"
  maxElementsOnDisk="0" eternal="false" overflowToDisk="false"
  diskSpoolBufferSizeMB="20" timeToIdleSeconds="0"
  timeToLiveSeconds="0" memoryStoreEvictionPolicy="LFU"
  statistics = "true">
  </cache>

初始化时,我收到以下错误:

Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jcacheUserCache' defined in class path resource [application-context.xml]: Unsatisfied dependency expressed through constructor argument with index 1 of type [net.sf.ehcache.jcache.JCacheManager]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?

任何人都可以提供有关如何正确初始化此jCacheUserCache bean的帮助吗?

由于

1 个答案:

答案 0 :(得分:0)

net.sf.ehcache.jcache.JCache的构造函数有三个参数,但在创建jcacheUserCache bean时只提供了第一个参数。您得到的错误是关于缺少的第二个参数(类型net.sf.ehcache.jcache.JCacheManager)。

JCache的构造函数如下所示:

public JCache(Ehcache ehcache, JCacheManager cacheManager, ClassLoader classLoader) {
    // ...
}

因此,您还需要提供JCacheManagerClassLoader作为构造函数参数。

(见JCache.java here