Spring3.1和hibernate4 + eh-cache

时间:2012-10-26 09:09:31

标签: spring hibernate ehcache

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

调度-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'的引用' CacheManager的“;嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为'ehcache'的bean     在

请尽快建议任何解决方案。

提前致谢

1 个答案:

答案 0 :(得分:5)

这个配置对我来说很合适:

<强> META-INF /弹簧/沙箱缓存-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();
    }
}