38
38
import org .slf4j .Logger ;
39
39
import org .slf4j .LoggerFactory ;
40
40
import org .springframework .beans .BeansException ;
41
+ import org .springframework .beans .factory .BeanClassLoaderAware ;
41
42
import org .springframework .context .ApplicationContext ;
42
43
import org .springframework .context .ApplicationContextAware ;
43
44
import org .springframework .core .CollectionFactory ;
@@ -123,6 +124,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
123
124
protected @ Nullable String mapKeyDotReplacement = null ;
124
125
protected @ Nullable CodecRegistryProvider codecRegistryProvider ;
125
126
127
+ private MongoTypeMapper defaultTypeMapper ;
126
128
private SpELContext spELContext ;
127
129
private @ Nullable EntityCallbacks entityCallbacks ;
128
130
private final DocumentPointerFactory documentPointerFactory ;
@@ -144,7 +146,7 @@ public MappingMongoConverter(DbRefResolver dbRefResolver,
144
146
this .dbRefResolver = dbRefResolver ;
145
147
146
148
this .mappingContext = mappingContext ;
147
- this .typeMapper = new DefaultMongoTypeMapper (DefaultMongoTypeMapper .DEFAULT_TYPE_KEY , mappingContext ,
149
+ this .defaultTypeMapper = new DefaultMongoTypeMapper (DefaultMongoTypeMapper .DEFAULT_TYPE_KEY , mappingContext ,
148
150
this ::getWriteTarget );
149
151
this .idMapper = new QueryMapper (this );
150
152
@@ -197,9 +199,7 @@ public MappingMongoConverter(MongoDatabaseFactory mongoDbFactory,
197
199
* @param typeMapper the typeMapper to set. Can be {@literal null}.
198
200
*/
199
201
public void setTypeMapper (@ Nullable MongoTypeMapper typeMapper ) {
200
- this .typeMapper = typeMapper == null
201
- ? new DefaultMongoTypeMapper (DefaultMongoTypeMapper .DEFAULT_TYPE_KEY , mappingContext )
202
- : typeMapper ;
202
+ this .typeMapper = typeMapper ;
203
203
}
204
204
205
205
/*
@@ -208,7 +208,7 @@ public void setTypeMapper(@Nullable MongoTypeMapper typeMapper) {
208
208
*/
209
209
@ Override
210
210
public MongoTypeMapper getTypeMapper () {
211
- return this .typeMapper ;
211
+ return this .typeMapper == null ? this . defaultTypeMapper : this . typeMapper ;
212
212
}
213
213
214
214
/**
@@ -257,6 +257,11 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
257
257
if (entityCallbacks == null ) {
258
258
setEntityCallbacks (EntityCallbacks .create (applicationContext ));
259
259
}
260
+
261
+ ClassLoader classLoader = applicationContext .getClassLoader ();
262
+ if (this .defaultTypeMapper instanceof BeanClassLoaderAware && classLoader != null ) {
263
+ ((BeanClassLoaderAware ) this .defaultTypeMapper ).setBeanClassLoader (classLoader );
264
+ }
260
265
}
261
266
262
267
/**
@@ -301,7 +306,7 @@ protected <S extends Object> S readDocument(ConversionContext context, Bson bson
301
306
TypeInformation <? extends S > typeHint ) {
302
307
303
308
Document document = bson instanceof BasicDBObject ? new Document ((BasicDBObject ) bson ) : (Document ) bson ;
304
- TypeInformation <? extends S > typeToRead = typeMapper .readType (document , typeHint );
309
+ TypeInformation <? extends S > typeToRead = getTypeMapper () .readType (document , typeHint );
305
310
Class <? extends S > rawType = typeToRead .getType ();
306
311
307
312
if (conversions .hasCustomReadTarget (bson .getClass (), rawType )) {
@@ -657,7 +662,7 @@ public void write(Object obj, Bson bson) {
657
662
BsonUtils .removeNullId (bson );
658
663
659
664
if (requiresTypeHint (entityType )) {
660
- typeMapper .writeType (type , bson );
665
+ getTypeMapper () .writeType (type , bson );
661
666
}
662
667
}
663
668
@@ -1099,7 +1104,7 @@ protected void addCustomTypeKeyIfNecessary(@Nullable TypeInformation<?> type, Ob
1099
1104
1100
1105
boolean notTheSameClass = !valueType .equals (reference );
1101
1106
if (notTheSameClass ) {
1102
- typeMapper .writeType (valueType , bson );
1107
+ getTypeMapper () .writeType (valueType , bson );
1103
1108
}
1104
1109
}
1105
1110
@@ -1307,7 +1312,7 @@ protected Map<Object, Object> readMap(ConversionContext context, Bson bson, Type
1307
1312
Assert .notNull (bson , "Document must not be null!" );
1308
1313
Assert .notNull (targetType , "TypeInformation must not be null!" );
1309
1314
1310
- Class <?> mapType = typeMapper .readType (bson , targetType ).getType ();
1315
+ Class <?> mapType = getTypeMapper () .readType (bson , targetType ).getType ();
1311
1316
1312
1317
TypeInformation <?> keyType = targetType .getComponentType ();
1313
1318
TypeInformation <?> valueType = targetType .getMapValueType () == null ? ClassTypeInformation .OBJECT
@@ -1326,7 +1331,7 @@ protected Map<Object, Object> readMap(ConversionContext context, Bson bson, Type
1326
1331
1327
1332
sourceMap .forEach ((k , v ) -> {
1328
1333
1329
- if (typeMapper .isTypeKey (k )) {
1334
+ if (getTypeMapper () .isTypeKey (k )) {
1330
1335
return ;
1331
1336
}
1332
1337
@@ -1489,7 +1494,7 @@ private Object removeTypeInfo(Object object, boolean recursively) {
1489
1494
}
1490
1495
}
1491
1496
1492
- if (typeMapper .isTypeKey (key )) {
1497
+ if (getTypeMapper () .isTypeKey (key )) {
1493
1498
1494
1499
keyToRemove = key ;
1495
1500
@@ -1660,6 +1665,7 @@ public MappingMongoConverter with(MongoDatabaseFactory dbFactory) {
1660
1665
target .conversions = conversions ;
1661
1666
target .spELContext = spELContext ;
1662
1667
target .setInstantiators (instantiators );
1668
+ target .defaultTypeMapper = defaultTypeMapper ;
1663
1669
target .typeMapper = typeMapper ;
1664
1670
target .setCodecRegistryProvider (dbFactory );
1665
1671
target .afterPropertiesSet ();
0 commit comments