|
16 | 16 |
|
17 | 17 | package org.springframework.boot.autoconfigure.data.mongo; |
18 | 18 |
|
19 | | -import java.util.Arrays; |
20 | | -import java.util.List; |
21 | | - |
22 | 19 | import com.mongodb.ClientSessionOptions; |
23 | 20 | import com.mongodb.DB; |
24 | 21 | import com.mongodb.MongoClient; |
|
32 | 29 | import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
33 | 30 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
34 | 31 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
35 | | -import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration.AnySyncMongoClientAvailable; |
| 32 | +import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration.AnyMongoClientAvailable; |
36 | 33 | import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; |
37 | 34 | import org.springframework.boot.autoconfigure.mongo.MongoProperties; |
38 | 35 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
|
43 | 40 | import org.springframework.dao.DataAccessException; |
44 | 41 | import org.springframework.dao.support.PersistenceExceptionTranslator; |
45 | 42 | import org.springframework.data.mongodb.MongoDbFactory; |
| 43 | +import org.springframework.data.mongodb.core.MongoDbFactorySupport; |
46 | 44 | import org.springframework.data.mongodb.core.MongoTemplate; |
47 | 45 | import org.springframework.data.mongodb.core.SimpleMongoClientDbFactory; |
48 | 46 | import org.springframework.data.mongodb.core.SimpleMongoDbFactory; |
|
75 | 73 | * @since 1.1.0 |
76 | 74 | */ |
77 | 75 | @Configuration |
78 | | -@ConditionalOnClass({ MongoClient.class, MongoTemplate.class }) |
79 | | -@Conditional(AnySyncMongoClientAvailable.class) |
| 76 | +@ConditionalOnClass({ MongoClient.class, com.mongodb.client.MongoClient.class, |
| 77 | + MongoTemplate.class }) |
| 78 | +@Conditional(AnyMongoClientAvailable.class) |
80 | 79 | @EnableConfigurationProperties(MongoProperties.class) |
81 | 80 | @Import(MongoDataConfiguration.class) |
82 | 81 | @AutoConfigureAfter(MongoAutoConfiguration.class) |
83 | 82 | public class MongoDataAutoConfiguration { |
84 | 83 |
|
85 | 84 | private final MongoProperties properties; |
86 | 85 |
|
87 | | - private final MongoDbFactoryFactory dbFactoryFactory; |
88 | | - |
89 | | - public MongoDataAutoConfiguration(ObjectProvider<MongoClient> mongoClientProvider, |
90 | | - ObjectProvider<com.mongodb.client.MongoClient> mongoClientClientProvider, |
91 | | - MongoProperties properties) { |
92 | | - |
| 86 | + public MongoDataAutoConfiguration(MongoProperties properties) { |
93 | 87 | this.properties = properties; |
94 | | - this.dbFactoryFactory = new MongoDbFactoryFactory(mongoClientProvider, |
95 | | - mongoClientClientProvider); |
96 | 88 | } |
97 | 89 |
|
98 | 90 | @Bean |
99 | | - @Conditional(AnySyncMongoClientAvailable.class) |
100 | 91 | @ConditionalOnMissingBean(MongoDbFactory.class) |
101 | | - public MongoDbFactory mongoDbFactory() { |
102 | | - return this.dbFactoryFactory.getFor(this.properties.getMongoClientDatabase()); |
| 92 | + public MongoDbFactorySupport<?> mongoDbFactory(ObjectProvider<MongoClient> mongo, |
| 93 | + ObjectProvider<com.mongodb.client.MongoClient> mongoClient) { |
| 94 | + MongoClient preferredClient = mongo.getIfAvailable(); |
| 95 | + if (preferredClient != null) { |
| 96 | + return new SimpleMongoDbFactory(preferredClient, |
| 97 | + this.properties.getMongoClientDatabase()); |
| 98 | + } |
| 99 | + com.mongodb.client.MongoClient fallbackClient = mongoClient.getIfAvailable(); |
| 100 | + if (fallbackClient != null) { |
| 101 | + return new SimpleMongoClientDbFactory(fallbackClient, |
| 102 | + this.properties.getMongoClientDatabase()); |
| 103 | + } |
| 104 | + throw new IllegalStateException("Expected to find at least one MongoDB client."); |
103 | 105 | } |
104 | 106 |
|
105 | 107 | @Bean |
@@ -183,87 +185,23 @@ public MongoDbFactory withSession(ClientSession session) { |
183 | 185 | } |
184 | 186 |
|
185 | 187 | /** |
186 | | - * Check if either {@link com.mongodb.MongoClient} or |
187 | | - * {@link com.mongodb.client.MongoClient} is already defined in the |
188 | | - * {@link org.springframework.context.ApplicationContext}. |
189 | | - * |
190 | | - * @author Christoph Strobl |
191 | | - * @since 2.1 |
| 188 | + * Check if either a {@link com.mongodb.MongoClient} or |
| 189 | + * {@link com.mongodb.client.MongoClient} bean is available. |
192 | 190 | */ |
193 | | - static class AnySyncMongoClientAvailable extends AnyNestedCondition { |
| 191 | + static class AnyMongoClientAvailable extends AnyNestedCondition { |
194 | 192 |
|
195 | | - AnySyncMongoClientAvailable() { |
| 193 | + AnyMongoClientAvailable() { |
196 | 194 | super(ConfigurationPhase.REGISTER_BEAN); |
197 | 195 | } |
198 | 196 |
|
199 | | - @ConditionalOnBean(com.mongodb.MongoClient.class) |
200 | | - static class MongoClientPreferred { |
| 197 | + @ConditionalOnBean(MongoClient.class) |
| 198 | + static class PreferredClientAvailable { |
201 | 199 |
|
202 | 200 | } |
203 | 201 |
|
204 | 202 | @ConditionalOnBean(com.mongodb.client.MongoClient.class) |
205 | | - static class MongoClientClientPreferred { |
206 | | - |
207 | | - } |
208 | | - |
209 | | - } |
210 | | - |
211 | | - /** |
212 | | - * Encapsulation of {@link MongoDbFactory} creation depending on available beans |
213 | | - * {@link com.mongodb.MongoClient} or {@link com.mongodb.client.MongoClient} expressed |
214 | | - * via the given {@link ObjectProvider ObjectProviders}. Prefers the first available |
215 | | - * MongoDB client creating a suitable instance of {@link MongoDbFactory} for it. |
216 | | - * |
217 | | - * @author Christoph Strobl |
218 | | - * @since 2.1 |
219 | | - */ |
220 | | - static class MongoDbFactoryFactory { |
221 | | - |
222 | | - private final List<ObjectProvider<?>> clientProviders; |
223 | | - |
224 | | - /** |
225 | | - * Create new instance of {@link MongoDbFactoryFactory}. |
226 | | - * @param clientProviders order matters here, as we choose the first available |
227 | | - * one. |
228 | | - */ |
229 | | - MongoDbFactoryFactory(ObjectProvider<?>... clientProviders) { |
230 | | - this.clientProviders = Arrays.asList(clientProviders); |
231 | | - } |
232 | | - |
233 | | - /** |
234 | | - * Get the {@link MongoDbFactory} suitable for the first available MongoDB client. |
235 | | - * @param database the name of the default database to return on |
236 | | - * {@link MongoDbFactory#getDb()}. |
237 | | - * @return new instance of {@link MongoDbFactory} suitable for the first available |
238 | | - * MongoDB client. |
239 | | - */ |
240 | | - MongoDbFactory getFor(String database) { |
241 | | - |
242 | | - Object client = findAvailableClientProvider(); |
243 | | - |
244 | | - if (client instanceof MongoClient) { |
245 | | - return new SimpleMongoDbFactory(MongoClient.class.cast(client), database); |
246 | | - } |
247 | | - |
248 | | - if (client instanceof com.mongodb.client.MongoClient) { |
249 | | - return new SimpleMongoClientDbFactory( |
250 | | - com.mongodb.client.MongoClient.class.cast(client), database); |
251 | | - } |
252 | | - |
253 | | - return null; |
254 | | - } |
255 | | - |
256 | | - private Object findAvailableClientProvider() { |
257 | | - |
258 | | - for (ObjectProvider<?> provider : this.clientProviders) { |
259 | | - Object client = provider.getIfAvailable(); |
260 | | - if (client != null) { |
261 | | - return client; |
262 | | - } |
263 | | - } |
| 203 | + static class FallbackClientAvailable { |
264 | 204 |
|
265 | | - throw new IllegalStateException( |
266 | | - "Expected to find at least one MongoDB client."); |
267 | 205 | } |
268 | 206 |
|
269 | 207 | } |
|
0 commit comments