From 1fa8b1ac91f31fe727f836bb7791c0b35a2ea0eb Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 30 Dec 2016 14:19:03 +0100 Subject: [PATCH 1/2] Defend against lambda transaction customizers Update `CacheManagerCustomizers` to deal directly with `ClassCastException` assuming that they are because a customizer is implemented using a lambda. Closes gh-7788 --- .../cache/CacheManagerCustomizers.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheManagerCustomizers.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheManagerCustomizers.java index a821e76276..fb76047a0d 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheManagerCustomizers.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheManagerCustomizers.java @@ -21,6 +21,9 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.cache.CacheManager; @@ -38,6 +41,8 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator; */ class CacheManagerCustomizers implements ApplicationContextAware { + private static final Log logger = LogFactory.getLog(CacheManagerCustomizers.class); + private ConfigurableApplicationContext applicationContext; /** @@ -53,11 +58,27 @@ class CacheManagerCustomizers implements ApplicationContextAware { cacheManager); AnnotationAwareOrderComparator.sort(customizers); for (CacheManagerCustomizer customizer : customizers) { - customizer.customize(cacheManager); + customize(cacheManager, customizer); } return cacheManager; } + @SuppressWarnings({ "unchecked", "rawtypes" }) + private void customize(CacheManager cacheManager, + CacheManagerCustomizer customizer) { + try { + customizer.customize(cacheManager); + } + catch (ClassCastException ex) { + // Possibly a lambda-defined listener which we could not resolve the generic + // event type for + if (logger.isDebugEnabled()) { + logger.debug("Non-matching transaction manager type for customizer: " + + customizer, ex); + } + } + } + @SuppressWarnings({ "unchecked", "rawtypes" }) private List> findCustomizers( CacheManager cacheManager) { From 7b494cf31928e3d8d90674f4ace2e943f5de6884 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 30 Dec 2016 15:59:17 +0100 Subject: [PATCH 2/2] Polish See gh-7793 --- .../boot/autoconfigure/cache/CacheManagerCustomizers.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheManagerCustomizers.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheManagerCustomizers.java index fb76047a0d..716a1ebdf6 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheManagerCustomizers.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheManagerCustomizers.java @@ -70,10 +70,10 @@ class CacheManagerCustomizers implements ApplicationContextAware { customizer.customize(cacheManager); } catch (ClassCastException ex) { - // Possibly a lambda-defined listener which we could not resolve the generic + // Possibly a lambda-defined customizer which we could not resolve the generic // event type for if (logger.isDebugEnabled()) { - logger.debug("Non-matching transaction manager type for customizer: " + logger.debug("Non-matching cache manager type for customizer: " + customizer, ex); } }