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 0699c0de3c..c800ff49e6 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 @@ -49,12 +49,12 @@ public class CassandraAutoConfiguration { private final CassandraProperties properties; - private final List customizers; + private final List clusterCustomizers; public CassandraAutoConfiguration(CassandraProperties properties, - ObjectProvider> customizers) { + ObjectProvider> clusterCustomizersProvider) { this.properties = properties; - this.customizers = customizers.getIfAvailable(); + this.clusterCustomizers = clusterCustomizersProvider.getIfAvailable(); } @Bean @@ -96,8 +96,8 @@ public class CassandraAutoConfiguration { } private void customize(Cluster cluster) { - if (this.customizers != null) { - for (ClusterCustomizer customizer : this.customizers) { + if (this.clusterCustomizers != null) { + for (ClusterCustomizer customizer : this.clusterCustomizers) { customizer.customize(cluster); } } 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/ClusterCustomizer.java index 8bbba2f0f5..2b02cf7987 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/ClusterCustomizer.java @@ -19,16 +19,17 @@ package org.springframework.boot.autoconfigure.cassandra; import com.datastax.driver.core.Cluster; /** - * Callback interface that can be used to customize a {@link 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. * * @author Eddú Meléndez * @since 1.5.0 - * @see CassandraAutoConfiguration */ public interface ClusterCustomizer { /** - * Customize the cluster. + * Customize the {@link Cluster}. * @param cluster the cluster to customize */ void customize(Cluster cluster); 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 f3fb9e7a94..b94312b3a1 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 @@ -33,6 +33,7 @@ import static org.mockito.Mockito.mock; * Tests for {@link CassandraAutoConfiguration} * * @author Eddú Meléndez + * @author Stephane Nicoll */ public class CassandraAutoConfigurationTests { @@ -47,8 +48,7 @@ public class CassandraAutoConfigurationTests { @Test public void createClusterWithDefault() { - this.context = doLoad(); - this.context.refresh(); + load(); assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1); Cluster cluster = this.context.getBean(Cluster.class); assertThat(cluster.getClusterName()).startsWith("cluster"); @@ -56,8 +56,7 @@ public class CassandraAutoConfigurationTests { @Test public void createClusterWithOverrides() { - this.context = doLoad("spring.data.cassandra.cluster-name=testcluster"); - this.context.refresh(); + load("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("testcluster"); @@ -65,30 +64,27 @@ public class CassandraAutoConfigurationTests { @Test public void createCustomizeCluster() { - this.context = doLoad(ClusterConfig.class); - this.context.refresh(); + load(ClusterConfig.class); assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1); assertThat(this.context.getBeanNamesForType(ClusterCustomizer.class).length).isEqualTo(1); } - private AnnotationConfigApplicationContext doLoad() { - AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); - applicationContext.register(PropertyPlaceholderAutoConfiguration.class, + private void load(String... environment) { + load(null, environment); + } + + private void load(Class config, String... environment) { + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + if (config != null) { + ctx.register(config); + } + ctx.register(PropertyPlaceholderAutoConfiguration.class, CassandraAutoConfiguration.class); - return applicationContext; + EnvironmentTestUtils.addEnvironment(ctx, environment); + ctx.refresh(); + this.context = ctx; } - private AnnotationConfigApplicationContext doLoad(Class clazz) { - AnnotationConfigApplicationContext applicationContext = doLoad(); - applicationContext.register(clazz); - return applicationContext; - } - - private AnnotationConfigApplicationContext doLoad(String... environment) { - AnnotationConfigApplicationContext applicationContext = doLoad(); - EnvironmentTestUtils.addEnvironment(applicationContext, environment); - return applicationContext; - } @Configuration static class ClusterConfig {