diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java index 643936d367..0eb08fc694 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java @@ -40,6 +40,8 @@ import org.springframework.util.StringUtils; * * @author Julien Dubois * @author Phillip Webb + * @author Eddú Meléndez + * @author Stephane Nicoll * @since 1.3.0 */ @Configuration @@ -49,12 +51,13 @@ public class CassandraAutoConfiguration { private final CassandraProperties properties; - private final List clusterCustomizers; + private final List builderCustomizers; public CassandraAutoConfiguration(CassandraProperties properties, - ObjectProvider> clusterCustomizersProvider) { + ObjectProvider> builderCustomizersProvider) { this.properties = properties; - this.clusterCustomizers = clusterCustomizersProvider.getIfAvailable(); + this.builderCustomizers = builderCustomizersProvider + .getIfAvailable(); } @Bean @@ -90,15 +93,14 @@ public class CassandraAutoConfiguration { String points = properties.getContactPoints(); builder.addContactPoints(StringUtils.commaDelimitedListToStringArray(points)); - Cluster cluster = builder.build(); - customize(cluster); - return cluster; + customize(builder); + return builder.build(); } - private void customize(Cluster cluster) { - if (this.clusterCustomizers != null) { - for (ClusterCustomizer customizer : this.clusterCustomizers) { - customizer.customize(cluster); + private void customize(Cluster.Builder builder) { + if (this.builderCustomizers != null) { + for (ClusterBuilderCustomizer customizer : this.builderCustomizers) { + customizer.customize(builder); } } } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterCustomizer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterBuilderCustomizer.java similarity index 75% rename from spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterCustomizer.java rename to spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterBuilderCustomizer.java index 2b02cf7987..5bb8cd6c37 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterCustomizer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterBuilderCustomizer.java @@ -20,18 +20,17 @@ import com.datastax.driver.core.Cluster; /** * Callback interface that can be implemented by beans wishing to customize the - * {@link Cluster} before it is fully initialized, in particular to tune its - * configuration. + * {@link Cluster} via {@link Cluster.Builder} retaining its default auto-configuration. * * @author Eddú Meléndez * @since 1.5.0 */ -public interface ClusterCustomizer { +public interface ClusterBuilderCustomizer { /** - * Customize the {@link Cluster}. - * @param cluster the cluster to customize + * Customize the {@link Cluster.Builder}. + * @param clusterBuilder the builder to customize */ - void customize(Cluster cluster); + void customize(Cluster.Builder clusterBuilder); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java index 64d09cac7d..0627fddf1e 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java @@ -64,12 +64,21 @@ public class CassandraAutoConfigurationTests { @Test public void createCustomizeCluster() { - load(ClusterConfig.class); + load(MockCustomizerConfig.class); assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1); - assertThat(this.context.getBeanNamesForType(ClusterCustomizer.class).length) + assertThat(this.context.getBeanNamesForType(ClusterBuilderCustomizer.class).length) .isEqualTo(1); } + @Test + public void customizerOverridesAutoConfig() { + load(SimpleCustomizerConfig.class, + "spring.data.cassandra.cluster-name=testcluster"); + assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1); + Cluster cluster = this.context.getBean(Cluster.class); + assertThat(cluster.getClusterName()).isEqualTo("overridden-name"); + } + private void load(String... environment) { load(null, environment); } @@ -87,11 +96,26 @@ public class CassandraAutoConfigurationTests { } @Configuration - static class ClusterConfig { + static class MockCustomizerConfig { + + @Bean + public ClusterBuilderCustomizer customizer() { + return mock(ClusterBuilderCustomizer.class); + } + + } + + @Configuration + static class SimpleCustomizerConfig { @Bean - public ClusterCustomizer customizer() { - return mock(ClusterCustomizer.class); + public ClusterBuilderCustomizer customizer() { + return new ClusterBuilderCustomizer() { + @Override + public void customize(Cluster.Builder clusterBuilder) { + clusterBuilder.withClusterName("overridden-name"); + } + }; } }