From 2e9005de14b77158e819b6f698b67089eb54c9e2 Mon Sep 17 00:00:00 2001 From: ayudovin Date: Thu, 4 Apr 2019 11:08:19 +0300 Subject: [PATCH 1/2] Add property to configure Mongo auto index creation Closes gh-16454 --- .../data/mongo/MongoDataConfiguration.java | 4 ++++ .../boot/autoconfigure/mongo/MongoProperties.java | 14 ++++++++++++++ .../mongo/MongoDataAutoConfigurationTests.java | 12 ++++++++++++ .../autoconfigure/mongo/MongoPropertiesTests.java | 12 ++++++++++++ 4 files changed, 42 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataConfiguration.java index 62694fbe76..3bf8988ad2 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataConfiguration.java @@ -21,6 +21,7 @@ import org.springframework.beans.BeanUtils; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.domain.EntityScanner; import org.springframework.boot.autoconfigure.mongo.MongoProperties; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -34,6 +35,7 @@ import org.springframework.data.mongodb.core.mapping.MongoMappingContext; * Base configuration class for Spring Data's mongo support. * * @author Madhura Bhave + * @author Artsiom Yudovin */ @Configuration(proxyBeanMethods = false) class MongoDataConfiguration { @@ -43,7 +45,9 @@ class MongoDataConfiguration { public MongoMappingContext mongoMappingContext(ApplicationContext applicationContext, MongoProperties properties, MongoCustomConversions conversions) throws ClassNotFoundException { + PropertyMapper mapper = PropertyMapper.get().alwaysApplyingWhenNonNull(); MongoMappingContext context = new MongoMappingContext(); + mapper.from(properties.isAutoIndexCreation()).to(context::setAutoIndexCreation); context.setInitialEntitySet(new EntityScanner(applicationContext) .scan(Document.class, Persistent.class)); Class strategyClass = properties.getFieldNamingStrategy(); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java index fd3e4c8745..57addc9166 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java @@ -31,6 +31,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties; * @author Stephane Nicoll * @author Nasko Vasilev * @author Mark Paluch + * @author Artsiom Yudovin */ @ConfigurationProperties(prefix = "spring.data.mongodb") public class MongoProperties { @@ -90,6 +91,11 @@ public class MongoProperties { */ private Class fieldNamingStrategy; + /** + * Enables/disables auto-index creation. + */ + private Boolean autoIndexCreation; + public String getHost() { return this.host; } @@ -173,4 +179,12 @@ public class MongoProperties { return new MongoClientURI(determineUri()).getDatabase(); } + public Boolean isAutoIndexCreation() { + return this.autoIndexCreation; + } + + public void setAutoIndexCreation(Boolean autoIndexCreation) { + this.autoIndexCreation = autoIndexCreation; + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java index 9e874459ac..27623dcaf7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java @@ -132,6 +132,18 @@ public class MongoDataAutoConfigurationTests { }); } + @Test + public void customAutoIndexCreation() { + this.contextRunner + .withPropertyValues("spring.data.mongodb.autoIndexCreation:true") + .run((context) -> { + MongoMappingContext mappingContext = context + .getBean(MongoMappingContext.class); + assertThat(mappingContext.isAutoIndexCreation()) + .isEqualTo(Boolean.TRUE); + }); + } + @Test public void interfaceFieldNamingStrategy() { this.contextRunner diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java index 067fc6d7d5..e75152bf4d 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoPropertiesTests.java @@ -37,6 +37,7 @@ import static org.assertj.core.api.Assertions.assertThat; * @author Andy Wilkinson * @author Stephane Nicoll * @author Mark Paluch + * @author Artsiom Yudovin */ public class MongoPropertiesTests { @@ -155,6 +156,17 @@ public class MongoPropertiesTests { assertServerAddress(allAddresses.get(0), "localhost", 27017); } + @Test + public void canBindAutoIndexCreation() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + TestPropertyValues.of("spring.data.mongodb.autoIndexCreation:true") + .applyTo(context); + context.register(Config.class); + context.refresh(); + MongoProperties properties = context.getBean(MongoProperties.class); + assertThat(properties.isAutoIndexCreation()).isEqualTo(Boolean.TRUE); + } + private void assertServerAddress(ServerAddress serverAddress, String expectedHost, int expectedPort) { assertThat(serverAddress.getHost()).isEqualTo(expectedHost); From bf6f9b4c43355b92a3c5de4bec1af0e4deaf3ec5 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Mon, 15 Apr 2019 16:52:44 -0700 Subject: [PATCH 2/2] Polish "Add property for mongo auto-index creation" See gh-16454 --- .../boot/autoconfigure/mongo/MongoProperties.java | 2 +- .../data/mongo/MongoDataAutoConfigurationTests.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java index 57addc9166..79417b0b40 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoProperties.java @@ -92,7 +92,7 @@ public class MongoProperties { private Class fieldNamingStrategy; /** - * Enables/disables auto-index creation. + * Whether to enable auto-index creation. */ private Boolean autoIndexCreation; diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java index 27623dcaf7..e3b36b53b9 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfigurationTests.java @@ -135,12 +135,12 @@ public class MongoDataAutoConfigurationTests { @Test public void customAutoIndexCreation() { this.contextRunner - .withPropertyValues("spring.data.mongodb.autoIndexCreation:true") + .withPropertyValues("spring.data.mongodb.autoIndexCreation:false") .run((context) -> { MongoMappingContext mappingContext = context .getBean(MongoMappingContext.class); assertThat(mappingContext.isAutoIndexCreation()) - .isEqualTo(Boolean.TRUE); + .isEqualTo(Boolean.FALSE); }); }