Merge pull request #13338 from mtheiss:master

* pr/13338:
  Polish "Retrieve javax.cache.CacheManager using Bean ClassLoader"
  Retrieve javax.cache.CacheManager using Bean ClassLoader
This commit is contained in:
Stephane Nicoll 2018-06-04 14:11:08 +02:00
commit a562cb5d6c
2 changed files with 22 additions and 4 deletions

View File

@ -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.
@ -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<JCachePropertiesCustomizer> cachePropertiesCustomizers;
private ClassLoader beanClassLoader;
JCacheCacheConfiguration(CacheProperties cacheProperties,
CacheManagerCustomizers customizers,
ObjectProvider<javax.cache.configuration.Configuration<?, ?>> defaultCacheConfiguration,
@ -85,6 +88,11 @@ class JCacheCacheConfiguration {
this.cachePropertiesCustomizers = cachePropertiesCustomizers.getIfAvailable();
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
@Bean
public JCacheCacheManager cacheManager(CacheManager jCacheCacheManager) {
JCacheCacheManager cacheManager = new JCacheCacheManager(jCacheCacheManager);
@ -113,9 +121,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) {

View File

@ -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");