【问题标题】:Spring3.1 and hibernate4 + eh-cacheSpring 3.1 和 hibernate4 + ehcache
【发布时间】:2012-10-16 13:05:42
【问题描述】:

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

dispatcher-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 在

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

提前致谢

【问题讨论】:

    标签: spring hibernate ehcache


    【解决方案1】:

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

    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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      • 2015-07-31
      • 1970-01-01
      • 2012-02-01
      相关资源
      最近更新 更多