title: Spring 3.1 @Cacheable - method still executed
body: <p>I'm trying to implement Spring 3.1 caching as explained <a href="http://www.javacodegeeks.com/2011/02/spring-31-cache-abstraction-tutorial.html" rel="noreferrer" title="here">here</a> and <a href="http://blog.springsource.org/2011/02/23/spring-3-1-m1-caching/" rel="noreferrer">here</a>, but it doesn't seem to be working: my method is run through every time even though it is marked @cacheable. What am I doing wrong?</p>
  
  <p>I've moved it into a junit test case with its own configuration file to isolate it from the rest of my application, but the problem still happens. Here are the relevant files:</p>
  
  <p>Spring-test-servlet.xml</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:cache="http://www.springframework.org/schema/cache"
     xmlns:p="http://www.springframework.org/schema/p"
     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd"&gt;
  &lt;cache:annotation-driven /&gt;
  
  &lt;bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cache-manager-ref="ehcache"/&gt;
  &lt;bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
        p:config-location="classpath:ehcache.xml"/&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>ehcache.xml</p>
  
  <pre><code>&lt;ehcache&gt;
  &lt;diskStore path="java.io.tmpdir"/&gt;
  &lt;cache name="cache"
         maxElementsInMemory="100"
         eternal="false"
         timeToIdleSeconds="120"
         timeToLiveSeconds="120"
         overflowToDisk="true"
         maxElementsOnDisk="10000000"
         diskPersistent="false"
         diskExpiryThreadIntervalSeconds="120"
         memoryStoreEvictionPolicy="LRU"/&gt;
  
  &lt;/ehcache&gt;
  </code></pre>
  
  <p>MyTest.java</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration({"classpath:spring-test-servlet.xml"})
  @Component
  public class MyTest extends TestCase {
  
      @Test
      public void testCache1(){
          for(int i = 0; i &lt; 5; i++){
              System.out.println("Calling someMethod...");
              System.out.println(someMethod(0));
          }
      }
  
      @Cacheable("testmethod")
      private int someMethod(int val){
          System.out.println("Not from cache");
          return 5;
      }
  }
  </code></pre>
  
  <p>Relevant Pom entries: (spring-version = 3.1.1.RELEASE)</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;net.sf.ehcache&lt;/groupId&gt;
          &lt;artifactId&gt;ehcache-core&lt;/artifactId&gt;
          &lt;version&gt;2.5.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-context-support&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework&lt;/groupId&gt;
          &lt;artifactId&gt;spring-test&lt;/artifactId&gt;
          &lt;version&gt;${spring.version}&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>when I run the test, Spring puts out some debug messages that looks like my cache is initialized without errors</p>
  
  <pre><code>DEBUG: config.ConfigurationHelper - No CacheManagerEventListenerFactory class specified. Skipping...
  DEBUG: ehcache.Cache - No BootstrapCacheLoaderFactory class specified. Skipping...
  DEBUG: ehcache.Cache - CacheWriter factory not configured. Skipping...
  DEBUG: config.ConfigurationHelper - No CacheExceptionHandlerFactory class specified. Skipping...
  DEBUG: store.MemoryStore - Initialized net.sf.ehcache.store.MemoryStore for cache
  DEBUG: disk.DiskStorageFactory - Failed to delete file cache.data
  DEBUG: disk.DiskStorageFactory - Failed to delete file cache.index
  DEBUG: disk.DiskStorageFactory - Matching data file missing (or empty) for index file. Deleting index file /var/folders/qg/xwdvsg6x3mx_z_rcfvq7lc0m0000gn/T/cache.index
  DEBUG: disk.DiskStorageFactory - Failed to delete file cache.index
  DEBUG: ehcache.Cache - Initialised cache: cache
  DEBUG: config.ConfigurationHelper - CacheDecoratorFactory not configured. Skipping for 'cache'.
  DEBUG: config.ConfigurationHelper - CacheDecoratorFactory not configured for defaultCache. Skipping for 'cache'.
  </code></pre>
  
  <p>but the debug output shows no cache checks between method calls to someMethod and the print statement from inside someMethod prints every time. </p>
  
  <p>Is there something I'm missing?</p>