|
38 | 38 | import org.springframework.beans.factory.FactoryBean;
|
39 | 39 | import org.springframework.beans.factory.InitializingBean;
|
40 | 40 | import org.springframework.util.Assert;
|
| 41 | +import org.springframework.util.ClassUtils; |
41 | 42 |
|
42 | 43 | /**
|
43 | 44 | * {@link FactoryBean} that creates a named EhCache {@link net.sf.ehcache.Cache} instance
|
|
64 | 65 | */
|
65 | 66 | public class EhCacheFactoryBean implements FactoryBean<Ehcache>, BeanNameAware, InitializingBean {
|
66 | 67 |
|
| 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 | + |
67 | 73 | protected final Log logger = LogFactory.getLog(getClass());
|
68 | 74 |
|
69 | 75 | private CacheManager cacheManager;
|
@@ -275,15 +281,19 @@ public void setCacheEventListeners(Set<CacheEventListener> cacheEventListeners)
|
275 | 281 |
|
276 | 282 | /**
|
277 | 283 | * 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 |
279 | 287 | */
|
280 | 288 | public void setStatisticsEnabled(boolean statisticsEnabled) {
|
281 | 289 | this.statisticsEnabled = statisticsEnabled;
|
282 | 290 | }
|
283 | 291 |
|
284 | 292 | /**
|
285 | 293 | * 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 |
287 | 297 | */
|
288 | 298 | public void setSampledStatisticsEnabled(boolean sampledStatisticsEnabled) {
|
289 | 299 | this.sampledStatisticsEnabled = sampledStatisticsEnabled;
|
@@ -346,11 +356,14 @@ public void afterPropertiesSet() throws CacheException, IOException {
|
346 | 356 | this.cacheManager.addCache(rawCache);
|
347 | 357 | }
|
348 | 358 |
|
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 | + } |
354 | 367 | }
|
355 | 368 | if (this.disabled) {
|
356 | 369 | rawCache.setDisabled(true);
|
|
0 commit comments