From 73d8f069fe660baac35b7ec437296998b5dcbfe8 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Mon, 30 Dec 2013 12:52:35 +0100 Subject: [PATCH] EhCacheFactoryBean does not call set(Sampled)StatisticsEnabled on EhCache 2.7/2.8 Issue: SPR-11265 --- .../cache/ehcache/EhCacheFactoryBean.java | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) 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 26e4059420b..1aed482cfbf 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 @@ -37,6 +37,7 @@ import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; +import org.springframework.util.ClassUtils; /** * {@link FactoryBean} that creates a named EhCache {@link net.sf.ehcache.Cache} instance @@ -63,6 +64,11 @@ import org.springframework.beans.factory.InitializingBean; */ public class EhCacheFactoryBean extends CacheConfiguration implements FactoryBean, BeanNameAware, InitializingBean { + // EhCache's setStatisticsEnabled(boolean) available? Not anymore as of EhCache 2.7... + private static final boolean setStatisticsAvailable = + ClassUtils.hasMethod(Ehcache.class, "setStatisticsEnabled", boolean.class); + + protected final Log logger = LogFactory.getLog(getClass()); private CacheManager cacheManager; @@ -188,7 +194,9 @@ public class EhCacheFactoryBean extends CacheConfiguration implements FactoryBea /** * Set whether to enable EhCache statistics on this cache. - * @see net.sf.ehcache.Cache#setStatisticsEnabled + *

Note: As of EhCache 2.7, statistics are enabled by default, and cannot be turned off. + * This setter therefore has no effect in such a scenario. + * @see net.sf.ehcache.Ehcache#setStatisticsEnabled */ public void setStatisticsEnabled(boolean statisticsEnabled) { this.statisticsEnabled = statisticsEnabled; @@ -196,7 +204,9 @@ public class EhCacheFactoryBean extends CacheConfiguration implements FactoryBea /** * Set whether to enable EhCache's sampled statistics on this cache. - * @see net.sf.ehcache.Cache#setSampledStatisticsEnabled + *

Note: As of EhCache 2.7, statistics are enabled by default, and cannot be turned off. + * This setter therefore has no effect in such a scenario. + * @see net.sf.ehcache.Ehcache#setSampledStatisticsEnabled */ public void setSampledStatisticsEnabled(boolean sampledStatisticsEnabled) { this.sampledStatisticsEnabled = sampledStatisticsEnabled; @@ -263,11 +273,14 @@ public class EhCacheFactoryBean extends CacheConfiguration implements FactoryBea this.cacheManager.addCache(rawCache); } - if (this.statisticsEnabled) { - rawCache.setStatisticsEnabled(true); - } - if (this.sampledStatisticsEnabled) { - rawCache.setSampledStatisticsEnabled(true); + // Only necessary on EhCache <2.7: As of 2.7, statistics are on by default. + if (setStatisticsAvailable) { + if (this.statisticsEnabled) { + rawCache.setStatisticsEnabled(true); + } + if (this.sampledStatisticsEnabled) { + rawCache.setSampledStatisticsEnabled(true); + } } if (this.disabled) { rawCache.setDisabled(true);