Announcement Announcement Module
Collapse
No announcement yet.
Java based configuration for Ehcache based caching not working. Please help. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Java based configuration for Ehcache based caching not working. Please help.

    I am using java annotation based configuration for initializing ehcache based caching, with Spring 3.1.

    Here is the sample code...

    @Configuration
    @EnableCaching
    public class EhcacheConfig implements CachingConfigurer {
    .....

    @Bean
    public CacheManager cacheManager() {
    .....
    EhCacheManagerFactoryBean bean = new EhCacheManagerFactoryBean();
    bean.setCacheManagerName(CACHE_MANAGER);
    bean.setShared(Boolean.TRUE);
    File file = new File(property + Constants.Slash + EHCACHE_XML);
    bean.setConfigLocation(new FileSystemResource(file));

    try {
    bean.afterPropertiesSet();
    } catch (Exception e) {
    throw new RuntimeException(e);
    }
    EhCacheCacheManager cm = new EhCacheCacheManager();
    cm.setCacheManager(bean.getObject());
    return cm;
    }

    public KeyGenerator keyGenerator() {
    return new DefaultKeyGenerator();
    }
    }

    There is a valid ehcache.xml with 1 cache declared in it.

    This is all the configuration that I have for initializing ehcache with Spring. There is no XML based initialization in the application.

    At runtime, I have noticed that cacheManager() is initialized, as expected. After its successful execution, the code fails to complete the initialization by erring out in:

    CachingInterceptor.afterPropertiesSet() ->

    if (this.cacheManager == null) {
    throw new IllegalStateException("'cacheManager' is required");
    }


    I have done some investigation.

    It appears that the problem occurs when CachingInterceptor is being initialized by ProxyCachingConfiguration.

    ProxyCachingConfiguration is derived from AbstractCachingConfiguration.

    AbstractCachingConfiguration has a method called:

    @PostConstruct
    protected void reconcileCacheManager()

    This method is not invoked. Had it been invoked, the cacheManager instantiated in EhcacheConfig.cacheManger() would have been setup correctly for used by the CacheInterceptor.afterPropertiesSet().

    I do not understand the reason why reconcileCacheManager() is not invoked before CacheInterceptor.afterPropertiesSet() is invoked.

    Am I missing something? Can some one help me with the problem that I am facing?

    Thank you.

    Dhruv

  • #2
    I had the same problem as you, vauge "cacheManager is required" message.

    Your post gave me enough info to work out where we were going wrong. You need to have the class javax.annotation.PostConstruct on your classpath. In practise this means you're using java5 and don't have the JSR250 jar on your classpath.

    Originally posted by dhpd View Post
    @PostConstruct
    protected void reconcileCacheManager()

    This method is not invoked.

    Comment

    Working...
    X