java - Spring Ehcache3 导致键类型和值类型异常

标签 java spring ehcache jcache

我尝试在带有 spring 4.3 的项目上使用 ehcache3。 我配置了缓存管理器:

<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
        <property name="cacheManager">
            <bean class="org.springframework.cache.jcache.JCacheManagerFactoryBean">
               <property name="cacheManagerUri" value="classpath:ehcache.xml"/>
            </bean>
        </property>
</bean>

ehcache.xml:

<config
        xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
        xmlns='http://www.ehcache.org/v3'
        xmlns:jsr107='http://www.ehcache.org/v3/jsr107'
        xsi:schemaLocation="
        http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.0.xsd
        http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.0.xsd" >
    <service>
        <jsr107:defaults enable-statistics="true" enable-management="true"/>
    </service>
    <cache alias="customerSettings">
        <key-type>java.lang.Long</key-type>
        <expiry>
            <none/>
        </expiry>
        <resources>
            <heap>500</heap>
        </resources>
    </cache>
</config>

但是当我部署项目时,出现异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in ServletContext resource [/WEB-INF/spring/root-context.xml]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Cache [customerSettings] specifies key/value types. Use getCache(String, Class, Class)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
    ... 100 more
Caused by: java.lang.IllegalArgumentException: Cache [customerSettings] specifies key/value types. Use getCache(String, Class, Class)
    at org.ehcache.jsr107.Eh107CacheManager.getCache(Eh107CacheManager.java:297)
    at org.springframework.cache.jcache.JCacheCacheManager.loadCaches(JCacheCacheManager.java:105)
    at org.springframework.cache.support.AbstractCacheManager.initializeCaches(AbstractCacheManager.java:61)
    at org.springframework.cache.support.AbstractCacheManager.afterPropertiesSet(AbstractCacheManager.java:50)
    at org.springframework.cache.jcache.JCacheCacheManager.afterPropertiesSet(JCacheCacheManager.java:97)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)
    ... 107 more

如果我删除:

<key-type>java.lang.Long</key-type>

工作正常,但是缓存的 keyType 是 Object, 我需要做什么才能使用自己的键类型和值类型?

最佳答案

Spring cache is not typed ,所以它没有使用 Jcache (javax.cache / JSR-107 caching API) 的类型化 API

现在,由于您在 ehcache.xml 中指定了类型,Ehcache 拒绝让 Spring 使用 getCache() 的非类型签名

当你想一想,如果你让 Spring 使用 Ehcache ( via @CacheResult and other JCache annotations for example ),你必须让它为你选择什么是键和值类型 - 不再是你应该指定类型。

关于java - Spring Ehcache3 导致键类型和值类型异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45737109/

相关文章:

java - ehCache缓存有自动刷新的选项吗?没有任何调度程序工作?

java - Spring:获取 ManyToOne 实体时,引用实体 (OneToMany) 未显示在 JSON 中

java - 在Spring Batch中根据标题读取Excel文件列

java - 无法阻止 ant 生成编译器 Sun 专有 API 警告

java.lang.NoClassDefFoundError : android. support.v4.app.NavUtilsJB 在Android Studio中添加新模块时出错

java - 如何在 Spring 中从上下文设置静态最终参数?

caching - overFlowToDisk何时在EHCACHE中被激活?

java - 使用 hibernate BeanCreationException 配置 ehcache

windows - 通过批处理文件设置默认 JRE 版本

java - 如何同时访问不同类的两个方法?