From 28a8e3db2f861d2fdecd310ea7fc47265dc6a298 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 3 Mar 2017 11:18:06 +0100 Subject: [PATCH] Remove spring.cache.hazelcast.config Remove the creation of a separate `HazelcastInstance` specific to caching. Instead, the general `HazelcastAutoConfiguration` is used and if a `HazelcastInstance` bean is present we wrap it in a `CacheManager`. Closes gh-8470 --- .../autoconfigure/cache/CacheProperties.java | 31 ---- .../cache/HazelcastCacheConfiguration.java | 22 ++- .../cache/HazelcastInstanceConfiguration.java | 135 ------------------ .../cache/CacheAutoConfigurationTests.java | 106 ++++---------- .../cache/hazelcast-specific.xml | 17 --- .../hazelcast/hazelcast-specific.xml | 5 + 6 files changed, 51 insertions(+), 265 deletions(-) delete mode 100644 spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastInstanceConfiguration.java delete mode 100644 spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheProperties.java index fdf759db768..6ea9fad1423 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/CacheProperties.java @@ -51,8 +51,6 @@ public class CacheProperties { private final EhCache ehcache = new EhCache(); - private final Hazelcast hazelcast = new Hazelcast(); - private final Infinispan infinispan = new Infinispan(); private final JCache jcache = new JCache(); @@ -85,11 +83,6 @@ public class CacheProperties { return this.ehcache; } - @Deprecated - public Hazelcast getHazelcast() { - return this.hazelcast; - } - public Infinispan getInfinispan() { return this.infinispan; } @@ -184,30 +177,6 @@ public class CacheProperties { } - /** - * Hazelcast specific cache properties. - */ - @Deprecated - public static class Hazelcast { - - /** - * The location of the configuration file to use to initialize Hazelcast. - */ - private Resource config; - - @DeprecatedConfigurationProperty(replacement = "spring.hazelcast.config", - reason = "Use general hazelcast auto-configuration instead.") - @Deprecated - public Resource getConfig() { - return this.config; - } - - public void setConfig(Resource config) { - this.config = config; - } - - } - /** * Infinispan specific cache properties. */ diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java index d81dd16e1d4..f1f34cf9a84 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastCacheConfiguration.java @@ -16,17 +16,20 @@ package org.springframework.boot.autoconfigure.cache; +import java.io.IOException; + import com.hazelcast.core.HazelcastInstance; import com.hazelcast.spring.cache.HazelcastCacheManager; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration; import org.springframework.boot.autoconfigure.hazelcast.HazelcastConfigResourceCondition; import org.springframework.cache.CacheManager; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.Import; /** * Hazelcast cache configuration. Can either reuse the {@link HazelcastInstance} that has @@ -44,8 +47,21 @@ import org.springframework.context.annotation.Import; @ConditionalOnClass({ HazelcastInstance.class, HazelcastCacheManager.class }) @ConditionalOnMissingBean(CacheManager.class) @Conditional(CacheCondition.class) -@Import({ HazelcastInstanceConfiguration.Existing.class, - HazelcastInstanceConfiguration.Specific.class }) +@ConditionalOnSingleCandidate(HazelcastInstance.class) class HazelcastCacheConfiguration { + private final CacheManagerCustomizers customizers; + + HazelcastCacheConfiguration(CacheManagerCustomizers customizers) { + this.customizers = customizers; + } + + @Bean + public HazelcastCacheManager cacheManager( + HazelcastInstance existingHazelcastInstance) throws IOException { + HazelcastCacheManager cacheManager = new HazelcastCacheManager( + existingHazelcastInstance); + return this.customizers.customize(cacheManager); + } + } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastInstanceConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastInstanceConfiguration.java deleted file mode 100644 index 667f6e29b3f..00000000000 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cache/HazelcastInstanceConfiguration.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Copyright 2012-2016 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. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.autoconfigure.cache; - -import java.io.Closeable; -import java.io.IOException; - -import com.hazelcast.core.Hazelcast; -import com.hazelcast.core.HazelcastInstance; -import com.hazelcast.spring.cache.HazelcastCacheManager; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; -import org.springframework.boot.autoconfigure.hazelcast.HazelcastConfigResourceCondition; -import org.springframework.boot.autoconfigure.hazelcast.HazelcastInstanceFactory; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Conditional; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.io.Resource; - -/** - * Actual {@link HazelcastInstance} configurations imported by - * {@link HazelcastCacheConfiguration}. - * - * @author Stephane Nicoll - */ -abstract class HazelcastInstanceConfiguration { - - @Configuration - @ConditionalOnSingleCandidate(HazelcastInstance.class) - static class Existing { - - private final CacheProperties cacheProperties; - - private final CacheManagerCustomizers customizers; - - Existing(CacheProperties cacheProperties, CacheManagerCustomizers customizers) { - this.cacheProperties = cacheProperties; - this.customizers = customizers; - } - - @Bean - public HazelcastCacheManager cacheManager( - HazelcastInstance existingHazelcastInstance) throws IOException { - Resource config = this.cacheProperties.getHazelcast().getConfig(); - Resource location = this.cacheProperties.resolveConfigLocation(config); - if (location != null) { - HazelcastInstance cacheHazelcastInstance = new HazelcastInstanceFactory( - location).getHazelcastInstance(); - return new CloseableHazelcastCacheManager(cacheHazelcastInstance); - } - HazelcastCacheManager cacheManager = new HazelcastCacheManager( - existingHazelcastInstance); - return this.customizers.customize(cacheManager); - } - - } - - @Configuration - @ConditionalOnMissingBean(HazelcastInstance.class) - @Conditional(ConfigAvailableCondition.class) - static class Specific { - - private final CacheProperties cacheProperties; - - private final CacheManagerCustomizers customizers; - - Specific(CacheProperties cacheProperties, CacheManagerCustomizers customizers) { - this.cacheProperties = cacheProperties; - this.customizers = customizers; - } - - @Bean - public HazelcastInstance hazelcastInstance() throws IOException { - Resource config = this.cacheProperties.getHazelcast().getConfig(); - Resource location = this.cacheProperties.resolveConfigLocation(config); - if (location != null) { - return new HazelcastInstanceFactory(location).getHazelcastInstance(); - } - return Hazelcast.newHazelcastInstance(); - } - - @Bean - public HazelcastCacheManager cacheManager() throws IOException { - HazelcastCacheManager cacheManager = new HazelcastCacheManager( - hazelcastInstance()); - return this.customizers.customize(cacheManager); - } - - } - - /** - * {@link HazelcastConfigResourceCondition} that checks if the - * {@code spring.cache.hazelcast.config} configuration key is defined. - */ - static class ConfigAvailableCondition extends HazelcastConfigResourceCondition { - - ConfigAvailableCondition() { - super("spring.cache.hazelcast", "config"); - } - - } - - private static class CloseableHazelcastCacheManager extends HazelcastCacheManager - implements Closeable { - - private final HazelcastInstance hazelcastInstance; - - CloseableHazelcastCacheManager(HazelcastInstance hazelcastInstance) { - super(hazelcastInstance); - this.hazelcastInstance = hazelcastInstance; - } - - @Override - public void close() throws IOException { - this.hazelcastInstance.shutdown(); - } - - } - -} diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java index 3b920d68c64..c3d26ed5eb7 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cache/CacheAutoConfigurationTests.java @@ -52,7 +52,6 @@ import org.junit.rules.ExpectedException; import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.BeanCreationException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; -import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.cache.support.MockCachingProvider; import org.springframework.boot.autoconfigure.hazelcast.HazelcastAutoConfiguration; import org.springframework.boot.test.util.EnvironmentTestUtils; @@ -347,7 +346,7 @@ public class CacheAutoConfigurationTests { @Test public void jCacheCacheWithConfig() throws IOException { String cachingProviderFqn = MockCachingProvider.class.getName(); - String configLocation = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"; + String configLocation = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; load(JCacheCustomConfiguration.class, "spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.jcache.config=" + configLocation); @@ -431,7 +430,8 @@ public class CacheAutoConfigurationTests { @Test public void hazelcastCacheExplicit() { - load(DefaultCacheConfiguration.class, "spring.cache.type=hazelcast"); + load(new Class[] { HazelcastAutoConfiguration.class, + DefaultCacheConfiguration.class }, "spring.cache.type=hazelcast"); HazelcastCacheManager cacheManager = validateCacheManager( HazelcastCacheManager.class); // NOTE: the hazelcast implementation knows about a cache in a lazy manner. @@ -443,38 +443,10 @@ public class CacheAutoConfigurationTests { @Test public void hazelcastCacheWithCustomizers() { - testCustomizers(DefaultCacheAndCustomizersConfiguration.class, "hazelcast", + testCustomizers(HazelcastCacheAndCustomizersConfiguration.class, "hazelcast", "allCacheManagerCustomizer", "hazelcastCacheManagerCustomizer"); } - @Test - @Deprecated - public void hazelcastCacheWithConfig() throws IOException { - load(DefaultCacheConfiguration.class, "spring.cache.type=hazelcast", - "spring.cache.hazelcast.config=org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"); - HazelcastInstance hazelcastInstance = this.context - .getBean(HazelcastInstance.class); - HazelcastCacheManager cacheManager = validateCacheManager( - HazelcastCacheManager.class); - HazelcastInstance actual = cacheManager.getHazelcastInstance(); - assertThat(actual).isSameAs(hazelcastInstance); - assertThat(actual.getConfig().getConfigurationUrl()) - .isEqualTo(new ClassPathResource( - "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml") - .getURL()); - cacheManager.getCache("foobar"); - assertThat(cacheManager.getCacheNames()).containsOnly("foobar"); - } - - @Test - @Deprecated - public void hazelcastWithWrongConfig() { - this.thrown.expect(BeanCreationException.class); - this.thrown.expectMessage("foo/bar/unknown.xml"); - load(DefaultCacheConfiguration.class, "spring.cache.type=hazelcast", - "spring.cache.hazelcast.config=foo/bar/unknown.xml"); - } - @Test public void hazelcastCacheWithExistingHazelcastInstance() { load(HazelcastCustomHazelcastInstance.class, "spring.cache.type=hazelcast"); @@ -485,49 +457,20 @@ public class CacheAutoConfigurationTests { } @Test - public void hazelcastCacheWithMainHazelcastAutoConfiguration() throws IOException { - String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; - AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(applicationContext, - "spring.cache.type=hazelcast", "spring.hazelcast.config=" + mainConfig); - applicationContext.register(DefaultCacheConfiguration.class); - applicationContext.register(HazelcastAndCacheConfiguration.class); - applicationContext.refresh(); - this.context = applicationContext; + public void hazelcastCacheWithHazelcastAutoConfiguration() throws IOException { + String hazelcastConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; + load(new Class[] { HazelcastAutoConfiguration.class, + DefaultCacheConfiguration.class }, "spring.cache.type=hazelcast", + "spring.hazelcast.config=" + hazelcastConfig); HazelcastCacheManager cacheManager = validateCacheManager( HazelcastCacheManager.class); HazelcastInstance hazelcastInstance = this.context .getBean(HazelcastInstance.class); - assertThat(cacheManager.getHazelcastInstance()).isEqualTo(hazelcastInstance); + assertThat(cacheManager.getHazelcastInstance()).isSameAs(hazelcastInstance); assertThat(hazelcastInstance.getConfig().getConfigurationFile()) - .isEqualTo(new ClassPathResource(mainConfig).getFile()); - } - - @Test - @Deprecated - public void hazelcastCacheWithMainHazelcastAutoConfigurationAndSeparateCacheConfig() - throws IOException { - String mainConfig = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; - String cacheConfig = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"; - AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(applicationContext, - "spring.cache.type=hazelcast", - "spring.cache.hazelcast.config=" + cacheConfig, - "spring.hazelcast.config=" + mainConfig); - applicationContext.register(DefaultCacheConfiguration.class); - applicationContext.register(HazelcastAndCacheConfiguration.class); - applicationContext.refresh(); - this.context = applicationContext; - HazelcastInstance hazelcastInstance = this.context - .getBean(HazelcastInstance.class); - HazelcastCacheManager cacheManager = validateCacheManager( - HazelcastCacheManager.class); - HazelcastInstance cacheHazelcastInstance = cacheManager.getHazelcastInstance(); - assertThat(cacheHazelcastInstance).isNotEqualTo(hazelcastInstance); // Our custom - assertThat(hazelcastInstance.getConfig().getConfigurationFile()) - .isEqualTo(new ClassPathResource(mainConfig).getFile()); - assertThat(cacheHazelcastInstance.getConfig().getConfigurationFile()) - .isEqualTo(new ClassPathResource(cacheConfig).getFile()); + .isEqualTo(new ClassPathResource(hazelcastConfig).getFile()); + assertThat(cacheManager.getCache("foobar")).isNotNull(); + assertThat(cacheManager.getCacheNames()).containsOnly("foobar"); } @Test @@ -549,7 +492,7 @@ public class CacheAutoConfigurationTests { @Test public void hazelcastAsJCacheWithConfig() throws IOException { String cachingProviderFqn = HazelcastCachingProvider.class.getName(); - String configLocation = "org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml"; + String configLocation = "org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml"; load(DefaultCacheConfiguration.class, "spring.cache.type=jcache", "spring.cache.jcache.provider=" + cachingProviderFqn, "spring.cache.jcache.config=" + configLocation); @@ -711,9 +654,13 @@ public class CacheAutoConfigurationTests { } private void load(Class config, String... environment) { + load(new Class[]{config}, environment); + } + + private void load(Class[] configs, String... environment) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(applicationContext, environment); - applicationContext.register(config); + applicationContext.register(configs); applicationContext.register(CacheAutoConfiguration.class); applicationContext.refresh(); this.context = applicationContext; @@ -760,6 +707,14 @@ public class CacheAutoConfigurationTests { } + @Configuration + @EnableCaching + @Import({ HazelcastAutoConfiguration.class, + CacheManagerCustomizersConfiguration.class }) + static class HazelcastCacheAndCustomizersConfiguration { + + } + @Configuration @EnableCaching static class CouchbaseCacheConfiguration { @@ -870,13 +825,6 @@ public class CacheAutoConfigurationTests { } - @Configuration - @ImportAutoConfiguration({ CacheAutoConfiguration.class, - HazelcastAutoConfiguration.class }) - static class HazelcastAndCacheConfiguration { - - } - @Configuration @EnableCaching static class InfinispanCustomConfiguration { diff --git a/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml b/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml deleted file mode 100644 index f7e59aeaead..00000000000 --- a/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/cache/hazelcast-specific.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - 3600 - 600 - - - - - - - - - - \ No newline at end of file diff --git a/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml b/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml index 9a0d0513b7d..8e6ec15d2f5 100644 --- a/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml +++ b/spring-boot-autoconfigure/src/test/resources/org/springframework/boot/autoconfigure/hazelcast/hazelcast-specific.xml @@ -4,6 +4,11 @@ + + 3600 + 600 + +