1

我正在为我的 Web 应用程序使用Spring3.1hibernate4 。在这里,我正在尝试使用eh 缓存,但出现了一些错误,这是我使用的配置:-

调度程序-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:cache="http://www.springframework.org/schema/cache"
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">

    <cache:annotation-driven />
    <bean id="defaultEhCacheManager"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
        p:config-location="/WEB-INF/ehcache.xml" p:shared="false"></bean>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcache"></property>
    </bean>
    <cache:annotation-driven cache-manager="cacheManager" />
    <bean id="cacheManager">
        <property name="cacheManager" ref="ehcache" />
    </bean>
    <bean id="ehcache"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="/WEB-INF/ehcache.xml" />
    </bean> 

ehcache.xml

<cache name="sampleCache1" 
    maxElementsInMemory="10000"
    eternal="false"
    overflowToDisk="true"
    timeToIdleSeconds="300"
    timeToLiveSeconds="600">
</cache>

这是依赖项:-

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

我收到以下错误:--

严重:上下文初始化失败org.springframework.beans.factory.BeanCreationException:创建名为“org.springframework.cache.interceptor.CacheInterceptor#0”的bean时出错:设置bean属性“cacheManager”时无法解析对bean“cacheManager”的引用;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建 ServletContext 资源 [/WEB-INF/dispatcher-servlet.xml] 中定义的名称为“cacheManager”的 bean 时出错:设置 bean 属性时无法解析对 bean 'ehcache' 的引用'缓存管理器'; 嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'ehcache' is defined at

请尽快提出任何解决方案。

提前致谢

4

1 回答 1

5

这个配置对我来说很好用:

META-INF/spring/sandbox-cache-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:c="http://www.springframework.org/schema/c"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">

    <bean id="ehCacheManager"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
        p:configLocation="classpath:META-INF/ehcache.xml"
        p:shared="false" />

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

    <cache:annotation-driven cache-manager="cacheManager" />

</beans>

META-INF/ehcache.xml

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

    <cache name="sampleCache1"
        maxElementsInMemory="10000"
        eternal="false"
        overflowToDisk="true"
        timeToIdleSeconds="300"
        timeToLiveSeconds="600" />

    <defaultCache
        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="120"
        timeToLiveSeconds="120"
        maxElementsOnDisk="10000000"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU">
        <persistence strategy="localTempSwap"/>
    </defaultCache>
</ehcache>

CacheContextTest.java

import static org.fest.assertions.Assertions.assertThat;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:META-INF/spring/sandbox-cache-context.xml")
public class CacheContextTest {

    @Autowired
    CacheManager cacheManager;

    @Test
    public void shouldStartContext() throws Exception {
        assertThat(cacheManager.getCache("sampleCache1")).isNotNull();
        assertThat(cacheManager.getCache("nonExistentCache")).isNull();
    }
}
于 2012-10-28T15:43:50.510 回答