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 0798071967..35f91e454d 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 @@ -40,6 +40,7 @@ import org.springframework.util.StringUtils; * @author Phillip Webb * @author EddĂș MelĂ©ndez * @author Stephane Nicoll + * @auther Steffen F. Qvistgaard * @since 1.3.0 */ @Configuration(proxyBeanMethods = false) @@ -50,7 +51,8 @@ public class CassandraAutoConfiguration { @Bean @ConditionalOnMissingBean public Cluster cassandraCluster(CassandraProperties properties, - ObjectProvider builderCustomizers) { + ObjectProvider builderCustomizers, + ObjectProvider clusterFactory) { PropertyMapper map = PropertyMapper.get(); Cluster.Builder builder = Cluster.builder() .withClusterName(properties.getClusterName()) @@ -71,7 +73,7 @@ public class CassandraAutoConfiguration { .toCall(builder::withoutJMXReporting); builderCustomizers.orderedStream() .forEach((customizer) -> customizer.customize(builder)); - return builder.build(); + 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/ClusterFactory.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterFactory.java new file mode 100644 index 0000000000..73d8684c1e --- /dev/null +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/ClusterFactory.java @@ -0,0 +1,39 @@ +/* + * 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; + +/** + * {@code CassandraClusterFactory} provides control over the creation of a {@Cluster} from + * an {@link Initializer}. + * + * @auther Steffen F. Qvistgaard + * @since 2.2.0 + */ +@FunctionalInterface +public interface ClusterFactory { + + /** + * 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 e9e9fc9496..a7f0c85fbb 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); + } + + } + }