EhCacheManager bean始终返回Null

问题描述

你好,所以我试图创建EhCacheManager并配置Bean,以便可以将其注入我的代码

问题是,每当我想使用Bean来获取缓存时(在ehcache.xml中预先配置),我总是有一个空指针

我的EhCacheManager bean

<bean class="org.springframework.cache.ehcache.EhCacheCacheManager" id="MyDefaultCacheManager">
    <property name="cacheManager">
        <bean class="org.springframework.cache.ehcache.EhCacheManagerfactorybean">
            <property name="configLocation" value="classpath:/ehcache.xml"/>
        </bean>
    </property>
</bean>

ehcache.xml

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
    monitoring="autodetect" dynamicConfig="true">

    <!-- By default,Ehcache stored the cached files in temp folder. -->
    <!-- <diskStore path="java.io.tmpdir" /> -->

    <!-- Ask Ehcache to store cache in this path -->
    <diskStore path="c:\\cache" />

    <!-- Sample cache named cache1
    This cache contains a maximum in memory of 10000 elements,and will expire
    an element if it is idle for more than 5 minutes and lives for more than
    10 minutes.

    If there are more than 10000 elements it will overflow to the
    disk cache,which in this configuration will go to wherever java.io.tmp is
    defined on your system. On a standard Linux system this will be /tmp" -->
    <cache name="simpleCache"
        maxEntriesLocalHeap="10000"
        maxEntriesLocaldisk="1000"
        eternal="false"
        diskSpoolBufferSizeMB="20"
        timetoIdleSeconds="300" timetoLiveSeconds="600"
        memoryStoreevictionPolicy="LFU"
        transactionalMode="off">
        <persistence strategy="localTempSwap" />
    </cache>

</ehcache>

注入豆

@Autowired
EhCacheCacheManager  ehCacheManager ;

解决方法

尝试这个

<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
    <property name="cacheManager" ref="ehcache"/>
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
    <property name="configLocation" value="classpath:/ehcache.xml"/>
    <property name="shared" value="true"/>
</bean>

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...