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