EhCacheFactoryBean does not call set(Sampled)StatisticsEnabled on EhCache 2.7/2.8

Issue: SPR-11265
(cherry picked from commit 73d8f06)
This commit is contained in:
Juergen Hoeller 2014-01-01 19:18:12 +01:00
parent 5dddb492b8
commit f477a024ad
1 changed files with 20 additions and 7 deletions

View File

@ -38,6 +38,7 @@ import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
/** /**
* {@link FactoryBean} that creates a named EhCache {@link net.sf.ehcache.Cache} instance * {@link FactoryBean} that creates a named EhCache {@link net.sf.ehcache.Cache} instance
@ -64,6 +65,11 @@ import org.springframework.util.Assert;
*/ */
public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware, InitializingBean { public class EhCacheFactoryBean implements FactoryBean<Ehcache>, 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()); protected final Log logger = LogFactory.getLog(getClass());
private CacheManager cacheManager; private CacheManager cacheManager;
@ -275,7 +281,9 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
/** /**
* Set whether to enable EhCache statistics on this cache. * Set whether to enable EhCache statistics on this cache.
* @see net.sf.ehcache.Cache#setStatisticsEnabled * <p>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) { public void setStatisticsEnabled(boolean statisticsEnabled) {
this.statisticsEnabled = statisticsEnabled; this.statisticsEnabled = statisticsEnabled;
@ -283,7 +291,9 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
/** /**
* Set whether to enable EhCache's sampled statistics on this cache. * Set whether to enable EhCache's sampled statistics on this cache.
* @see net.sf.ehcache.Cache#setSampledStatisticsEnabled * <p>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) { public void setSampledStatisticsEnabled(boolean sampledStatisticsEnabled) {
this.sampledStatisticsEnabled = sampledStatisticsEnabled; this.sampledStatisticsEnabled = sampledStatisticsEnabled;
@ -346,11 +356,14 @@ public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware,
this.cacheManager.addCache(rawCache); this.cacheManager.addCache(rawCache);
} }
if (this.statisticsEnabled) { // Only necessary on EhCache <2.7: As of 2.7, statistics are on by default.
rawCache.setStatisticsEnabled(true); if (setStatisticsAvailable) {
} if (this.statisticsEnabled) {
if (this.sampledStatisticsEnabled) { rawCache.setStatisticsEnabled(true);
rawCache.setSampledStatisticsEnabled(true); }
if (this.sampledStatisticsEnabled) {
rawCache.setSampledStatisticsEnabled(true);
}
} }
if (this.disabled) { if (this.disabled) {
rawCache.setDisabled(true); rawCache.setDisabled(true);