ehcache-spring-annotations not working for me

295 views
Skip to first unread message

Claudio Santana

unread,
Nov 7, 2012, 2:38:04 PM11/7/12
to ehcache-sprin...@googlegroups.com
I have the following applicationContext.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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:task="http://www.springframework.org/schema/task" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
           http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">


    <context:annotation-config />
    <context:spring-configured />
    <context:load-time-weaver />

    <ehcache:annotation-driven cache-manager="ehCacheManager" />

    <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="cacheManagerName" value="SpringEhcacheExample1EhCacheManager"/>
    </bean>

    <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean">
    <property name="locateExistingServerIfPossible" value="true" />
</bean>
    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetClass" value="net.sf.ehcache.management.ManagementService" />
    <property name="targetMethod" value="registerMBeans" />
    <property name="arguments">
        <list>
            <ref bean="ehCacheManager" />
            <ref bean="mbeanServer" />
            <value>true</value>
            <value>true</value>
            <value>true</value>
            <value>true</value>
        </list>
    </property>
</bean>

. . .
</beans>


The content of my ehcache.xml

<ehcache updateCheck="false">

   <defaultCache
       maxElementsInMemory="20000"
       eternal="false"
       overflowToDisk="false"
       timeToIdleSeconds="600"
       timeToLiveSeconds="600"
       diskPersistent="false"
       memoryStoreEvictionPolicy="LRU">
   </defaultCache>
   

    <cache name="cachedMethod1" eternal="false"
        maxElementsInMemory="500" overflowToDisk="false" diskPersistent="false"
        timeToIdleSeconds="0" timeToLiveSeconds="1200"
        memoryStoreEvictionPolicy="LRU" />

    <cache name="cachedMethod2" eternal="false"
        maxElementsInMemory="500" overflowToDisk="false" diskPersistent="false"
        timeToIdleSeconds="0" timeToLiveSeconds="1200"
        memoryStoreEvictionPolicy="LRU" />

. . .
</ehcache>


I created an interface and implemented the methods I want to have cached, they look like this:

    @Override
    @Cacheable(cacheName="cachedMethod")
    public SomeObject getCachedMethod1(...) {
    ...
        return someObjects;
    }


In Tomcat's web.xml I'm instantiating Spring with

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/resources/spring/applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>



I am using Spring 3.0.6, EhCache core 1.7 and Tomcat 6.

I run JConsole and I can see the regions declared in ehcache.xml being created but when I run my code it always calls the method implementation rather than the cache.

Any ideas?

Nicholas Blair

unread,
Nov 7, 2012, 3:41:40 PM11/7/12
to ehcache-sprin...@googlegroups.com
Are the instances of whatever class implements "public SomeObject getCachedMethod1()" spring managed beans?
If so, are the collaborating classes that call your instance's getCachedMethod1 method receiving a reference to the annotated class via dependency injection?

Claudio Santana

unread,
Nov 7, 2012, 4:15:31 PM11/7/12
to ehcache-sprin...@googlegroups.com
The class exposing public SomeObject getCachedMethod1() is a Spring managed bean through @Service("myService") and is being injected with @Autowired dependency injection annotation.

Claudio Santana

unread,
Nov 7, 2012, 4:34:27 PM11/7/12
to ehcache-sprin...@googlegroups.com
I set up a breakpoint right on EhCacheInterceptor#invoke but it never gets called. Is there anything to configure regarding cut-points I'm missing ?

Eric Dalquist

unread,
Nov 9, 2012, 12:31:15 PM11/9/12
to ehcache-sprin...@googlegroups.com
And how are the classes using it referencing it? Any client of a class annotated with @Cacheable must use the interface that defines the annotated method.

Example:

class SomeInterface {
    ResultObject foo();
}

class SomeObject implements SomeInterface {
    @Cacheable
    public ResultObject foo() {
        return something;
    }
}

class SomeClient {
    @Autowired
    private SomeInterface si;

    //call si.foo() in here
Reply all
Reply to author
Forward
0 new messages