diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java index be58c02228f..35f91e454d0 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java @@ -48,17 +48,11 @@ import org.springframework.util.StringUtils; @EnableConfigurationProperties(CassandraProperties.class) public class CassandraAutoConfiguration { - @Bean - @ConditionalOnMissingBean - public CassandraClusterFactory cassandraClusterFactory() { - return new CassandraClusterFactoryImpl(); - } - @Bean @ConditionalOnMissingBean public Cluster cassandraCluster(CassandraProperties properties, ObjectProvider builderCustomizers, - CassandraClusterFactory clusterFactory) { + ObjectProvider clusterFactory) { PropertyMapper map = PropertyMapper.get(); Cluster.Builder builder = Cluster.builder() .withClusterName(properties.getClusterName()) @@ -79,8 +73,7 @@ public class CassandraAutoConfiguration { .toCall(builder::withoutJMXReporting); builderCustomizers.orderedStream() .forEach((customizer) -> customizer.customize(builder)); - - return clusterFactory.build(builder); + return clusterFactory.getIfAvailable(() -> Cluster::buildFrom).create(builder); } private QueryOptions getQueryOptions(CassandraProperties properties) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactoryImpl.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactoryImpl.java deleted file mode 100644 index 2b858f9f92e..00000000000 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactoryImpl.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2012-2019 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 - * - * https://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.cassandra; - -import com.datastax.driver.core.Cluster; -import com.datastax.driver.core.Cluster.Initializer; - -/** - * Default Cassandra Cluster Factory Implementation. - * - * @auther Steffen F. Qvistgaard - * @since 2.2.0 - */ -public class CassandraClusterFactoryImpl implements CassandraClusterFactory { - - @Override - public Cluster build(final Initializer initializer) { - return Cluster.buildFrom(initializer); - } - -} diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactory.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterFactory.java similarity index 69% rename from spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactory.java rename to spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterFactory.java index a07c55edb9c..73d8684c1e6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraClusterFactory.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterFactory.java @@ -20,15 +20,20 @@ import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Cluster.Initializer; /** - * Cassandra Cluster Factory Interface. - * - * This interface allows the default cassandra cluster builder to be overwritten + * {@code CassandraClusterFactory} provides control over the creation of a {@Cluster} from + * an {@link Initializer}. * * @auther Steffen F. Qvistgaard * @since 2.2.0 */ -public interface CassandraClusterFactory { +@FunctionalInterface +public interface ClusterFactory { - Cluster build(Initializer initializer); + /** + * Creates a {@link Cluster} from the given {@link Initializer}. + * @param initializer the {@Code Initializer} + * @return the {@code Cluster} + */ + Cluster create(Initializer initializer); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java index e9e9fc9496c..a7f0c85fbb7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java @@ -17,6 +17,7 @@ package org.springframework.boot.autoconfigure.cassandra; import com.datastax.driver.core.Cluster; +import com.datastax.driver.core.Cluster.Initializer; import com.datastax.driver.core.PoolingOptions; import org.junit.jupiter.api.Test; @@ -115,6 +116,14 @@ public class CassandraAutoConfigurationTests { }); } + @Test + public void clusterFactoryIsCalledToCreateCluster() { + this.contextRunner.withUserConfiguration(ClusterFactoryConfig.class) + .run((context) -> assertThat( + context.getBean(TestClusterFactory.class).initializer) + .isNotNull()); + } + @Configuration(proxyBeanMethods = false) static class MockCustomizerConfig { @@ -135,4 +144,26 @@ public class CassandraAutoConfigurationTests { } + @Configuration(proxyBeanMethods = false) + static class ClusterFactoryConfig { + + @Bean + public TestClusterFactory clusterFactory() { + return new TestClusterFactory(); + } + + } + + static class TestClusterFactory implements ClusterFactory { + + private Initializer initializer = null; + + @Override + public Cluster create(Initializer initializer) { + this.initializer = initializer; + return Cluster.buildFrom(initializer); + } + + } + }