The annotation @Cacheable is disallowed for this location

3,416 views
Skip to first unread message

sajjanmanju

unread,
Jun 23, 2011, 10:29:17 AM6/23/11
to Ehcache Spring Annotations
Hi,

I am trying to implement ehCache (general purpose caching) using
spring annotations. But when I type @Cacheable in my class just
before the method, I am getting an error that: "The annotation
@Cacheable is disallowed for this location". I am using Spring 3.0.3
maven2. Any one faced same problem...?? And @Cacheable is supported in
spring 3.0.3 ???

Any help will be appreciated.

Thanks in advance.

Nicholas Blair

unread,
Jun 23, 2011, 10:32:05 AM6/23/11
to ehcache-sprin...@googlegroups.com

@cacheable is only valid on methods. Spring 3.0.x is fine.

Nicholas Blair

unread,
Jun 23, 2011, 10:37:18 AM6/23/11
to ehcache-sprin...@googlegroups.com

I should add that your class must also implement an interface, otherwise the package will not be able to proxy your annotated methods.

Manjunath Sajjan

unread,
Jun 23, 2011, 10:41:48 AM6/23/11
to ehcache-sprin...@googlegroups.com
Ya I have an interface, if i type @cacheable in interface, i am getting same problem.."The annotation

@Cacheable is disallowed for this location"

Of course I am using Spring 3.0.3...
--
-Manjunath Sajjan

Eric Dalquist

unread,
Jun 23, 2011, 11:01:10 AM6/23/11
to ehcache-sprin...@googlegroups.com
Can you share an example of your class and methods with @Cacheable?

Manjunath Sajjan

unread,
Jun 24, 2011, 12:26:23 AM6/24/11
to ehcache-sprin...@googlegroups.com
Thanks for your replies...

Here is my code. Also tried giving @Cacheable("RestCallCache") in place of @Cacheable, still getting same error.
//My interface
import javax.persistence.Cacheable;

import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;

public interface IRestClient {
    @Cacheable                                                            //Geting error at this line
    public ResponseEntity<?> callRest(HttpEntity<?> requestEntity,
            String ServiceUrl, Class<?> Classref);
}

//My Class implementation of interface
import javax.persistence.Cacheable;
public class RestClient implements IRestClient {
    @Cacheable                                                          //Geting error at this line also
    public ResponseEntity<?> callRest(HttpEntity<?> requestEntity,
            String ServiceUrl, Class<?> Classref) {
RestTemplate restTemplate = new RestTemplate();
//some more lines here;implementation details here;
}
}

pom.xml
<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
    <version>2.3.1</version>
    <type>pom</type>
</dependency>

dispatcher-servlet.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:p="http://www.springframework.org/schema/p"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:ehcache="http://www.springmodules.org/schema/ehcache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">
      
       <ehcache:config configLocation="classpath:ehcache.xml" />
</beans>

ehcache.xml file

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false">
  
    <!-- Location of persistent caches on disk -->
    <diskStore path="java.io.tmpdir/EhCacheSpringAnnotationsExampleApp" />

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

    <cache name="RestCallCache" eternal="false"
        maxElementsInMemory="100" overflowToDisk="false" diskPersistent="false"
        timeToIdleSeconds="0" timeToLiveSeconds="300"
        memoryStoreEvictionPolicy="LRU" />

</ehcache>
--
-Manjunath Sajjan

Eric Dalquist

unread,
Jun 24, 2011, 9:26:09 AM6/24/11
to ehcache-sprin...@googlegroups.com
Look at your import. You're using the Cacheable annotation that exists in JPA

-Eric

Manjunath Sajjan

unread,
Jun 24, 2011, 9:46:04 AM6/24/11
to ehcache-sprin...@googlegroups.com
Thanks Eric for replying.. I resolved that problem.
Now I am facing problem in caching..
I followed this link: http://code.google.com/p/ehcache-spring-annotations/wiki/UsingCacheable
But caching is not happening.. my cached method is getting called every time...
One more thing is, in Debug message the "Generated key" is different every time I call the cached function.
If you have any working example of maven type project, if you can share it would be helpful (as I am new to caching)..

Thanks in advance.
-Manju
--
-Manjunath Sajjan

Eric Dalquist

unread,
Jun 24, 2011, 10:23:47 AM6/24/11
to ehcache-sprin...@googlegroups.com
The default key generator relies on the hashCode method of each method argument. I'd check the HttpEntity impl to see if it's hashCode does what you expect.

-Eric
Reply all
Reply to author
Forward
0 new messages