diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactory.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactory.java index 3a9e4e1db5..61d3c6eb0d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactory.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactory.java @@ -92,8 +92,7 @@ public class ReactiveMongoClientFactory { ClusterSettings clusterSettings = ClusterSettings.builder() .hosts(Collections.singletonList(new ServerAddress(host, port))).build(); builder.clusterSettings(clusterSettings); - customize(builder); - return MongoClients.create(builder.build()); + return createMongoClient(builder); } private MongoClient createNetworkMongoClient(MongoClientSettings settings) { @@ -102,7 +101,7 @@ public class ReactiveMongoClientFactory { } ConnectionString connectionString = new ConnectionString( this.properties.determineUri()); - return MongoClients.create(createBuilder(settings, connectionString).build()); + return createMongoClient(createBuilder(settings, connectionString)); } private MongoClient createCredentialNetworkMongoClient(MongoClientSettings settings) { @@ -117,7 +116,7 @@ public class ReactiveMongoClientFactory { ServerAddress serverAddress = new ServerAddress(host, port); builder.clusterSettings(ClusterSettings.builder() .hosts(Collections.singletonList(serverAddress)).build()); - return MongoClients.create(builder.build()); + return createMongoClient(builder); } private void applyCredentials(Builder builder) { @@ -134,6 +133,11 @@ public class ReactiveMongoClientFactory { return (value == null ? defaultValue : value); } + private MongoClient createMongoClient(Builder builder) { + customize(builder); + return MongoClients.create(builder.build()); + } + private Builder createBuilder(MongoClientSettings settings, ConnectionString connection) { Builder builder = builder(settings); @@ -155,7 +159,6 @@ public class ReactiveMongoClientFactory { if (connection.getApplicationName() != null) { builder.applicationName(connection.getApplicationName()); } - customize(builder); return builder; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactoryTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactoryTests.java index 33962435dd..48eb1144a5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactoryTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/ReactiveMongoClientFactoryTests.java @@ -160,6 +160,16 @@ public class ReactiveMongoClientFactoryTests { verify(customizer).customize(any(MongoClientSettings.Builder.class)); } + @Test + public void customizerIsInvokedWhenHostIsSet() { + MongoProperties properties = new MongoProperties(); + properties.setHost("localhost"); + MongoClientSettingsBuilderCustomizer customizer = mock( + MongoClientSettingsBuilderCustomizer.class); + createMongoClient(properties, this.environment, customizer); + verify(customizer).customize(any(MongoClientSettings.Builder.class)); + } + @Test public void customizerIsInvokedForEmbeddedMongo() { MongoProperties properties = new MongoProperties();