|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2022 the original author or authors. |
| 2 | + * Copyright 2012-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
19 | 19 | import java.util.Collections;
|
20 | 20 |
|
21 | 21 | import org.springframework.beans.BeanUtils;
|
| 22 | +import org.springframework.beans.factory.ObjectProvider; |
22 | 23 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
23 | 24 | import org.springframework.boot.autoconfigure.domain.EntityScanner;
|
24 | 25 | import org.springframework.boot.autoconfigure.mongo.MongoProperties;
|
|
27 | 28 | import org.springframework.context.annotation.Bean;
|
28 | 29 | import org.springframework.context.annotation.Configuration;
|
29 | 30 | import org.springframework.data.mapping.model.FieldNamingStrategy;
|
| 31 | +import org.springframework.data.mongodb.MongoDatabaseFactory; |
30 | 32 | import org.springframework.data.mongodb.MongoManagedTypes;
|
| 33 | +import org.springframework.data.mongodb.core.convert.DbRefResolver; |
| 34 | +import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver; |
| 35 | +import org.springframework.data.mongodb.core.convert.MappingMongoConverter; |
| 36 | +import org.springframework.data.mongodb.core.convert.MongoConverter; |
31 | 37 | import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
|
| 38 | +import org.springframework.data.mongodb.core.convert.NoOpDbRefResolver; |
32 | 39 | import org.springframework.data.mongodb.core.mapping.Document;
|
33 | 40 | import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
|
34 | 41 |
|
@@ -70,4 +77,16 @@ MongoCustomConversions mongoCustomConversions() {
|
70 | 77 | return new MongoCustomConversions(Collections.emptyList());
|
71 | 78 | }
|
72 | 79 |
|
| 80 | + @Bean |
| 81 | + @ConditionalOnMissingBean(MongoConverter.class) |
| 82 | + MappingMongoConverter mappingMongoConverter(ObjectProvider<MongoDatabaseFactory> factory, |
| 83 | + MongoMappingContext context, MongoCustomConversions conversions) { |
| 84 | + MongoDatabaseFactory mongoDatabaseFactory = factory.getIfAvailable(); |
| 85 | + DbRefResolver dbRefResolver = (mongoDatabaseFactory != null) ? new DefaultDbRefResolver(mongoDatabaseFactory) |
| 86 | + : NoOpDbRefResolver.INSTANCE; |
| 87 | + MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context); |
| 88 | + mappingConverter.setCustomConversions(conversions); |
| 89 | + return mappingConverter; |
| 90 | + } |
| 91 | + |
73 | 92 | }
|
0 commit comments