Skip to content

Commit c4daff7

Browse files
committed
Polish "Upgrade to MongoDB Java Driver 4.0 beta1"
See gh-19960
1 parent d2d6dbd commit c4daff7

29 files changed

+381
-380
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ dependencies {
8383
optional("org.influxdb:influxdb-java")
8484
optional("org.jolokia:jolokia-core")
8585
optional("org.liquibase:liquibase-core")
86-
optional("org.mongodb:mongodb-driver-sync")
8786
optional("org.mongodb:mongodb-driver-reactivestreams")
87+
optional("org.mongodb:mongodb-driver-sync")
8888
optional("org.springframework:spring-jdbc")
8989
optional("org.springframework:spring-jms")
9090
optional("org.springframework:spring-messaging")

spring-boot-project/spring-boot-actuator/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ dependencies {
4343
optional("org.hibernate.validator:hibernate-validator")
4444
optional("org.influxdb:influxdb-java")
4545
optional("org.liquibase:liquibase-core")
46-
optional("org.mongodb:mongodb-driver-sync")
4746
optional("org.mongodb:mongodb-driver-reactivestreams")
47+
optional("org.mongodb:mongodb-driver-sync")
4848
optional("org.springframework:spring-jdbc")
4949
optional("org.springframework:spring-messaging")
5050
optional("org.springframework:spring-webflux")

spring-boot-project/spring-boot-autoconfigure/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ dependencies {
9595
optional("org.jooq:jooq")
9696
optional("org.liquibase:liquibase-core")
9797
optional("org.messaginghub:pooled-jms")
98-
optional("org.mongodb:mongodb-driver-sync")
9998
optional("org.mongodb:mongodb-driver-reactivestreams")
99+
optional("org.mongodb:mongodb-driver-sync")
100100
optional("org.quartz-scheduler:quartz")
101101
optional("org.springframework:spring-jdbc")
102102
optional("org.springframework.integration:spring-integration-core")

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoClientDependsOnBeanFactoryPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoDataAutoConfiguration.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.autoconfigure.data.mongo;
1818

19+
import com.mongodb.client.MongoClient;
20+
1921
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2022
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2123
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -46,9 +48,10 @@
4648
* @since 1.1.0
4749
*/
4850
@Configuration(proxyBeanMethods = false)
49-
@ConditionalOnClass({ com.mongodb.client.MongoClient.class, MongoTemplate.class })
51+
@ConditionalOnClass({ MongoClient.class, MongoTemplate.class })
5052
@EnableConfigurationProperties(MongoProperties.class)
51-
@Import({ MongoDataConfiguration.class, MongoDbFactoryConfiguration.class, MongoDbFactoryDependentConfiguration.class })
53+
@Import({ MongoDataConfiguration.class, MongoDatabaseFactoryConfiguration.class,
54+
MongoDatabaseFactoryDependentConfiguration.class })
5255
@AutoConfigureAfter(MongoAutoConfiguration.class)
5356
public class MongoDataAutoConfiguration {
5457

Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,14 +18,10 @@
1818

1919
import com.mongodb.client.MongoClient;
2020

21-
import org.springframework.beans.factory.ObjectProvider;
22-
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
23-
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2421
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
25-
import org.springframework.boot.autoconfigure.data.mongo.MongoDbFactoryConfiguration.AnyMongoClientAvailable;
22+
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
2623
import org.springframework.boot.autoconfigure.mongo.MongoProperties;
2724
import org.springframework.context.annotation.Bean;
28-
import org.springframework.context.annotation.Conditional;
2925
import org.springframework.context.annotation.Configuration;
3026
import org.springframework.data.mongodb.MongoDatabaseFactory;
3127
import org.springframework.data.mongodb.core.MongoDatabaseFactorySupport;
@@ -35,43 +31,16 @@
3531
* Configuration for a {@link MongoDatabaseFactory}.
3632
*
3733
* @author Andy Wilkinson
34+
* @author Stephane Nicoll
3835
*/
3936
@Configuration(proxyBeanMethods = false)
4037
@ConditionalOnMissingBean(MongoDatabaseFactory.class)
41-
@Conditional(AnyMongoClientAvailable.class)
42-
class MongoDbFactoryConfiguration {
38+
@ConditionalOnSingleCandidate(MongoClient.class)
39+
class MongoDatabaseFactoryConfiguration {
4340

4441
@Bean
45-
MongoDatabaseFactorySupport<?> mongoDbFactory(ObjectProvider<MongoClient> mongoClient, MongoProperties properties) {
46-
47-
com.mongodb.client.MongoClient fallbackClient = mongoClient.getIfAvailable();
48-
if (fallbackClient != null) {
49-
return new SimpleMongoClientDatabaseFactory(fallbackClient, properties.getMongoClientDatabase());
50-
}
51-
throw new IllegalStateException("Expected to find at least one MongoDB client.");
52-
}
53-
54-
/**
55-
* Check if either a {@link MongoClient com.mongodb.MongoClient} or
56-
* {@link com.mongodb.client.MongoClient com.mongodb.client.MongoClient} bean is
57-
* available.
58-
*/
59-
static class AnyMongoClientAvailable extends AnyNestedCondition {
60-
61-
AnyMongoClientAvailable() {
62-
super(ConfigurationPhase.REGISTER_BEAN);
63-
}
64-
65-
@ConditionalOnBean(MongoClient.class)
66-
static class PreferredClientAvailable {
67-
68-
}
69-
70-
@ConditionalOnBean(com.mongodb.client.MongoClient.class)
71-
static class FallbackClientAvailable {
72-
73-
}
74-
42+
MongoDatabaseFactorySupport<?> mongoDatabaseFactory(MongoClient mongoClient, MongoProperties properties) {
43+
return new SimpleMongoClientDatabaseFactory(mongoClient, properties.getMongoClientDatabase());
7544
}
7645

7746
}
Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,18 +48,18 @@
4848
*/
4949
@Configuration(proxyBeanMethods = false)
5050
@ConditionalOnBean(MongoDatabaseFactory.class)
51-
class MongoDbFactoryDependentConfiguration {
51+
class MongoDatabaseFactoryDependentConfiguration {
5252

5353
private final MongoProperties properties;
5454

55-
MongoDbFactoryDependentConfiguration(MongoProperties properties) {
55+
MongoDatabaseFactoryDependentConfiguration(MongoProperties properties) {
5656
this.properties = properties;
5757
}
5858

5959
@Bean
6060
@ConditionalOnMissingBean(MongoOperations.class)
61-
MongoTemplate mongoTemplate(MongoDatabaseFactory mongoDbFactory, MongoConverter converter) {
62-
return new MongoTemplate(mongoDbFactory, converter);
61+
MongoTemplate mongoTemplate(MongoDatabaseFactory factory, MongoConverter converter) {
62+
return new MongoTemplate(factory, converter);
6363
}
6464

6565
@Bean
@@ -74,55 +74,55 @@ MappingMongoConverter mappingMongoConverter(MongoDatabaseFactory factory, MongoM
7474

7575
@Bean
7676
@ConditionalOnMissingBean(GridFsOperations.class)
77-
GridFsTemplate gridFsTemplate(MongoDatabaseFactory mongoDbFactory, MongoTemplate mongoTemplate) {
78-
return new GridFsTemplate(new GridFsMongoDbFactory(mongoDbFactory, this.properties),
77+
GridFsTemplate gridFsTemplate(MongoDatabaseFactory factory, MongoTemplate mongoTemplate) {
78+
return new GridFsTemplate(new GridFsMongoDatabaseFactory(factory, this.properties),
7979
mongoTemplate.getConverter());
8080
}
8181

8282
/**
8383
* {@link MongoDatabaseFactory} decorator to respect
8484
* {@link MongoProperties#getGridFsDatabase()} if set.
8585
*/
86-
static class GridFsMongoDbFactory implements MongoDatabaseFactory {
86+
static class GridFsMongoDatabaseFactory implements MongoDatabaseFactory {
8787

88-
private final MongoDatabaseFactory mongoDbFactory;
88+
private final MongoDatabaseFactory mongoDatabaseFactory;
8989

9090
private final MongoProperties properties;
9191

92-
GridFsMongoDbFactory(MongoDatabaseFactory mongoDbFactory, MongoProperties properties) {
93-
Assert.notNull(mongoDbFactory, "MongoDbFactory must not be null");
92+
GridFsMongoDatabaseFactory(MongoDatabaseFactory mongoDatabaseFactory, MongoProperties properties) {
93+
Assert.notNull(mongoDatabaseFactory, "MongoDatabaseFactory must not be null");
9494
Assert.notNull(properties, "Properties must not be null");
95-
this.mongoDbFactory = mongoDbFactory;
95+
this.mongoDatabaseFactory = mongoDatabaseFactory;
9696
this.properties = properties;
9797
}
9898

9999
@Override
100100
public MongoDatabase getMongoDatabase() throws DataAccessException {
101101
String gridFsDatabase = this.properties.getGridFsDatabase();
102102
if (StringUtils.hasText(gridFsDatabase)) {
103-
return this.mongoDbFactory.getMongoDatabase(gridFsDatabase);
103+
return this.mongoDatabaseFactory.getMongoDatabase(gridFsDatabase);
104104
}
105-
return this.mongoDbFactory.getMongoDatabase();
105+
return this.mongoDatabaseFactory.getMongoDatabase();
106106
}
107107

108108
@Override
109109
public MongoDatabase getMongoDatabase(String dbName) throws DataAccessException {
110-
return this.mongoDbFactory.getMongoDatabase(dbName);
110+
return this.mongoDatabaseFactory.getMongoDatabase(dbName);
111111
}
112112

113113
@Override
114114
public PersistenceExceptionTranslator getExceptionTranslator() {
115-
return this.mongoDbFactory.getExceptionTranslator();
115+
return this.mongoDatabaseFactory.getExceptionTranslator();
116116
}
117117

118118
@Override
119119
public ClientSession getSession(ClientSessionOptions options) {
120-
return this.mongoDbFactory.getSession(options);
120+
return this.mongoDatabaseFactory.getSession(options);
121121
}
122122

123123
@Override
124124
public MongoDatabaseFactory withSession(ClientSession session) {
125-
return this.mongoDbFactory.withSession(session);
125+
return this.mongoDatabaseFactory.withSession(session);
126126
}
127127

128128
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoReactiveDataAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ public DefaultDataBufferFactory dataBufferFactory() {
9797

9898
@Bean
9999
@ConditionalOnMissingBean(ReactiveGridFsOperations.class)
100-
public ReactiveGridFsTemplate reactiveGridFsTemplate(ReactiveMongoDatabaseFactory reactiveMongoDbFactory,
100+
public ReactiveGridFsTemplate reactiveGridFsTemplate(ReactiveMongoDatabaseFactory reactiveMongoDatabaseFactory,
101101
MappingMongoConverter mappingMongoConverter, DataBufferFactory dataBufferFactory) {
102-
return new ReactiveGridFsTemplate(dataBufferFactory, reactiveMongoDbFactory, mappingMongoConverter, null);
102+
return new ReactiveGridFsTemplate(dataBufferFactory, reactiveMongoDatabaseFactory, mappingMongoConverter, null);
103103
}
104104

105105
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/mongo/MongoRepositoriesAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mongo/MongoAutoConfiguration.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,6 @@
1616

1717
package org.springframework.boot.autoconfigure.mongo;
1818

19-
import java.util.stream.Collectors;
20-
2119
import com.mongodb.MongoClientSettings;
2220
import com.mongodb.client.MongoClient;
2321

@@ -43,17 +41,14 @@
4341
@Configuration(proxyBeanMethods = false)
4442
@ConditionalOnClass(MongoClient.class)
4543
@EnableConfigurationProperties(MongoProperties.class)
46-
@ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDbFactory")
44+
@ConditionalOnMissingBean(type = "org.springframework.data.mongodb.MongoDatabaseFactory")
4745
public class MongoAutoConfiguration {
4846

4947
@Bean
50-
@ConditionalOnMissingBean(type = { "com.mongodb.client.MongoClient" })
51-
public MongoClient mongo(MongoProperties properties, Environment environment,
52-
ObjectProvider<MongoClientSettingsBuilderCustomizer> builderCustomizers,
53-
ObjectProvider<MongoClientSettings> settings) {
54-
return new MongoClientFactory(properties, environment,
55-
builderCustomizers.orderedStream().collect(Collectors.toList()))
56-
.createMongoClient(settings.getIfAvailable());
48+
@ConditionalOnMissingBean(MongoClient.class)
49+
public MongoClient mongo(MongoProperties properties, ObjectProvider<MongoClientSettings> settings,
50+
Environment environment) {
51+
return new MongoClientFactory(properties, environment).createMongoClient(settings.getIfAvailable());
5752
}
5853

5954
}

0 commit comments

Comments
 (0)