title: @Cachebale not working with Ehcache and spring MVC Ehcache not working with Spring Caching Annotation
body: <p>My application is Spring MVC application. Tried using Spring Annotation based Caching. But it not working. Please refer my code below </p>
  
  <pre><code>                1. pom.xml
                  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
                      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
                      &lt;groupId&gt;com.giggal.spring&lt;/groupId&gt;
                      &lt;artifactId&gt;spring-tutorial-mvc&lt;/artifactId&gt;
                      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
                      &lt;packaging&gt;war&lt;/packaging&gt;
                  ......
                  &lt;dependencies&gt;
                          &lt;dependency&gt;
                              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
                              &lt;version&gt;4.3.4.RELEASE&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;4.3.1.RELEASE&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;4.3.4.RELEASE&lt;/version&gt;
                          &lt;/dependency&gt;
                          &lt;dependency&gt;
                              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                              &lt;artifactId&gt;spring-beans&lt;/artifactId&gt;
                              &lt;version&gt;4.3.4.RELEASE&lt;/version&gt;
                          &lt;/dependency&gt;
                          &lt;dependency&gt;
                              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                              &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
                              &lt;version&gt;4.3.4.RELEASE&lt;/version&gt;
                          &lt;/dependency&gt;
                          &lt;dependency&gt;
                              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
                              &lt;version&gt;4.3.4.RELEASE&lt;/version&gt;
                          &lt;/dependency&gt;
                          &lt;dependency&gt;
                              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                              &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
                              &lt;version&gt;4.3.4.RELEASE&lt;/version&gt;
                          &lt;/dependency&gt;
                          &lt;!-- Servlet --&gt;
                          &lt;dependency&gt;
                              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
                              &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
                              &lt;version&gt;2.5&lt;/version&gt;
                              &lt;scope&gt;provided&lt;/scope&gt;
                          &lt;/dependency&gt;
                          &lt;dependency&gt;
                              &lt;groupId&gt;javax.servlet.jsp&lt;/groupId&gt;
                              &lt;artifactId&gt;jsp-api&lt;/artifactId&gt;
                              &lt;version&gt;2.1&lt;/version&gt;
                              &lt;scope&gt;provided&lt;/scope&gt;
                          &lt;/dependency&gt;
                          &lt;dependency&gt;
                              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
                              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
                              &lt;version&gt;1.2&lt;/version&gt;
                          &lt;/dependency&gt;
                          &lt;!-- EHCache --&gt;
                          &lt;dependency&gt;
                              &lt;groupId&gt;net.sf.ehcache&lt;/groupId&gt;
                              &lt;artifactId&gt;ehcache&lt;/artifactId&gt;
                              &lt;version&gt;2.10.2.2.21&lt;/version&gt;
                          &lt;/dependency&gt;
  
                      &lt;/dependencies&gt;
                  &lt;/project&gt;
  
  2. My Spring app consists of two context xml config files, the first one app-ctx.xml only scans non-@Controller annotated beans:
  
              &lt;beans xmlns="http://www.springframework.org/schema/beans"
                  xmlns:context="http://www.springframework.org/schema/context"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
                  xmlns:aop="http://www.springframework.org/schema/aop" 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.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-3.1.xsd
                 http://www.springframework.org/schema/cache
                 http://www.springframework.org/schema/cache/spring-cache.xsd"&gt;
  
                  &lt;context:component-scan base-package="com.giggal.spring"&gt;
                      &lt;context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation" /&gt;
                  &lt;/context:component-scan&gt;
                  &lt;bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"&gt;
                      &lt;property name="cacheManager" ref="ehcache" /&gt;
                  &lt;/bean&gt;
                  &lt;bean id="ehcache"
                      class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"&gt;
                      &lt;property name="configLocation" value="classpath:ehcache.xml" /&gt;
                      &lt;property name="shared" value="true" /&gt;
                  &lt;/bean&gt;
  
              .....
  
              &lt;/beans&gt;
  
  3. The second one mvc-config.xml contains all spring-mvc setup and scans @Controller annotated beans
  
                  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
                  &lt;beans xmlns="http://www.springframework.org/schema/beans"
                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
                      xmlns:context="http://www.springframework.org/schema/context"
                      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
                          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;
  
                      &lt;context:component-scan base-package="com.giggal.spring" use-default-filters="false"&gt;
                          &lt;context:include-filter expression="org.springframework.stereotype.Controller"
                              type="annotation" /&gt;
                      &lt;/context:component-scan&gt;
  
  
                      &lt;mvc:annotation-driven /&gt;
                      &lt;!-- 2. HandlerMapping : Used default handler mapping internally --&gt;
  
                      &lt;!-- 3. ViewResolver --&gt;
                      &lt;bean
                          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
                          &lt;!-- Example: a logical view name of 'showMessage' is mapped to '/WEB-INF/jsp/showMessage.jsp' --&gt;
                          &lt;property name="prefix" value="/WEB-INF/view/" /&gt;
                          &lt;property name="suffix" value=".jsp" /&gt;
                      &lt;/bean&gt;
  
                  &lt;/beans&gt;
  
  4. The ehcache.xml used for caching with cache name emp :
  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"&gt;
      &lt;defaultCache eternal="true" maxElementsInMemory="100"
          overflowToDisk="false" /&gt;
      &lt;cache name="emp" maxElementsInMemory="10000" eternal="true"
          overflowToDisk="false" /&gt;
  &lt;/ehcache&gt;
  
  
  5. The controller class UserController
  
  @Controller
  @RequestMapping(value = "/user/")
  public class UserController {
  @Autowired
      UserService userService; // Service which will do all data
                                  // retrieval/manipulation work
  
  @RequestMapping(value = "/getAllUsers/", method = RequestMethod.GET)
      public ModelAndView listAllUsers() {
          ModelAndView mav = new ModelAndView("user/user_list");
          List&lt;User&gt; users = userService.findAllUsers();
          mav.addObject("users", users);
          return mav;
      }
  
  ......
  }
  
  6. UserService class:
  
      @Service("userService")
      @Transactional
      public class UserServiceImpl implements UserService {
  
          private static final AtomicLong counter = new AtomicLong();
  
          private static List&lt;User&gt; users;
  
          static {
              users = populateDummyUsers();
          }
  
          @Cacheable("emp") 
          public List&lt;User&gt; findAllUsers() {
              System.out.println("execute getEmployee method..");
              return users;
          }  
      ....
      }
  </code></pre>
  
  <p>But the findAllUsers() method call never gets cached, I always get sysout "execute getEmployee method.." everytime. What may cause this? There's definitely something I don't yet understand about caching annotations.</p>
  
  <pre><code>But the same code is working as below
  1. 
  @Configuration
  @EnableCaching
  @ComponentScan({ "com.giggal.*" })
  public class AppConfig {
  
      @Bean
      public CacheManager cacheManager() {
          return new EhCacheCacheManager(ehCacheCacheManager().getObject());
      }
  
      @Bean
      public EhCacheManagerFactoryBean ehCacheCacheManager() {
          EhCacheManagerFactoryBean cmfb = new EhCacheManagerFactoryBean();
          cmfb.setConfigLocation(new ClassPathResource("ehcache.xml"));
          cmfb.setShared(true);
          return cmfb;
      }
  }
  
  ---
  @Controller
  @RequestMapping(value = "/user/")
  public class UserController {
  
  
  
  @RequestMapping(value = "/getAllUsers/", method = RequestMethod.GET)
      public ModelAndView listAllUsers() {
             AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(com.giggal.spring.AppConfig.class);
              UserService obj = (UserService) context.getBean("userService");
              List&lt;User&gt; users = obj.findAllUsers();
          ModelAndView mav = new ModelAndView("user/user_list");
          //List&lt;User&gt; users = userService.findAllUsers();
          mav.addObject("users", users);
          return mav;
      }
  ..}
  </code></pre>
  
  <p>Please suggest the mechanism to work with annotations</p>