Commit a562cb5d authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #13338 from mtheiss:master

* pr/13338:
  Polish "Retrieve javax.cache.CacheManager using Bean ClassLoader"
  Retrieve javax.cache.CacheManager using Bean ClassLoader
parents f745f20c c67aedd8
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -26,6 +26,7 @@ import javax.cache.Caching; ...@@ -26,6 +26,7 @@ import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration; import javax.cache.configuration.MutableConfiguration;
import javax.cache.spi.CachingProvider; import javax.cache.spi.CachingProvider;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition; import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionMessage; import org.springframework.boot.autoconfigure.condition.ConditionMessage;
...@@ -61,7 +62,7 @@ import org.springframework.util.StringUtils; ...@@ -61,7 +62,7 @@ import org.springframework.util.StringUtils;
@Conditional({ CacheCondition.class, @Conditional({ CacheCondition.class,
JCacheCacheConfiguration.JCacheAvailableCondition.class }) JCacheCacheConfiguration.JCacheAvailableCondition.class })
@Import(HazelcastJCacheCustomizationConfiguration.class) @Import(HazelcastJCacheCustomizationConfiguration.class)
class JCacheCacheConfiguration { class JCacheCacheConfiguration implements BeanClassLoaderAware {
private final CacheProperties cacheProperties; private final CacheProperties cacheProperties;
...@@ -73,6 +74,8 @@ class JCacheCacheConfiguration { ...@@ -73,6 +74,8 @@ class JCacheCacheConfiguration {
private final List<JCachePropertiesCustomizer> cachePropertiesCustomizers; private final List<JCachePropertiesCustomizer> cachePropertiesCustomizers;
private ClassLoader beanClassLoader;
JCacheCacheConfiguration(CacheProperties cacheProperties, JCacheCacheConfiguration(CacheProperties cacheProperties,
CacheManagerCustomizers customizers, CacheManagerCustomizers customizers,
ObjectProvider<javax.cache.configuration.Configuration<?, ?>> defaultCacheConfiguration, ObjectProvider<javax.cache.configuration.Configuration<?, ?>> defaultCacheConfiguration,
...@@ -85,6 +88,11 @@ class JCacheCacheConfiguration { ...@@ -85,6 +88,11 @@ class JCacheCacheConfiguration {
this.cachePropertiesCustomizers = cachePropertiesCustomizers.getIfAvailable(); this.cachePropertiesCustomizers = cachePropertiesCustomizers.getIfAvailable();
} }
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.beanClassLoader = classLoader;
}
@Bean @Bean
public JCacheCacheManager cacheManager(CacheManager jCacheCacheManager) { public JCacheCacheManager cacheManager(CacheManager jCacheCacheManager) {
JCacheCacheManager cacheManager = new JCacheCacheManager(jCacheCacheManager); JCacheCacheManager cacheManager = new JCacheCacheManager(jCacheCacheManager);
...@@ -113,9 +121,9 @@ class JCacheCacheConfiguration { ...@@ -113,9 +121,9 @@ class JCacheCacheConfiguration {
.resolveConfigLocation(this.cacheProperties.getJcache().getConfig()); .resolveConfigLocation(this.cacheProperties.getJcache().getConfig());
if (configLocation != null) { if (configLocation != null) {
return cachingProvider.getCacheManager(configLocation.getURI(), 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) { private CachingProvider getCachingProvider(String cachingProviderFqn) {
......
...@@ -374,6 +374,16 @@ public class CacheAutoConfigurationTests { ...@@ -374,6 +374,16 @@ public class CacheAutoConfigurationTests {
"spring.cache.jcache.config=" + configLocation); "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 @Test
public void ehcacheCacheWithCaches() { public void ehcacheCacheWithCaches() {
load(DefaultCacheConfiguration.class, "spring.cache.type=ehcache"); load(DefaultCacheConfiguration.class, "spring.cache.type=ehcache");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment