Spring3.1和hibernate4 + eh-cache

Tar*_*pta 1 spring hibernate ehcache

我在我的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> 
Run Code Online (Sandbox Code Playgroud)

ehcache.xml中

<cache name="sampleCache1" 
    maxElementsInMemory="10000"
    eternal="false"
    overflowToDisk="true"
    timeToIdleSeconds="300"
    timeToLiveSeconds="600">
</cache>
Run Code Online (Sandbox Code Playgroud)

这是依赖: -

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.5.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我收到以下错误: -

重度:上下文初始化失败org.springframework.beans.factory.BeanCreationException:错误创建名称为豆"org.springframework.cache.interceptor.CacheInterceptor#0":无法解析参考豆"的CacheManager"而设置的bean属性"的CacheManager"; 嵌套的例外是org.springframework.beans.factory.BeanCreationException:错误创建名称为豆"的CacheManager"在ServletContext的资源定义[/WEB-INF/dispatcher-servlet.xml]:无法解析参考豆"的Ehcache"而设置的bean属性'的CacheManager'; 嵌套的例外是org.springframework.beans.factory.NoSuchBeanDefinitionException:没有名为"的Ehcache"豆是指在确定

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

提前致谢

Roa*_*ner 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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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();
    }
}
Run Code Online (Sandbox Code Playgroud)