Skip to content

Commit a562cb5

Browse files
committed
Merge pull request #13338 from mtheiss:master
* pr/13338: Polish "Retrieve javax.cache.CacheManager using Bean ClassLoader" Retrieve javax.cache.CacheManager using Bean ClassLoader
2 parents f745f20 + c67aedd commit a562cb5

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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,
@@ -85,6 +88,11 @@ class JCacheCacheConfiguration {
8588
this.cachePropertiesCustomizers = cachePropertiesCustomizers.getIfAvailable();
8689
}
8790

91+
@Override
92+
public void setBeanClassLoader(ClassLoader classLoader) {
93+
this.beanClassLoader = classLoader;
94+
}
95+
8896
@Bean
8997
public JCacheCacheManager cacheManager(CacheManager jCacheCacheManager) {
9098
JCacheCacheManager cacheManager = new JCacheCacheManager(jCacheCacheManager);
@@ -113,9 +121,9 @@ private CacheManager createCacheManager() throws IOException {
113121
.resolveConfigLocation(this.cacheProperties.getJcache().getConfig());
114122
if (configLocation != null) {
115123
return cachingProvider.getCacheManager(configLocation.getURI(),
116-
cachingProvider.getDefaultClassLoader(), properties);
124+
this.beanClassLoader, properties);
117125
}
118-
return cachingProvider.getCacheManager(null, null, properties);
126+
return cachingProvider.getCacheManager(null, this.beanClassLoader, properties);
119127
}
120128

121129
private CachingProvider getCachingProvider(String cachingProviderFqn) {

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,16 @@ public void jCacheCacheWithWrongConfig() {
374374
"spring.cache.jcache.config=" + configLocation);
375375
}
376376

377+
@Test
378+
public void jCacheCacheUseBeanClassLoader() {
379+
String cachingProviderFqn = MockCachingProvider.class.getName();
380+
load(DefaultCacheConfiguration.class, "spring.cache.type=jcache",
381+
"spring.cache.jcache.provider=" + cachingProviderFqn);
382+
JCacheCacheManager cacheManager = validateCacheManager(JCacheCacheManager.class);
383+
assertThat(cacheManager.getCacheManager().getClassLoader())
384+
.isEqualTo(this.context.getClassLoader());
385+
}
386+
377387
@Test
378388
public void ehcacheCacheWithCaches() {
379389
load(DefaultCacheConfiguration.class, "spring.cache.type=ehcache");

0 commit comments

Comments
 (0)