From 76b9d0d243a6f17eca5a198a713682fa41e0d909 Mon Sep 17 00:00:00 2001 From: Martin Theiss Date: Sat, 2 Jun 2018 11:46:18 +0200 Subject: [PATCH 1/2] Retrieve javax.cache.CacheManager using Bean ClassLoader This commit uses the bean's classloader to retrieve the CacheManager to prevent issues with off-heap serialization. See gh-13338 --- .../cache/JCacheCacheConfiguration.java | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java index 06c2033c3aa..acb202ad115 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java @@ -26,6 +26,7 @@ import javax.cache.Caching; import javax.cache.configuration.MutableConfiguration; import javax.cache.spi.CachingProvider; +import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.ObjectProvider; import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; import org.springframework.boot.autoconfigure.condition.ConditionMessage; @@ -61,7 +62,7 @@ import org.springframework.util.StringUtils; @Conditional({ CacheCondition.class, JCacheCacheConfiguration.JCacheAvailableCondition.class }) @Import(HazelcastJCacheCustomizationConfiguration.class) -class JCacheCacheConfiguration { +class JCacheCacheConfiguration implements BeanClassLoaderAware { private final CacheProperties cacheProperties; @@ -73,6 +74,8 @@ class JCacheCacheConfiguration { private final List cachePropertiesCustomizers; + private ClassLoader beanClassLoader; + JCacheCacheConfiguration(CacheProperties cacheProperties, CacheManagerCustomizers customizers, ObjectProvider> defaultCacheConfiguration, @@ -113,9 +116,9 @@ class JCacheCacheConfiguration { .resolveConfigLocation(this.cacheProperties.getJcache().getConfig()); if (configLocation != null) { return cachingProvider.getCacheManager(configLocation.getURI(), - cachingProvider.getDefaultClassLoader(), properties); + this.beanClassLoader, properties); } - return cachingProvider.getCacheManager(null, null, properties); + return cachingProvider.getCacheManager(null, this.beanClassLoader, properties); } private CachingProvider getCachingProvider(String cachingProviderFqn) { @@ -151,6 +154,11 @@ class JCacheCacheConfiguration { } } + @Override + public void setBeanClassLoader(ClassLoader classLoader) { + this.beanClassLoader = classLoader; + } + /** * Determine if JCache is available. This either kicks in if a provider is available * as defined per {@link JCacheProviderAvailableCondition} or if a From c67aedd8bcbaa4ff8be1383fc3d95bcb2711f68d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 4 Jun 2018 14:08:41 +0200 Subject: [PATCH 2/2] Polish "Retrieve javax.cache.CacheManager using Bean ClassLoader" Closes gh-13338 --- .../cache/JCacheCacheConfiguration.java | 12 ++++++------ .../cache/CacheAutoConfigurationTests.java | 10 ++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java index acb202ad115..246b6166a01 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -88,6 +88,11 @@ class JCacheCacheConfiguration implements BeanClassLoaderAware { this.cachePropertiesCustomizers = cachePropertiesCustomizers.getIfAvailable(); } + @Override + public void setBeanClassLoader(ClassLoader classLoader) { + this.beanClassLoader = classLoader; + } + @Bean public JCacheCacheManager cacheManager(CacheManager jCacheCacheManager) { JCacheCacheManager cacheManager = new JCacheCacheManager(jCacheCacheManager); @@ -154,11 +159,6 @@ class JCacheCacheConfiguration implements BeanClassLoaderAware { } } - @Override - public void setBeanClassLoader(ClassLoader classLoader) { - this.beanClassLoader = classLoader; - } - /** * Determine if JCache is available. This either kicks in if a provider is available * as defined per {@link JCacheProviderAvailableCondition} or if a diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java index 940093ea038..3d083367eb1 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java @@ -374,6 +374,16 @@ public class CacheAutoConfigurationTests { "spring.cache.jcache.config=" + configLocation); } + @Test + public void jCacheCacheUseBeanClassLoader() { + String cachingProviderFqn = MockCachingProvider.class.getName(); + load(DefaultCacheConfiguration.class, "spring.cache.type=jcache", + "spring.cache.jcache.provider=" + cachingProviderFqn); + JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class); + assertThat(cacheManager.getCacheManager().getClassLoader()) + .isEqualTo(this.context.getClassLoader()); + } + @Test public void ehcacheCacheWithCaches() { load(DefaultCacheConfiguration.class, "spring.cache.type=ehcache");