From b1b7f8ab1fe349c4d59819ed204b0c3e2552f879 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Wed, 16 Jul 2014 16:02:03 +0200 Subject: [PATCH] EhCacheFactoryBean uses reflection to call setStatisticsEnabled method (which allows for building against EhCache 2.7+) Issue: SPR-11262 --- .../cache/ehcache/EhCacheFactoryBean.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 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 0a48df385d..6589dc875c 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 @@ -17,6 +17,7 @@ package org.springframework.cache.ehcache; import java.io.IOException; +import java.lang.reflect.Method; import java.util.Set; import net.sf.ehcache.Cache; @@ -38,6 +39,7 @@ import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.util.ClassUtils; +import org.springframework.util.ReflectionUtils; /** * {@link FactoryBean} that creates a named EhCache {@link net.sf.ehcache.Cache} instance @@ -64,8 +66,12 @@ import org.springframework.util.ClassUtils; 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); + private static final Method setStatisticsEnabledMethod = + ClassUtils.getMethodIfAvailable(Ehcache.class, "setStatisticsEnabled", boolean.class); + + // EhCache's setSampledStatisticsEnabled(boolean) available? Not anymore as of EhCache 2.7... + private static final Method setSampledStatisticsEnabledMethod = + ClassUtils.getMethodIfAvailable(Ehcache.class, "setSampledStatisticsEnabled", boolean.class); protected final Log logger = LogFactory.getLog(getClass()); @@ -271,14 +277,13 @@ public class EhCacheFactoryBean extends CacheConfiguration implements FactoryBea } // 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.statisticsEnabled && setStatisticsEnabledMethod != null) { + ReflectionUtils.invokeMethod(setStatisticsEnabledMethod, rawCache, true); } + if (this.sampledStatisticsEnabled && setSampledStatisticsEnabledMethod != null) { + ReflectionUtils.invokeMethod(setSampledStatisticsEnabledMethod, rawCache, true); + } + if (this.disabled) { rawCache.setDisabled(true); }