Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand All @@ -34,6 +35,7 @@
* Base configuration class for Spring Data's mongo support.
*
* @author Madhura Bhave
* @author Artsiom Yudovin
*/
@Configuration(proxyBeanMethods = false)
class MongoDataConfiguration {
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @author Stephane Nicoll
* @author Nasko Vasilev
* @author Mark Paluch
* @author Artsiom Yudovin
*/
@ConfigurationProperties(prefix = "spring.data.mongodb")
public class MongoProperties {
Expand Down Expand Up @@ -90,6 +91,11 @@ public class MongoProperties {
*/
private Class<?> fieldNamingStrategy;

/**
* Enables/disables auto-index creation.
*/
private Boolean autoIndexCreation;

public String getHost() {
return this.host;
}
Expand Down Expand Up @@ -173,4 +179,12 @@ public String getMongoClientDatabase() {
return new MongoClientURI(determineUri()).getDatabase();
}

public Boolean isAutoIndexCreation() {
return this.autoIndexCreation;
}

public void setAutoIndexCreation(Boolean autoIndexCreation) {
this.autoIndexCreation = autoIndexCreation;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ public void customFieldNamingStrategy() {
});
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
* @author Andy Wilkinson
* @author Stephane Nicoll
* @author Mark Paluch
* @author Artsiom Yudovin
*/
public class MongoPropertiesTests {

Expand Down Expand Up @@ -155,6 +156,17 @@ public void noCustomAddressAndNoUriUsesDefaultUri() {
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);
Expand Down