Skip to content

Commit f477a02

Browse files
committed
EhCacheFactoryBean does not call set(Sampled)StatisticsEnabled on EhCache 2.7/2.8
Issue: SPR-11265 (cherry picked from commit 73d8f06)
1 parent 5dddb49 commit f477a02

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

spring-context-support/src/main/java/org/springframework/cache/ehcache/EhCacheFactoryBean.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.springframework.beans.factory.FactoryBean;
3939
import org.springframework.beans.factory.InitializingBean;
4040
import org.springframework.util.Assert;
41+
import org.springframework.util.ClassUtils;
4142

4243
/**
4344
* {@link FactoryBean} that creates a named EhCache {@link net.sf.ehcache.Cache} instance
@@ -64,6 +65,11 @@
6465
*/
6566
public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware, InitializingBean {
6667

68+
// EhCache's setStatisticsEnabled(boolean) available? Not anymore as of EhCache 2.7...
69+
private static final boolean setStatisticsAvailable =
70+
ClassUtils.hasMethod(Ehcache.class, "setStatisticsEnabled", boolean.class);
71+
72+
6773
protected final Log logger = LogFactory.getLog(getClass());
6874

6975
private CacheManager cacheManager;
@@ -275,15 +281,19 @@ public void setCacheEventListeners(Set<CacheEventListener> cacheEventListeners)
275281

276282
/**
277283
* Set whether to enable EhCache statistics on this cache.
278-
* @see net.sf.ehcache.Cache#setStatisticsEnabled
284+
* <p>Note: As of EhCache 2.7, statistics are enabled by default, and cannot be turned off.
285+
* This setter therefore has no effect in such a scenario.
286+
* @see net.sf.ehcache.Ehcache#setStatisticsEnabled
279287
*/
280288
public void setStatisticsEnabled(boolean statisticsEnabled) {
281289
this.statisticsEnabled = statisticsEnabled;
282290
}
283291

284292
/**
285293
* Set whether to enable EhCache's sampled statistics on this cache.
286-
* @see net.sf.ehcache.Cache#setSampledStatisticsEnabled
294+
* <p>Note: As of EhCache 2.7, statistics are enabled by default, and cannot be turned off.
295+
* This setter therefore has no effect in such a scenario.
296+
* @see net.sf.ehcache.Ehcache#setSampledStatisticsEnabled
287297
*/
288298
public void setSampledStatisticsEnabled(boolean sampledStatisticsEnabled) {
289299
this.sampledStatisticsEnabled = sampledStatisticsEnabled;
@@ -346,11 +356,14 @@ public void afterPropertiesSet() throws CacheException, IOException {
346356
this.cacheManager.addCache(rawCache);
347357
}
348358

349-
if (this.statisticsEnabled) {
350-
rawCache.setStatisticsEnabled(true);
351-
}
352-
if (this.sampledStatisticsEnabled) {
353-
rawCache.setSampledStatisticsEnabled(true);
359+
// Only necessary on EhCache <2.7: As of 2.7, statistics are on by default.
360+
if (setStatisticsAvailable) {
361+
if (this.statisticsEnabled) {
362+
rawCache.setStatisticsEnabled(true);
363+
}
364+
if (this.sampledStatisticsEnabled) {
365+
rawCache.setSampledStatisticsEnabled(true);
366+
}
354367
}
355368
if (this.disabled) {
356369
rawCache.setDisabled(true);

0 commit comments

Comments
 (0)