Tarun Gupta
Tarun Gupta

Reputation: 1242

Spring3.1 and hibernate4 + eh-cache

I am using Spring3.1 and hibernate4 for my web application. Here i am trying for eh cache but getting some error, here is my configuration that i have used:-

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>

and here is the dependency:-

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

I am getting the following error:--

SEVERE: Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0': Cannot resolve reference to bean 'cacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cacheManager' defined in ServletContext resource [/WEB-INF/dispatcher-servlet.xml]: Cannot resolve reference to bean 'ehcache' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'ehcache' is defined at

Please suggest any solution ASAP.

Thanks In Advance

Upvotes: 1

Views: 4638

Answers (1)

Roadrunner
Roadrunner

Reputation: 6801

This configuration worked just fine for me:

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

Upvotes: 5

Related Questions