|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.mongo;
|
18 | 18 |
|
| 19 | +import java.lang.reflect.Method; |
| 20 | + |
19 | 21 | import com.mongodb.MongoClientSettings;
|
20 | 22 | import com.mongodb.MongoClientSettings.Builder;
|
21 | 23 | import com.mongodb.connection.TransportSettings;
|
|
25 | 27 | import io.netty.channel.socket.SocketChannel;
|
26 | 28 | import reactor.core.publisher.Flux;
|
27 | 29 |
|
| 30 | +import org.springframework.aot.hint.MemberCategory; |
| 31 | +import org.springframework.aot.hint.RuntimeHints; |
| 32 | +import org.springframework.aot.hint.RuntimeHintsRegistrar; |
28 | 33 | import org.springframework.beans.factory.DisposableBean;
|
29 | 34 | import org.springframework.beans.factory.ObjectProvider;
|
30 | 35 | import org.springframework.boot.autoconfigure.AutoConfiguration;
|
|
37 | 42 | import org.springframework.context.annotation.Configuration;
|
38 | 43 | import org.springframework.core.Ordered;
|
39 | 44 | import org.springframework.core.annotation.Order;
|
| 45 | +import org.springframework.util.ReflectionUtils; |
40 | 46 |
|
41 | 47 | /**
|
42 | 48 | * {@link EnableAutoConfiguration Auto-configuration} for Reactive Mongo.
|
43 | 49 | *
|
44 | 50 | * @author Mark Paluch
|
45 | 51 | * @author Stephane Nicoll
|
46 | 52 | * @author Scott Frederick
|
| 53 | + * @author Christoph Strobl |
47 | 54 | * @since 2.0.0
|
48 | 55 | */
|
49 | 56 | @AutoConfiguration
|
@@ -132,9 +139,36 @@ public void destroy() {
|
132 | 139 | @SuppressWarnings("deprecation")
|
133 | 140 | private boolean isCustomTransportConfiguration(MongoClientSettings settings) {
|
134 | 141 | return settings != null
|
135 |
| - && (settings.getTransportSettings() != null || settings.getStreamFactoryFactory() != null); |
| 142 | + && (settings.getTransportSettings() != null || isMongo4configuredStreamFactorFactory(settings)); |
136 | 143 | }
|
137 | 144 |
|
| 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 | + } |
138 | 172 | }
|
139 | 173 |
|
140 | 174 | }
|
0 commit comments