diff --git a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java index d452c8ec40..6865dd217e 100644 --- a/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java +++ b/spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java @@ -234,6 +234,7 @@ public class EhCacheFactoryBean extends CacheConfiguration implements FactoryBea // Fetch cache region: If none with the given name exists, create one on the fly. Ehcache rawCache; boolean cacheExists = this.cacheManager.cacheExists(cacheName); + if (cacheExists) { if (logger.isDebugEnabled()) { logger.debug("Using existing EhCache cache region '" + cacheName + "'"); @@ -253,6 +254,12 @@ public class EhCacheFactoryBean extends CacheConfiguration implements FactoryBea rawCache.getCacheEventNotificationService().registerListener(listener); } } + + // Needs to happen after listener registration but before setStatisticsEnabled + if (!cacheExists) { + this.cacheManager.addCache(rawCache); + } + if (this.statisticsEnabled) { rawCache.setStatisticsEnabled(true); } @@ -263,9 +270,6 @@ public class EhCacheFactoryBean extends CacheConfiguration implements FactoryBea rawCache.setDisabled(true); } - if (!cacheExists) { - this.cacheManager.addCache(rawCache); - } Ehcache decoratedCache = decorateCache(rawCache); if (decoratedCache != rawCache) { this.cacheManager.replaceCacheWithDecoratedCache(rawCache, decoratedCache);