Polish "Upgrade to MongoDB Java Driver 4.0 beta1"

See gh-19960
pull/20020/head
Stephane Nicoll 5 years ago
parent d2d6dbdc00
commit c4daff7225

@ -83,8 +83,8 @@ dependencies {
optional("org.influxdb:influxdb-java")
optional("org.jolokia:jolokia-core")
optional("org.liquibase:liquibase-core")
optional("org.mongodb:mongodb-driver-sync")
optional("org.mongodb:mongodb-driver-reactivestreams")
optional("org.mongodb:mongodb-driver-sync")
optional("org.springframework:spring-jdbc")
optional("org.springframework:spring-jms")
optional("org.springframework:spring-messaging")

@ -43,8 +43,8 @@ dependencies {
optional("org.hibernate.validator:hibernate-validator")
optional("org.influxdb:influxdb-java")
optional("org.liquibase:liquibase-core")
optional("org.mongodb:mongodb-driver-sync")
optional("org.mongodb:mongodb-driver-reactivestreams")
optional("org.mongodb:mongodb-driver-sync")
optional("org.springframework:spring-jdbc")
optional("org.springframework:spring-messaging")
optional("org.springframework:spring-webflux")

@ -95,8 +95,8 @@ dependencies {
optional("org.jooq:jooq")
optional("org.liquibase:liquibase-core")
optional("org.messaginghub:pooled-jms")
optional("org.mongodb:mongodb-driver-sync")
optional("org.mongodb:mongodb-driver-reactivestreams")
optional("org.mongodb:mongodb-driver-sync")
optional("org.quartz-scheduler:quartz")
optional("org.springframework:spring-jdbc")
optional("org.springframework.integration:spring-integration-core")

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -16,6 +16,8 @@
package org.springframework.boot.autoconfigure.data.mongo;
import com.mongodb.client.MongoClient;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@ -46,9 +48,10 @@ import org.springframework.data.mongodb.gridfs.GridFsTemplate;
* @since 1.1.0
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ com.mongodb.client.MongoClient.class, MongoTemplate.class })
@ConditionalOnClass({ MongoClient.class, MongoTemplate.class })
@EnableConfigurationProperties(MongoProperties.class)
@Import({ MongoDataConfiguration.class, MongoDbFactoryConfiguration.class, MongoDbFactoryDependentConfiguration.class })
@Import({ MongoDataConfiguration.class, MongoDatabaseFactoryConfiguration.class,
MongoDatabaseFactoryDependentConfiguration.class })
@AutoConfigureAfter(MongoAutoConfiguration.class)
public class MongoDataAutoConfiguration {

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -18,14 +18,10 @@ package org.springframework.boot.autoconfigure.data.mongo;
import com.mongodb.client.MongoClient;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.mongo.MongoDbFactoryConfiguration.AnyMongoClientAvailable;
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoDatabaseFactorySupport;
@ -35,43 +31,16 @@ import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
* Configuration for a {@link MongoDatabaseFactory}.
*
* @author Andy Wilkinson
* @author Stephane Nicoll
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(MongoDatabaseFactory.class)
@Conditional(AnyMongoClientAvailable.class)
class MongoDbFactoryConfiguration {
@ConditionalOnSingleCandidate(MongoClient.class)
class MongoDatabaseFactoryConfiguration {
@Bean
MongoDatabaseFactorySupport<?> mongoDbFactory(ObjectProvider<MongoClient> mongoClient, MongoProperties properties) {
com.mongodb.client.MongoClient fallbackClient = mongoClient.getIfAvailable();
if (fallbackClient != null) {
return new SimpleMongoClientDatabaseFactory(fallbackClient, properties.getMongoClientDatabase());
}
throw new IllegalStateException("Expected to find at least one MongoDB client.");
}
/**
* Check if either a {@link MongoClient com.mongodb.MongoClient} or
* {@link com.mongodb.client.MongoClient com.mongodb.client.MongoClient} bean is
* available.
*/
static class AnyMongoClientAvailable extends AnyNestedCondition {
AnyMongoClientAvailable() {
super(ConfigurationPhase.REGISTER_BEAN);
}
@ConditionalOnBean(MongoClient.class)
static class PreferredClientAvailable {
}
@ConditionalOnBean(com.mongodb.client.MongoClient.class)
static class FallbackClientAvailable {
}
MongoDatabaseFactorySupport<?> mongoDatabaseFactory(MongoClient mongoClient, MongoProperties properties) {
return new SimpleMongoClientDatabaseFactory(mongoClient, properties.getMongoClientDatabase());
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -48,18 +48,18 @@ import org.springframework.util.StringUtils;
*/
@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(MongoDatabaseFactory.class)
class MongoDbFactoryDependentConfiguration {
class MongoDatabaseFactoryDependentConfiguration {
private final MongoProperties properties;
MongoDbFactoryDependentConfiguration(MongoProperties properties) {
MongoDatabaseFactoryDependentConfiguration(MongoProperties properties) {
this.properties = properties;
}
@Bean
@ConditionalOnMissingBean(MongoOperations.class)
MongoTemplate mongoTemplate(MongoDatabaseFactory mongoDbFactory, MongoConverter converter) {
return new MongoTemplate(mongoDbFactory, converter);
MongoTemplate mongoTemplate(MongoDatabaseFactory factory, MongoConverter converter) {
return new MongoTemplate(factory, converter);
}
@Bean
@ -74,8 +74,8 @@ class MongoDbFactoryDependentConfiguration {
@Bean
@ConditionalOnMissingBean(GridFsOperations.class)
GridFsTemplate gridFsTemplate(MongoDatabaseFactory mongoDbFactory, MongoTemplate mongoTemplate) {
return new GridFsTemplate(new GridFsMongoDbFactory(mongoDbFactory, this.properties),
GridFsTemplate gridFsTemplate(MongoDatabaseFactory factory, MongoTemplate mongoTemplate) {
return new GridFsTemplate(new GridFsMongoDatabaseFactory(factory, this.properties),
mongoTemplate.getConverter());
}
@ -83,16 +83,16 @@ class MongoDbFactoryDependentConfiguration {
* {@link MongoDatabaseFactory} decorator to respect
* {@link MongoProperties#getGridFsDatabase()} if set.
*/
static class GridFsMongoDbFactory implements MongoDatabaseFactory {
static class GridFsMongoDatabaseFactory implements MongoDatabaseFactory {
private final MongoDatabaseFactory mongoDbFactory;
private final MongoDatabaseFactory mongoDatabaseFactory;
private final MongoProperties properties;
GridFsMongoDbFactory(MongoDatabaseFactory mongoDbFactory, MongoProperties properties) {
Assert.notNull(mongoDbFactory, "MongoDbFactory must not be null");
GridFsMongoDatabaseFactory(MongoDatabaseFactory mongoDatabaseFactory, MongoProperties properties) {
Assert.notNull(mongoDatabaseFactory, "MongoDatabaseFactory must not be null");
Assert.notNull(properties, "Properties must not be null");
this.mongoDbFactory = mongoDbFactory;
this.mongoDatabaseFactory = mongoDatabaseFactory;
this.properties = properties;
}
@ -100,29 +100,29 @@ class MongoDbFactoryDependentConfiguration {
public MongoDatabase getMongoDatabase() throws DataAccessException {
String gridFsDatabase = this.properties.getGridFsDatabase();
if (StringUtils.hasText(gridFsDatabase)) {
return this.mongoDbFactory.getMongoDatabase(gridFsDatabase);
return this.mongoDatabaseFactory.getMongoDatabase(gridFsDatabase);
}
return this.mongoDbFactory.getMongoDatabase();
return this.mongoDatabaseFactory.getMongoDatabase();
}
@Override
public MongoDatabase getMongoDatabase(String dbName) throws DataAccessException {
return this.mongoDbFactory.getMongoDatabase(dbName);
return this.mongoDatabaseFactory.getMongoDatabase(dbName);
}
@Override
public PersistenceExceptionTranslator getExceptionTranslator() {
return this.mongoDbFactory.getExceptionTranslator();
return this.mongoDatabaseFactory.getExceptionTranslator();
}
@Override
public ClientSession getSession(ClientSessionOptions options) {
return this.mongoDbFactory.getSession(options);
return this.mongoDatabaseFactory.getSession(options);
}
@Override
public MongoDatabaseFactory withSession(ClientSession session) {
return this.mongoDbFactory.withSession(session);
return this.mongoDatabaseFactory.withSession(session);
}
}

@ -97,9 +97,9 @@ public class MongoReactiveDataAutoConfiguration {
@Bean
@ConditionalOnMissingBean(ReactiveGridFsOperations.class)
public ReactiveGridFsTemplate reactiveGridFsTemplate(ReactiveMongoDatabaseFactory reactiveMongoDbFactory,
public ReactiveGridFsTemplate reactiveGridFsTemplate(ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory,
MappingMongoConverter mappingMongoConverter, DataBufferFactory dataBufferFactory) {
return new ReactiveGridFsTemplate(dataBufferFactory, reactiveMongoDbFactory, mappingMongoConverter, null);
return new ReactiveGridFsTemplate(dataBufferFactory, reactiveMongoDatabaseFactory, mappingMongoConverter, null);
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -16,8 +16,6 @@
package org.springframework.boot.autoconfigure.mongo;
import java.util.stream.Collectors;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
@ -43,17 +41,14 @@ import org.springframework.core.env.Environment;
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass(MongoClient.class)
@EnableConfigurationProperties(MongoProperties.class)
@ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDbFactory")
@ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDatabaseFactory")
public class MongoAutoConfiguration {
@Bean
@ConditionalOnMissingBean(type = { "com.mongodb.client.MongoClient" })
public MongoClient mongo(MongoProperties properties, Environment environment,
ObjectProvider<MongoClientSettingsBuilderCustomizer> builderCustomizers,
ObjectProvider<MongoClientSettings> settings) {
return new MongoClientFactory(properties, environment,
builderCustomizers.orderedStream().collect(Collectors.toList()))
.createMongoClient(settings.getIfAvailable());
@ConditionalOnMissingBean(MongoClient.class)
public MongoClient mongo(MongoProperties properties, ObjectProvider<MongoClientSettings> settings,
Environment environment) {
return new MongoClientFactory(properties, environment).createMongoClient(settings.getIfAvailable());
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -16,9 +16,14 @@
package org.springframework.boot.autoconfigure.mongo;
import java.util.Collections;
import java.util.List;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoClientSettings.Builder;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
@ -35,23 +40,90 @@ import org.springframework.core.env.Environment;
* @author Stephane Nicoll
* @author Nasko Vasilev
* @author Mark Paluch
* @author Christoph Strobl
* @since 2.0.0
*/
public class MongoClientFactory extends MongoClientFactorySupport<MongoClient> {
public class MongoClientFactory {
public MongoClientFactory(MongoProperties properties, Environment environment,
List<MongoClientSettingsBuilderCustomizer> builderCustomizers) {
super(properties, environment, builderCustomizers);
private final MongoProperties properties;
private final Environment environment;
public MongoClientFactory(MongoProperties properties, Environment environment) {
this.properties = properties;
this.environment = environment;
}
/**
* Creates a {@link MongoClient} using the given {@link MongoClientSettings settings}.
* If the environment contains a {@code local.mongo.port} property, it is used to
* configure a client to an embedded MongoDB instance.
* @param settings the settings
* @return the Mongo client
*/
public MongoClient createMongoClient(MongoClientSettings settings) {
Builder settingsBuilder = (settings != null) ? MongoClientSettings.builder(settings)
: MongoClientSettings.builder();
settingsBuilder.uuidRepresentation(this.properties.getUuidRepresentation());
Integer embeddedPort = getEmbeddedPort();
if (embeddedPort != null) {
return createEmbeddedMongoClient(settingsBuilder, embeddedPort);
}
return createNetworkMongoClient(settingsBuilder);
}
private Integer getEmbeddedPort() {
if (this.environment != null) {
String localPort = this.environment.getProperty("local.mongo.port");
if (localPort != null) {
return Integer.valueOf(localPort);
}
}
return null;
}
private MongoClient createEmbeddedMongoClient(Builder settings, int port) {
String host = (this.properties.getHost() != null) ? this.properties.getHost() : "localhost";
settings.applyToClusterSettings(
(cluster) -> cluster.hosts(Collections.singletonList(new ServerAddress(host, port))));
return MongoClients.create(settings.build());
}
private MongoClient createNetworkMongoClient(Builder settings) {
MongoProperties properties = this.properties;
if (properties.getUri() != null) {
return createMongoClient(properties.getUri(), settings);
}
if (hasCustomAddress() || hasCustomCredentials()) {
if (hasCustomCredentials()) {
String database = (this.properties.getAuthenticationDatabase() != null)
? this.properties.getAuthenticationDatabase() : this.properties.getMongoClientDatabase();
settings.credential((MongoCredential.createCredential(this.properties.getUsername(), database,
this.properties.getPassword())));
}
String host = getValue(properties.getHost(), "localhost");
int port = getValue(properties.getPort(), MongoProperties.DEFAULT_PORT);
List<ServerAddress> seeds = Collections.singletonList(new ServerAddress(host, port));
settings.applyToClusterSettings((cluster) -> cluster.hosts(seeds));
return MongoClients.create(settings.build());
}
return createMongoClient(MongoProperties.DEFAULT_URI, settings);
}
private MongoClient createMongoClient(String uri, Builder settings) {
settings.applyConnectionString(new ConnectionString(uri));
return MongoClients.create(settings.build());
}
private <T> T getValue(T value, T fallback) {
return (value != null) ? value : fallback;
}
protected MongoClient createNetworkMongoClient(MongoClientSettings settings) {
return MongoClients.create(settings, driverInformation());
private boolean hasCustomAddress() {
return this.properties.getHost() != null || this.properties.getPort() != null;
}
@Override
protected MongoClient createEmbeddedMongoClient(MongoClientSettings settings) {
return MongoClients.create(settings, driverInformation());
private boolean hasCustomCredentials() {
return this.properties.getUsername() != null && this.properties.getPassword() != null;
}
}

@ -1,144 +0,0 @@
/*
* Copyright 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.mongo;
import java.util.Collections;
import java.util.List;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoClientSettings.Builder;
import com.mongodb.MongoCredential;
import com.mongodb.MongoDriverInformation;
import com.mongodb.ServerAddress;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
/**
* Base class for common setup bits (aka {@link MongoClientSettings}) required for
* instantiating a MongoClient.
*
* @author Christoph Strobl
* @since 2.3.0
*/
public abstract class MongoClientFactorySupport<T> {
private final MongoProperties properties;
private final Environment environment;
private final List<MongoClientSettingsBuilderCustomizer> builderCustomizers;
public MongoClientFactorySupport(MongoProperties properties, Environment environment,
List<MongoClientSettingsBuilderCustomizer> builderCustomizers) {
this.properties = properties;
this.environment = environment;
this.builderCustomizers = (builderCustomizers != null) ? builderCustomizers : Collections.emptyList();
}
public T createMongoClient(MongoClientSettings settings) {
MongoClientSettings targetSettings = computeClientSettings(settings);
return (getEmbeddedPort() != null) ? createEmbeddedMongoClient(targetSettings)
: createNetworkMongoClient(targetSettings);
}
private MongoClientSettings computeClientSettings(MongoClientSettings settings) {
Builder settingsBuilder = (settings != null) ? MongoClientSettings.builder(settings)
: MongoClientSettings.builder();
applyHostAndPort(settingsBuilder);
applyCredentials(settingsBuilder);
customize(settingsBuilder);
return settingsBuilder.build();
}
private void applyHostAndPort(MongoClientSettings.Builder settings) {
if (isEmbedded()) {
settings.applyConnectionString(new ConnectionString("mongodb://localhost:" + getEmbeddedPort()));
return;
}
if (!this.properties.determineUri().equals(MongoProperties.DEFAULT_URI)) {
if (hasCustomAddress()) {
Assert.state(this.properties.getUri() == null,
"Invalid mongo configuration, either uri or host/port/credentials must be specified");
}
settings.applyConnectionString(new ConnectionString(this.properties.determineUri()));
}
else if (hasCustomAddress()) {
String host = getOrDefault(this.properties.getHost(), "localhost");
int port = getOrDefault(this.properties.getPort(), MongoProperties.DEFAULT_PORT);
ServerAddress serverAddress = new ServerAddress(host, port);
settings.applyToClusterSettings((cluster) -> cluster.hosts(Collections.singletonList(serverAddress)));
}
}
private void applyCredentials(Builder builder) {
if (hasCustomCredentials()) {
String database = (this.properties.getAuthenticationDatabase() != null)
? this.properties.getAuthenticationDatabase() : this.properties.getMongoClientDatabase();
builder.credential((MongoCredential.createCredential(this.properties.getUsername(), database,
this.properties.getPassword())));
}
}
private void customize(MongoClientSettings.Builder builder) {
for (MongoClientSettingsBuilderCustomizer customizer : this.builderCustomizers) {
customizer.customize(builder);
}
}
private <T> T getOrDefault(T value, T defaultValue) {
return (value != null) ? value : defaultValue;
}
protected abstract T createNetworkMongoClient(MongoClientSettings settings);
protected abstract T createEmbeddedMongoClient(MongoClientSettings settings);
private Integer getEmbeddedPort() {
if (this.environment != null) {
String localPort = this.environment.getProperty("local.mongo.port");
if (localPort != null) {
return Integer.valueOf(localPort);
}
}
return null;
}
private boolean isEmbedded() {
return getEmbeddedPort() != null;
}
private boolean hasCustomCredentials() {
return this.properties.getUsername() != null && this.properties.getPassword() != null;
}
private boolean hasCustomAddress() {
return this.properties.getHost() != null || this.properties.getPort() != null;
}
protected static MongoDriverInformation driverInformation() {
return MongoDriverInformation.builder(MongoDriverInformation.builder().build()).driverName("spring-boot")
.build();
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.mongo;
import com.mongodb.ConnectionString;
import org.bson.UuidRepresentation;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ -93,6 +94,11 @@ public class MongoProperties {
*/
private Class<?> fieldNamingStrategy;
/**
* Representation to use when converting a UUID to a BSON binary value.
*/
private UuidRepresentation uuidRepresentation = UuidRepresentation.JAVA_LEGACY;
/**
* Whether to enable auto-index creation.
*/
@ -146,6 +152,14 @@ public class MongoProperties {
this.fieldNamingStrategy = fieldNamingStrategy;
}
public UuidRepresentation getUuidRepresentation() {
return this.uuidRepresentation;
}
public void setUuidRepresentation(UuidRepresentation uuidRepresentation) {
this.uuidRepresentation = uuidRepresentation;
}
public String getUri() {
return this.uri;
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -16,36 +16,137 @@
package org.springframework.boot.autoconfigure.mongo;
import java.util.Collections;
import java.util.List;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoClientSettings.Builder;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.reactivestreams.client.MongoClient;
import com.mongodb.reactivestreams.client.MongoClients;
import org.springframework.core.env.Environment;
import org.springframework.util.Assert;
/**
* A factory for a reactive {@link MongoClient} that applies {@link MongoProperties}.
*
* @author Mark Paluch
* @author Stephane Nicoll
* @author Christoph Strobl
* @since 2.0.0
*/
public class ReactiveMongoClientFactory extends MongoClientFactorySupport<MongoClient> {
public class ReactiveMongoClientFactory {
private final MongoProperties properties;
private final Environment environment;
private final List<MongoClientSettingsBuilderCustomizer> builderCustomizers;
public ReactiveMongoClientFactory(MongoProperties properties, Environment environment,
List<MongoClientSettingsBuilderCustomizer> builderCustomizers) {
super(properties, environment, builderCustomizers);
this.properties = properties;
this.environment = environment;
this.builderCustomizers = (builderCustomizers != null) ? builderCustomizers : Collections.emptyList();
}
/**
* Creates a {@link MongoClient} using the given {@code settings}. If the environment
* contains a {@code local.mongo.port} property, it is used to configure a client to
* an embedded MongoDB instance.
* @param settings the settings
* @return the Mongo client
*/
public MongoClient createMongoClient(MongoClientSettings settings) {
Integer embeddedPort = getEmbeddedPort();
if (embeddedPort != null) {
return createEmbeddedMongoClient(settings, embeddedPort);
}
return createNetworkMongoClient(settings);
}
private Integer getEmbeddedPort() {
if (this.environment != null) {
String localPort = this.environment.getProperty("local.mongo.port");
if (localPort != null) {
return Integer.valueOf(localPort);
}
}
return null;
}
private MongoClient createEmbeddedMongoClient(MongoClientSettings settings, int port) {
Builder builder = builder(settings);
String host = (this.properties.getHost() != null) ? this.properties.getHost() : "localhost";
builder.applyToClusterSettings(
(cluster) -> cluster.hosts(Collections.singletonList(new ServerAddress(host, port))));
return createMongoClient(builder);
}
private MongoClient createNetworkMongoClient(MongoClientSettings settings) {
if (hasCustomAddress() || hasCustomCredentials()) {
return createCredentialNetworkMongoClient(settings);
}
ConnectionString connectionString = new ConnectionString(this.properties.determineUri());
return createMongoClient(createBuilder(settings, connectionString));
}
private MongoClient createCredentialNetworkMongoClient(MongoClientSettings settings) {
Assert.state(this.properties.getUri() == null,
"Invalid mongo configuration, either uri or host/port/credentials must be specified");
Builder builder = builder(settings);
if (hasCustomCredentials()) {
applyCredentials(builder);
}
String host = getOrDefault(this.properties.getHost(), "localhost");
int port = getOrDefault(this.properties.getPort(), MongoProperties.DEFAULT_PORT);
ServerAddress serverAddress = new ServerAddress(host, port);
builder.applyToClusterSettings((cluster) -> cluster.hosts(Collections.singletonList(serverAddress)));
return createMongoClient(builder);
}
private void applyCredentials(Builder builder) {
String database = (this.properties.getAuthenticationDatabase() != null)
? this.properties.getAuthenticationDatabase() : this.properties.getMongoClientDatabase();
builder.credential((MongoCredential.createCredential(this.properties.getUsername(), database,
this.properties.getPassword())));
}
private <T> T getOrDefault(T value, T defaultValue) {
return (value != null) ? value : defaultValue;
}
private MongoClient createMongoClient(Builder builder) {
builder.uuidRepresentation(this.properties.getUuidRepresentation());
customize(builder);
return MongoClients.create(builder.build());
}
private Builder createBuilder(MongoClientSettings settings, ConnectionString connection) {
return builder(settings).applyConnectionString(connection);
}
private void customize(MongoClientSettings.Builder builder) {
for (MongoClientSettingsBuilderCustomizer customizer : this.builderCustomizers) {
customizer.customize(builder);
}
}
private boolean hasCustomAddress() {
return this.properties.getHost() != null || this.properties.getPort() != null;
}
protected MongoClient createNetworkMongoClient(MongoClientSettings settings) {
return MongoClients.create(settings, driverInformation());
private boolean hasCustomCredentials() {
return this.properties.getUsername() != null && this.properties.getPassword() != null;
}
@Override
protected MongoClient createEmbeddedMongoClient(MongoClientSettings settings) {
return MongoClients.create(settings, driverInformation());
private Builder builder(MongoClientSettings settings) {
if (settings == null) {
return MongoClientSettings.builder();
}
return MongoClientSettings.builder(settings);
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.

@ -398,6 +398,10 @@
"name": "spring.data.mongodb.uri",
"defaultValue": "mongodb://localhost/test"
},
{
"name": "spring.data.mongodb.uuid-representation",
"defaultValue": "java-legacy"
},
{
"name": "spring.data.neo4j.auto-index",
"defaultValue": "none"

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -168,7 +168,7 @@ class MongoDataAutoConfigurationTests {
}
@Test
void createsMongoDbFactoryForPreferredMongoClient() {
void createsMongoDatabaseFactoryForPreferredMongoClient() {
this.contextRunner.run((context) -> {
MongoDatabaseFactory dbFactory = context.getBean(MongoDatabaseFactory.class);
assertThat(dbFactory).isInstanceOf(SimpleMongoClientDatabaseFactory.class);
@ -176,7 +176,7 @@ class MongoDataAutoConfigurationTests {
}
@Test
void createsMongoDbFactoryForFallbackMongoClient() {
void createsMongoDatabaseFactoryForFallbackMongoClient() {
this.contextRunner.withUserConfiguration(FallbackMongoClientConfiguration.class).run((context) -> {
MongoDatabaseFactory dbFactory = context.getBean(MongoDatabaseFactory.class);
assertThat(dbFactory).isInstanceOf(SimpleMongoClientDatabaseFactory.class);
@ -184,8 +184,8 @@ class MongoDataAutoConfigurationTests {
}
@Test
void autoConfiguresIfUserProvidesMongoDbFactoryButNoClient() {
this.contextRunner.withUserConfiguration(MongoDbFactoryConfiguration.class)
void autoConfiguresIfUserProvidesMongoDatabaseFactoryButNoClient() {
this.contextRunner.withUserConfiguration(MongoDatabaseFactoryConfiguration.class)
.run((context) -> assertThat(context).hasSingleBean(MongoTemplate.class));
}
@ -222,10 +222,10 @@ class MongoDataAutoConfigurationTests {
}
@Configuration(proxyBeanMethods = false)
static class MongoDbFactoryConfiguration {
static class MongoDatabaseFactoryConfiguration {
@Bean
MongoDatabaseFactory mongoDbFactory() {
MongoDatabaseFactory mongoDatabaseFactory() {
return new SimpleMongoClientDatabaseFactory(MongoClients.create(), "test");
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -18,8 +18,6 @@ package org.springframework.boot.autoconfigure.mongo;
import java.util.concurrent.TimeUnit;
import javax.net.SocketFactory;
import com.mongodb.MongoClientSettings;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
@ -32,7 +30,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.test.util.ReflectionTestUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Tests for {@link MongoAutoConfiguration}.
@ -51,25 +48,25 @@ class MongoAutoConfigurationTests {
}
@Test
void optionsAdded() {
this.contextRunner.withUserConfiguration(OptionsConfig.class)
void settingsAdded() {
this.contextRunner.withUserConfiguration(SettingsConfig.class)
.run((context) -> assertThat(extractClientSettings(context.getBean(MongoClient.class))
.getSocketSettings().getConnectTimeout(TimeUnit.MILLISECONDS)).isEqualTo(300));
}
@Test
void optionsAddedButNoHost() {
this.contextRunner.withUserConfiguration(OptionsConfig.class)
void settingsAddedButNoHost() {
this.contextRunner.withUserConfiguration(SettingsConfig.class)
.run((context) -> assertThat(extractClientSettings(context.getBean(MongoClient.class))
.getSocketSettings().getConnectTimeout(TimeUnit.MILLISECONDS)).isEqualTo(300));
}
@Test
void optionsSslConfig() {
this.contextRunner.withUserConfiguration(SslOptionsConfig.class).run((context) -> {
void settingsSslConfig() {
this.contextRunner.withUserConfiguration(SslSettingsConfig.class).run((context) -> {
assertThat(context).hasSingleBean(MongoClient.class);
MongoClientSettings options = extractClientSettings(context.getBean(MongoClient.class));
assertThat(options.getSslSettings().isEnabled()).isTrue();
MongoClientSettings settings = extractClientSettings(context.getBean(MongoClient.class));
assertThat(settings.getSslSettings().isEnabled()).isTrue();
});
}
@ -84,27 +81,22 @@ class MongoAutoConfigurationTests {
}
@Configuration(proxyBeanMethods = false)
static class OptionsConfig {
static class SettingsConfig {
@Bean
MongoClientSettings mongoOptions() {
return MongoClientSettings.builder()
.applyToSocketSettings((it) -> it.connectTimeout(300, TimeUnit.MILLISECONDS)).build();
MongoClientSettings mongoClientSettings() {
return MongoClientSettings.builder().applyToSocketSettings(
(socketSettings) -> socketSettings.connectTimeout(300, TimeUnit.MILLISECONDS)).build();
}
}
@Configuration(proxyBeanMethods = false)
static class SslOptionsConfig {
@Bean
MongoClientSettings mongoClientOptions(SocketFactory socketFactory) {
return MongoClientSettings.builder().applyToSslSettings((it) -> it.enabled(true)).build();
}
static class SslSettingsConfig {
@Bean
SocketFactory mySocketFactory() {
return mock(SocketFactory.class);
MongoClientSettings mongoClientSettings() {
return MongoClientSettings.builder().applyToSslSettings((ssl) -> ssl.enabled(true)).build();
}
}
@ -113,7 +105,7 @@ class MongoAutoConfigurationTests {
static class FallbackMongoClientConfig {
@Bean
com.mongodb.client.MongoClient fallbackMongoClient() {
MongoClient fallbackMongoClient() {
return MongoClients.create();
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -16,17 +16,15 @@
package org.springframework.boot.autoconfigure.mongo;
import java.util.Collections;
import java.util.List;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoClient;
import org.bson.UuidRepresentation;
import org.junit.jupiter.api.Test;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.util.ReflectionTestUtils;
@ -71,7 +69,7 @@ class MongoClientFactoryTests {
properties.setUsername("user");
properties.setPassword("secret".toCharArray());
MongoClient client = createMongoClient(properties);
assertMongoCredential(getCredentials(client).get(0), "user", "secret", "test");
assertMongoCredential(getClientSettings(client).getCredential(), "user", "secret", "test");
}
@Test
@ -81,7 +79,22 @@ class MongoClientFactoryTests {
properties.setUsername("user");
properties.setPassword("secret".toCharArray());
MongoClient client = createMongoClient(properties);
assertMongoCredential(getCredentials(client).get(0), "user", "secret", "foo");
assertMongoCredential(getClientSettings(client).getCredential(), "user", "secret", "foo");
}
@Test
void uuidRepresentationDefaultToJavaLegacy() {
MongoProperties properties = new MongoProperties();
MongoClient client = createMongoClient(properties);
assertThat(getClientSettings(client).getUuidRepresentation()).isEqualTo(UuidRepresentation.JAVA_LEGACY);
}
@Test
void uuidRepresentationCanBeCustomized() {
MongoProperties properties = new MongoProperties();
properties.setUuidRepresentation(UuidRepresentation.STANDARD);
MongoClient client = createMongoClient(properties);
assertThat(getClientSettings(client).getUuidRepresentation()).isEqualTo(UuidRepresentation.STANDARD);
}
@Test
@ -91,7 +104,7 @@ class MongoClientFactoryTests {
properties.setUsername("user");
properties.setPassword("secret".toCharArray());
MongoClient client = createMongoClient(properties);
assertMongoCredential(getCredentials(client).get(0), "user", "secret", "foo");
assertMongoCredential(getClientSettings(client).getCredential(), "user", "secret", "foo");
}
@Test
@ -103,9 +116,7 @@ class MongoClientFactoryTests {
assertThat(allAddresses).hasSize(2);
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456);
List<MongoCredential> credentialsList = getCredentials(client);
assertThat(credentialsList).hasSize(1);
assertMongoCredential(credentialsList.get(0), "user", "secret", "test");
assertMongoCredential(getClientSettings(client).getCredential(), "user", "secret", "test");
}
@Test
@ -124,22 +135,15 @@ class MongoClientFactoryTests {
}
private MongoClient createMongoClient(MongoProperties properties, Environment environment) {
return new MongoClientFactory(properties, environment, Collections.emptyList()).createMongoClient(null);
return new MongoClientFactory(properties, environment).createMongoClient(null);
}
@SuppressWarnings("deprecation")
private List<ServerAddress> getAllAddresses(MongoClient client) {
// At some point we'll probably need to use reflection to find the address but for
// now, we can use the deprecated getAllAddress method.
return client.getClusterDescription().getClusterSettings().getHosts();
}
@SuppressWarnings("deprecation")
private List<MongoCredential> getCredentials(MongoClient client) {
// At some point we'll probably need to use reflection to find the credentials but
// for now, we can use the deprecated getCredentialsList method.
return Collections.singletonList(
((MongoClientSettings) ReflectionTestUtils.getField(client, "settings")).getCredential());
private MongoClientSettings getClientSettings(MongoClient client) {
return (MongoClientSettings) ReflectionTestUtils.getField(client, "settings");
}
private void assertServerAddress(ServerAddress serverAddress, String expectedHost, int expectedPort) {
@ -154,10 +158,4 @@ class MongoClientFactoryTests {
assertThat(credentials.getSource()).isEqualTo(expectedSource);
}
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(MongoProperties.class)
static class Config {
}
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -16,7 +16,6 @@
package org.springframework.boot.autoconfigure.mongo;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;
@ -56,8 +55,7 @@ class MongoPropertiesTests {
}
@Test
@SuppressWarnings("deprecation")
void allMongoClientOptionsCanBeSet() {
void allMongoClientSettingsCanBeSet() {
MongoClientSettings.Builder builder = MongoClientSettings.builder();
builder.applyToSocketSettings((settings) -> {
settings.connectTimeout(1000, TimeUnit.MILLISECONDS);
@ -65,17 +63,15 @@ class MongoPropertiesTests {
}).applyToServerSettings((settings) -> {
settings.heartbeatFrequency(10001, TimeUnit.MILLISECONDS);
settings.minHeartbeatFrequency(501, TimeUnit.MILLISECONDS);
}).applyToClusterSettings((settings) -> settings.requiredReplicaSetName("testReplicaSetName"))
.applyToConnectionPoolSettings((settings) -> {
settings.maxWaitTime(120001, TimeUnit.MILLISECONDS);
settings.maxConnectionLifeTime(60000, TimeUnit.MILLISECONDS);
settings.maxConnectionIdleTime(60000, TimeUnit.MILLISECONDS);
}).applyToSslSettings((settings) -> settings.enabled(true)).applicationName("test");
}).applyToConnectionPoolSettings((settings) -> {
settings.maxWaitTime(120001, TimeUnit.MILLISECONDS);
settings.maxConnectionLifeTime(60000, TimeUnit.MILLISECONDS);
settings.maxConnectionIdleTime(60000, TimeUnit.MILLISECONDS);
}).applyToSslSettings((settings) -> settings.enabled(true)).applicationName("test");
MongoClientSettings settings = builder.build();
MongoProperties properties = new MongoProperties();
MongoClient client = new MongoClientFactory(properties, null, Collections.emptyList())
.createMongoClient(settings);
MongoClient client = createMongoClient(properties, settings);
MongoClientSettings wrapped = (MongoClientSettings) ReflectionTestUtils.getField(client, "settings");
assertThat(wrapped.getSocketSettings().getConnectTimeout(TimeUnit.MILLISECONDS))
.isEqualTo(settings.getSocketSettings().getConnectTimeout(TimeUnit.MILLISECONDS));
@ -86,8 +82,6 @@ class MongoPropertiesTests {
assertThat(wrapped.getServerSettings().getMinHeartbeatFrequency(TimeUnit.MILLISECONDS))
.isEqualTo(settings.getServerSettings().getMinHeartbeatFrequency(TimeUnit.MILLISECONDS));
assertThat(wrapped.getApplicationName()).isEqualTo(settings.getApplicationName());
assertThat(wrapped.getClusterSettings().getRequiredReplicaSetName())
.isEqualTo(settings.getClusterSettings().getRequiredReplicaSetName());
assertThat(wrapped.getConnectionPoolSettings().getMaxWaitTime(TimeUnit.MILLISECONDS))
.isEqualTo(settings.getConnectionPoolSettings().getMaxWaitTime(TimeUnit.MILLISECONDS));
assertThat(wrapped.getConnectionPoolSettings().getMaxConnectionLifeTime(TimeUnit.MILLISECONDS))
@ -103,7 +97,7 @@ class MongoPropertiesTests {
properties.setHost("localhost");
properties.setPort(27017);
properties.setUri("mongodb://mongo1.example.com:12345");
MongoClient client = new MongoClientFactory(properties, null, Collections.emptyList()).createMongoClient(null);
MongoClient client = createMongoClient(properties);
List<ServerAddress> allAddresses = getAllAddresses(client);
assertThat(allAddresses).hasSize(1);
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
@ -114,7 +108,7 @@ class MongoPropertiesTests {
MongoProperties properties = new MongoProperties();
properties.setHost("localhost");
properties.setPort(27017);
MongoClient client = new MongoClientFactory(properties, null, Collections.emptyList()).createMongoClient(null);
MongoClient client = createMongoClient(properties);
List<ServerAddress> allAddresses = getAllAddresses(client);
assertThat(allAddresses).hasSize(1);
assertServerAddress(allAddresses.get(0), "localhost", 27017);
@ -124,7 +118,7 @@ class MongoPropertiesTests {
void onlyUriSetShouldUseThat() {
MongoProperties properties = new MongoProperties();
properties.setUri("mongodb://mongo1.example.com:12345");
MongoClient client = new MongoClientFactory(properties, null, Collections.emptyList()).createMongoClient(null);
MongoClient client = createMongoClient(properties);
List<ServerAddress> allAddresses = getAllAddresses(client);
assertThat(allAddresses).hasSize(1);
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
@ -133,16 +127,21 @@ class MongoPropertiesTests {
@Test
void noCustomAddressAndNoUriUsesDefaultUri() {
MongoProperties properties = new MongoProperties();
MongoClient client = new MongoClientFactory(properties, null, Collections.emptyList()).createMongoClient(null);
MongoClient client = createMongoClient(properties);
List<ServerAddress> allAddresses = getAllAddresses(client);
assertThat(allAddresses).hasSize(1);
assertServerAddress(allAddresses.get(0), "127.0.0.1", 27017);
assertServerAddress(allAddresses.get(0), "localhost", 27017);
}
private MongoClient createMongoClient(MongoProperties properties, MongoClientSettings settings) {
return new MongoClientFactory(properties, null).createMongoClient(settings);
}
private MongoClient createMongoClient(MongoProperties properties) {
return createMongoClient(properties, null);
}
@SuppressWarnings("deprecation")
private List<ServerAddress> getAllAddresses(MongoClient client) {
// At some point we'll probably need to use reflection to find the address but for
// now, we can use the deprecated getAllAddress method.
return client.getClusterDescription().getClusterSettings().getHosts();
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -59,25 +59,25 @@ class MongoReactiveAutoConfigurationTests {
}
@Test
void optionsAdded() {
void settingsAdded() {
this.contextRunner.withPropertyValues("spring.data.mongodb.host:localhost")
.withUserConfiguration(OptionsConfig.class)
.withUserConfiguration(SettingsConfig.class)
.run((context) -> assertThat(getSettings(context).getSocketSettings().getReadTimeout(TimeUnit.SECONDS))
.isEqualTo(300));
}
@Test
void optionsAddedButNoHost() {
void settingsAddedButNoHost() {
this.contextRunner.withPropertyValues("spring.data.mongodb.uri:mongodb://localhost/test")
.withUserConfiguration(OptionsConfig.class)
.withUserConfiguration(SettingsConfig.class)
.run((context) -> assertThat(getSettings(context).getReadPreference())
.isEqualTo(ReadPreference.nearest()));
}
@Test
void optionsSslConfig() {
void settingsSslConfig() {
this.contextRunner.withPropertyValues("spring.data.mongodb.uri:mongodb://localhost/test")
.withUserConfiguration(SslOptionsConfig.class).run((context) -> {
.withUserConfiguration(SslSettingsConfig.class).run((context) -> {
assertThat(context).hasSingleBean(MongoClient.class);
MongoClientSettings settings = getSettings(context);
assertThat(settings.getApplicationName()).isEqualTo("test-config");
@ -111,7 +111,6 @@ class MongoReactiveAutoConfigurationTests {
});
}
@SuppressWarnings("deprecation")
private MongoClientSettings getSettings(ApplicationContext context) {
MongoClient client = context.getBean(MongoClient.class);
AsyncMongoClient wrappedClient = (AsyncMongoClient) ReflectionTestUtils.getField(client, "wrapped");
@ -119,7 +118,7 @@ class MongoReactiveAutoConfigurationTests {
}
@Configuration(proxyBeanMethods = false)
static class OptionsConfig {
static class SettingsConfig {
@Bean
MongoClientSettings mongoClientSettings() {
@ -130,7 +129,7 @@ class MongoReactiveAutoConfigurationTests {
}
@Configuration(proxyBeanMethods = false)
static class SslOptionsConfig {
static class SslSettingsConfig {
@Bean
MongoClientSettings mongoClientSettings(StreamFactoryFactory streamFactoryFactory) {

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -25,6 +25,7 @@ import com.mongodb.ServerAddress;
import com.mongodb.connection.ClusterSettings;
import com.mongodb.internal.async.client.AsyncMongoClient;
import com.mongodb.reactivestreams.client.MongoClient;
import org.bson.UuidRepresentation;
import org.junit.jupiter.api.Test;
import org.springframework.core.env.Environment;
@ -73,7 +74,7 @@ class ReactiveMongoClientFactoryTests {
properties.setUsername("user");
properties.setPassword("secret".toCharArray());
MongoClient client = createMongoClient(properties);
assertMongoCredential(extractMongoCredentials(client), "user", "secret", "test");
assertMongoCredential(getClientSettings(client).getCredential(), "user", "secret", "test");
}
@Test
@ -83,7 +84,22 @@ class ReactiveMongoClientFactoryTests {
properties.setUsername("user");
properties.setPassword("secret".toCharArray());
MongoClient client = createMongoClient(properties);
assertMongoCredential(extractMongoCredentials(client), "user", "secret", "foo");
assertMongoCredential(getClientSettings(client).getCredential(), "user", "secret", "foo");
}
@Test
void uuidRepresentationDefaultToJavaLegacy() {
MongoProperties properties = new MongoProperties();
MongoClient client = createMongoClient(properties);
assertThat(getClientSettings(client).getUuidRepresentation()).isEqualTo(UuidRepresentation.JAVA_LEGACY);
}
@Test
void uuidRepresentationCanBeCustomized() {
MongoProperties properties = new MongoProperties();
properties.setUuidRepresentation(UuidRepresentation.STANDARD);
MongoClient client = createMongoClient(properties);
assertThat(getClientSettings(client).getUuidRepresentation()).isEqualTo(UuidRepresentation.STANDARD);
}
@Test
@ -93,7 +109,7 @@ class ReactiveMongoClientFactoryTests {
properties.setUsername("user");
properties.setPassword("secret".toCharArray());
MongoClient client = createMongoClient(properties);
assertMongoCredential(extractMongoCredentials(client), "user", "secret", "foo");
assertMongoCredential(getClientSettings(client).getCredential(), "user", "secret", "foo");
}
@Test
@ -105,8 +121,7 @@ class ReactiveMongoClientFactoryTests {
assertThat(allAddresses).hasSize(2);
assertServerAddress(allAddresses.get(0), "mongo1.example.com", 12345);
assertServerAddress(allAddresses.get(1), "mongo2.example.com", 23456);
MongoCredential credential = extractMongoCredentials(client);
assertMongoCredential(credential, "user", "secret", "test");
assertMongoCredential(getClientSettings(client).getCredential(), "user", "secret", "test");
}
@Test
@ -114,17 +129,17 @@ class ReactiveMongoClientFactoryTests {
MongoProperties properties = new MongoProperties();
properties.setUri("mongodb://localhost/test?retryWrites=true");
MongoClient client = createMongoClient(properties);
assertThat(getSettings(client).getRetryWrites()).isTrue();
assertThat(getClientSettings(client).getRetryWrites()).isTrue();
}
@Test
void uriCanBeSetWithCredentials() {
void uriCannotBeSetWithCredentials() {
MongoProperties properties = new MongoProperties();
properties.setUri("mongodb://127.0.0.1:1234/mydb");
properties.setUsername("user");
properties.setPassword("secret".toCharArray());
MongoCredential credential = extractMongoCredentials(createMongoClient(properties));
assertMongoCredential(credential, "user", "secret", "mydb");
assertThatIllegalStateException().isThrownBy(() -> createMongoClient(properties)).withMessageContaining(
"Invalid mongo configuration, either uri or host/port/credentials must be specified");
}
@Test
@ -185,17 +200,12 @@ class ReactiveMongoClientFactoryTests {
}
private List<ServerAddress> extractServerAddresses(MongoClient client) {
MongoClientSettings settings = getSettings(client);
MongoClientSettings settings = getClientSettings(client);
ClusterSettings clusterSettings = settings.getClusterSettings();
return clusterSettings.getHosts();
}
private MongoCredential extractMongoCredentials(MongoClient client) {
return getSettings(client).getCredential();
}
@SuppressWarnings("deprecation")
private MongoClientSettings getSettings(MongoClient client) {
private MongoClientSettings getClientSettings(MongoClient client) {
AsyncMongoClient wrapped = (AsyncMongoClient) ReflectionTestUtils.getField(client, "wrapped");
return (MongoClientSettings) ReflectionTestUtils.getField(wrapped, "settings");
}

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
@ -237,11 +237,8 @@ class EmbeddedMongoAutoConfigurationTests {
return File.separatorChar == '\\';
}
@SuppressWarnings("deprecation")
private int getPort(MongoClient client) {
// At some point we'll probably need to use reflection to find the address but for
// now, we can use the deprecated getAddress method.
return client.getClusterDescription().getClusterSettings().getHosts().iterator().next().getPort();
return client.getClusterDescription().getClusterSettings().getHosts().get(0).getPort();
}
@Configuration(proxyBeanMethods = false)

@ -1196,10 +1196,10 @@ bom {
group("org.mongodb") {
modules = [
"bson",
"mongodb-driver-sync",
"mongodb-driver-async",
"mongodb-driver-core",
"mongodb-driver-reactivestreams"
"mongodb-driver-legacy",
"mongodb-driver-reactivestreams",
"mongodb-driver-sync"
]
}
}

@ -3833,19 +3833,19 @@ Spring Boot offers several conveniences for working with MongoDB, including the
[[boot-features-connecting-to-mongodb]]
==== Connecting to a MongoDB Database
To access Mongo databases, you can inject an auto-configured `org.springframework.data.mongodb.MongoDbFactory`.
To access Mongo databases, you can inject an auto-configured `org.springframework.data.mongodb.MongoDatabaseFactory`.
By default, the instance tries to connect to a MongoDB server at `mongodb://localhost/test`.
The following example shows how to connect to a MongoDB database:
[source,java,indent=0]
----
import org.springframework.data.mongodb.MongoDbFactory;
import com.mongodb.DB;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import com.mongodb.client.MongoDatabase;
@Component
public class MyBean {
private final MongoDbFactory mongo;
private final MongoDatabaseFactory mongo;
@Autowired
public MyBean(MongoDbFactory mongo) {
@ -3855,7 +3855,7 @@ The following example shows how to connect to a MongoDB database:
// ...
public void example() {
DB db = mongo.getDb();
MongoDatabase db = mongo.getMongoDatabase();
// ...
}
@ -3869,7 +3869,7 @@ You can set the configprop:spring.data.mongodb.uri[] property to change the URL
spring.data.mongodb.uri=mongodb://user:secret@mongo1.example.com:12345,mongo2.example.com:23456/test
----
Alternatively, as long as you use Mongo 2.x, you can specify a `host`/`port`.
Alternatively, you can specify a `host`/`port`.
For example, you might declare the following settings in your `application.properties`:
[source,properties,indent=0,configprops]
@ -3878,8 +3878,7 @@ For example, you might declare the following settings in your `application.prope
spring.data.mongodb.port=27017
----
If you have defined your own `MongoClient`, it will be used to auto-configure a suitable `MongoDbFactory`.
Both `com.mongodb.MongoClient` and `com.mongodb.client.MongoClient` are supported.
If you have defined your own `MongoClient`, it will be used to auto-configure a suitable `MongoDatabaseFactory`.
NOTE: If you use the Mongo 3.0 Java driver, `spring.data.mongodb.host` and `spring.data.mongodb.port` are not supported.
In such cases, `spring.data.mongodb.uri` should be used to provide all of the configuration.
@ -3887,8 +3886,8 @@ In such cases, `spring.data.mongodb.uri` should be used to provide all of the co
TIP: If `spring.data.mongodb.port` is not specified, the default of `27017` is used.
You could delete this line from the example shown earlier.
TIP: If you do not use Spring Data Mongo, you can inject `com.mongodb.MongoClient` beans instead of using `MongoDbFactory`.
If you want to take complete control of establishing the MongoDB connection, you can also declare your own `MongoDbFactory` or `MongoClient` bean.
TIP: If you do not use Spring Data Mongo, you can inject a `MongoClient` bean instead of using `MongoDatabaseFactory`.
If you want to take complete control of establishing the MongoDB connection, you can also declare your own `MongoDatabaseFactory` or `MongoClient` bean.
NOTE: If you are using the reactive driver, Netty is required for SSL.
The auto-configuration configures this factory automatically if Netty is available and the factory to use hasn't been customized already.
@ -3902,7 +3901,6 @@ As with `JdbcTemplate`, Spring Boot auto-configures a bean for you to inject the
[source,java,indent=0]
----
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
@ -3911,7 +3909,6 @@ As with `JdbcTemplate`, Spring Boot auto-configures a bean for you to inject the
private final MongoTemplate mongoTemplate;
@Autowired
public MyBean(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}

@ -8,9 +8,6 @@ dependencies {
api(platform(project(":spring-boot-project:spring-boot-dependencies")))
api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
api("io.projectreactor:reactor-core")
api("org.mongodb:mongodb-driver-sync")
api("org.mongodb:mongodb-driver-reactivestreams")
api("org.springframework.data:spring-data-mongodb") {
exclude group: "org.mongodb", module: "mongo-java-driver"
}
api("org.springframework.data:spring-data-mongodb")
}

@ -7,8 +7,6 @@ description = "Starter for using MongoDB document-oriented database and Spring D
dependencies {
api(platform(project(":spring-boot-project:spring-boot-dependencies")))
api(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
api("org.mongodb:mongodb-driver-core")
api("org.springframework.data:spring-data-mongodb") {
exclude group: "org.mongodb", module: "mongo-java-driver"
}
api("org.mongodb:mongodb-driver-sync")
api("org.springframework.data:spring-data-mongodb")
}

@ -45,8 +45,8 @@ dependencies {
optional("org.springframework.security:spring-security-config")
optional("org.springframework.security:spring-security-test")
optional("org.apache.tomcat.embed:tomcat-embed-core")
optional("org.mongodb:mongodb-driver-sync")
optional("org.mongodb:mongodb-driver-reactivestreams")
optional("org.mongodb:mongodb-driver-sync")
testImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
testImplementation("ch.qos.logback:logback-classic")

Loading…
Cancel
Save