Showing posts with label ehcache. Show all posts
Showing posts with label ehcache. Show all posts

Friday, October 11, 2013

Migrating ehcache to spring 3.2.3

Spring 3.2.3 offered new cache annotations and xml constructions. Make sure you use
org.springframework.cache.annotation.Cacheable instead of com.googlecode.ehcache.annotations.Cacheable. The correct xml initialization:


    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcache"/>
    </bean>

    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
        <property name="shared" value="true"/>
    </bean>

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

This must be placed in contextLoader part of spring congiguration (specified by contextConfigLocation parameter in web.xml), not in webapp part! cache namespace:
xmlns:cache="http://www.springframework.org/schema/cache", location: http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd

Also add dependency on 'net.sf.ehcache:ehcache:2.7.4' instead of 'com.googlecode.ehcache-spring-annotations:ehcache-spring-annotations:1.2.0', 'cause we will use spring's annotation.