Skip to content

Commit 76b9d0d

Browse files
mtheisssnicoll
authored andcommitted
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
1 parent f745f20 commit 76b9d0d

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import javax.cache.configuration.MutableConfiguration;
2727
import javax.cache.spi.CachingProvider;
2828

29+
import org.springframework.beans.factory.BeanClassLoaderAware;
2930
import org.springframework.beans.factory.ObjectProvider;
3031
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
3132
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
@@ -61,7 +62,7 @@
6162
@Conditional({ CacheCondition.class,
6263
JCacheCacheConfiguration.JCacheAvailableCondition.class })
6364
@Import(HazelcastJCacheCustomizationConfiguration.class)
64-
class JCacheCacheConfiguration {
65+
class JCacheCacheConfiguration implements BeanClassLoaderAware {
6566

6667
private final CacheProperties cacheProperties;
6768

@@ -73,6 +74,8 @@ class JCacheCacheConfiguration {
7374

7475
private final List<JCachePropertiesCustomizer> cachePropertiesCustomizers;
7576

77+
private ClassLoader beanClassLoader;
78+
7679
JCacheCacheConfiguration(CacheProperties cacheProperties,
7780
CacheManagerCustomizers customizers,
7881
ObjectProvider<javax.cache.configuration.Configuration<?, ?>> defaultCacheConfiguration,
@@ -113,9 +116,9 @@ private CacheManager createCacheManager() throws IOException {
113116
.resolveConfigLocation(this.cacheProperties.getJcache().getConfig());
114117
if (configLocation != null) {
115118
return cachingProvider.getCacheManager(configLocation.getURI(),
116-
cachingProvider.getDefaultClassLoader(), properties);
119+
this.beanClassLoader, properties);
117120
}
118-
return cachingProvider.getCacheManager(null, null, properties);
121+
return cachingProvider.getCacheManager(null, this.beanClassLoader, properties);
119122
}
120123

121124
private CachingProvider getCachingProvider(String cachingProviderFqn) {
@@ -151,6 +154,11 @@ private void customize(CacheManager cacheManager) {
151154
}
152155
}
153156

157+
@Override
158+
public void setBeanClassLoader(ClassLoader classLoader) {
159+
this.beanClassLoader = classLoader;
160+
}
161+
154162
/**
155163
* Determine if JCache is available. This either kicks in if a provider is available
156164
* as defined per {@link JCacheProviderAvailableCondition} or if a

0 commit comments

Comments
 (0)