Skip to content

Commit 2f8130e

Browse files
Switch to MongoDB 5.0 driver
1 parent 5c2f64d commit 2f8130e

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

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

+35-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

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

19+
import java.lang.reflect.Method;
20+
1921
import com.mongodb.MongoClientSettings;
2022
import com.mongodb.MongoClientSettings.Builder;
2123
import com.mongodb.connection.TransportSettings;
@@ -25,6 +27,9 @@
2527
import io.netty.channel.socket.SocketChannel;
2628
import reactor.core.publisher.Flux;
2729

30+
import org.springframework.aot.hint.MemberCategory;
31+
import org.springframework.aot.hint.RuntimeHints;
32+
import org.springframework.aot.hint.RuntimeHintsRegistrar;
2833
import org.springframework.beans.factory.DisposableBean;
2934
import org.springframework.beans.factory.ObjectProvider;
3035
import org.springframework.boot.autoconfigure.AutoConfiguration;
@@ -37,13 +42,15 @@
3742
import org.springframework.context.annotation.Configuration;
3843
import org.springframework.core.Ordered;
3944
import org.springframework.core.annotation.Order;
45+
import org.springframework.util.ReflectionUtils;
4046

4147
/**
4248
* {@link EnableAutoConfiguration Auto-configuration} for Reactive Mongo.
4349
*
4450
* @author Mark Paluch
4551
* @author Stephane Nicoll
4652
* @author Scott Frederick
53+
* @author Christoph Strobl
4754
* @since 2.0.0
4855
*/
4956
@AutoConfiguration
@@ -132,9 +139,36 @@ public void destroy() {
132139
@SuppressWarnings("deprecation")
133140
private boolean isCustomTransportConfiguration(MongoClientSettings settings) {
134141
return settings != null
135-
&& (settings.getTransportSettings() != null || settings.getStreamFactoryFactory() != null);
142+
&& (settings.getTransportSettings() != null || isMongo4configuredStreamFactorFactory(settings));
136143
}
137144

145+
/**
146+
* The {@code getStreamFactoryFactory} method has been removed from
147+
* {@link MongoClientSettings} in the 5.0 MongoDB driver generation. To remain
148+
* backwards compatible the lookup is now done reflectively.
149+
* @param settings must not be {@literal null}.
150+
* @return {@literal true} if method present and return value is not
151+
* {@literal null}.
152+
* @since 3.3
153+
*/
154+
boolean isMongo4configuredStreamFactorFactory(MongoClientSettings settings) {
155+
156+
Method getStreamFactoryFactory = ReflectionUtils.findMethod(MongoClientSettings.class,
157+
"getStreamFactoryFactory");
158+
if (getStreamFactoryFactory == null) {
159+
return false;
160+
}
161+
return ReflectionUtils.invokeMethod(getStreamFactoryFactory, settings) != null;
162+
}
163+
164+
}
165+
166+
static class MongoReactiveAutoConfigurationRuntimeHints implements RuntimeHintsRegistrar {
167+
168+
@Override
169+
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
170+
hints.reflection().registerTypeIfPresent(classLoader, "com.mongodb.MongoClientSettings", MemberCategory.INVOKE_PUBLIC_METHODS);
171+
}
138172
}
139173

140174
}

spring-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring/aot.factories

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ org.springframework.aot.hint.RuntimeHintsRegistrar=\
22
org.springframework.boot.autoconfigure.freemarker.FreeMarkerTemplateAvailabilityProvider.FreeMarkerTemplateAvailabilityRuntimeHints,\
33
org.springframework.boot.autoconfigure.groovy.template.GroovyTemplateAvailabilityProvider.GroovyTemplateAvailabilityRuntimeHints,\
44
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.JacksonAutoConfigurationRuntimeHints,\
5+
org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration.MongoReactiveAutoConfigurationRuntimeHints,\
56
org.springframework.boot.autoconfigure.template.TemplateRuntimeHints
67

78
org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor=\

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/mongo/MongoReactiveAutoConfigurationTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ void customizerWithTransportSettingsOverridesAutoConfig() {
230230
assertThat(settings.getApplicationName()).isEqualTo("custom-transport-settings");
231231
assertThat(settings.getTransportSettings())
232232
.isSameAs(SimpleTransportSettingsCustomizerConfig.transportSettings);
233-
assertThat(settings.getStreamFactoryFactory()).isNull();
234233
});
235234
}
236235

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ bom {
12771277
releaseNotes("https://github.com/mockito/mockito/releases/tag/v{version}")
12781278
}
12791279
}
1280-
library("MongoDB", "4.11.1") {
1280+
library("MongoDB", "5.0.0") {
12811281
group("org.mongodb") {
12821282
modules = [
12831283
"bson",

0 commit comments

Comments
 (0)