diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java index cbdf540f2c..ec308ad0b6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/JCacheCacheConfiguration.java +++ b/spring-boot-project/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. @@ -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, @@ -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) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java index 4d8953fa30..bde367c8cc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java @@ -469,6 +469,20 @@ public class CacheAutoConfigurationTests extends AbstractCacheAutoConfigurationT .hasMessageContaining(configLocation)); } + @Test + public void jCacheCacheUseBeanClassLoader() { + String cachingProviderFqn = MockCachingProvider.class.getName(); + this.contextRunner.withUserConfiguration(DefaultCacheConfiguration.class) + .withPropertyValues("spring.cache.type=jcache", + "spring.cache.jcache.provider=" + cachingProviderFqn) + .run((context) -> { + JCacheCacheManager cacheManager = getCacheManager(context, + JCacheCacheManager.class); + assertThat(cacheManager.getCacheManager().getClassLoader()) + .isEqualTo(context.getClassLoader()); + }); + } + @Test public void hazelcastCacheExplicit() { this.contextRunner