32
32
import org .springframework .beans .BeansException ;
33
33
import org .springframework .context .ApplicationContext ;
34
34
import org .springframework .context .ApplicationContextAware ;
35
- import org .springframework .context .ApplicationEventPublisher ;
36
35
import org .springframework .core .CollectionFactory ;
37
36
import org .springframework .core .convert .ConversionException ;
38
37
import org .springframework .core .convert .ConversionService ;
59
58
import org .springframework .data .mongodb .core .mapping .MongoPersistentProperty ;
60
59
import org .springframework .data .mongodb .core .mapping .event .AfterConvertEvent ;
61
60
import org .springframework .data .mongodb .core .mapping .event .AfterLoadEvent ;
61
+ import org .springframework .data .mongodb .core .mapping .event .MongoMappingEvent ;
62
62
import org .springframework .data .util .ClassTypeInformation ;
63
63
import org .springframework .data .util .TypeInformation ;
64
64
import org .springframework .expression .spel .standard .SpelExpressionParser ;
@@ -892,7 +892,7 @@ protected DBRef createDBRef(Object target, MongoPersistentProperty property) {
892
892
@ Override
893
893
public Object getValueInternal (MongoPersistentProperty prop , Bson dbo , SpELExpressionEvaluator evaluator ,
894
894
ObjectPath path ) {
895
- return new MongoDbPropertyValueProvider (dbo , evaluator , path , false ).getPropertyValue (prop );
895
+ return new MongoDbPropertyValueProvider (dbo , evaluator , path ).getPropertyValue (prop );
896
896
}
897
897
898
898
/**
@@ -1218,7 +1218,6 @@ private class MongoDbPropertyValueProvider implements PropertyValueProvider<Mong
1218
1218
private final DocumentAccessor source ;
1219
1219
private final SpELExpressionEvaluator evaluator ;
1220
1220
private final ObjectPath path ;
1221
- private final boolean ignoreLazyDBRefProperties ;
1222
1221
1223
1222
/**
1224
1223
* Creates a new {@link MongoDbPropertyValueProvider} for the given source, {@link SpELExpressionEvaluator} and
@@ -1229,18 +1228,13 @@ private class MongoDbPropertyValueProvider implements PropertyValueProvider<Mong
1229
1228
* @param path can be {@literal null}.
1230
1229
*/
1231
1230
public MongoDbPropertyValueProvider (Bson source , SpELExpressionEvaluator evaluator , ObjectPath path ) {
1232
- this (source , evaluator , path , true ); // ignoring by default
1233
- }
1234
1231
1235
- MongoDbPropertyValueProvider (Bson source , SpELExpressionEvaluator evaluator , ObjectPath path ,
1236
- boolean ignoreLazyDBRefProperties ) {
1237
1232
Assert .notNull (source );
1238
1233
Assert .notNull (evaluator );
1239
1234
1240
1235
this .source = new DocumentAccessor (source );
1241
1236
this .evaluator = evaluator ;
1242
1237
this .path = path ;
1243
- this .ignoreLazyDBRefProperties = ignoreLazyDBRefProperties ;
1244
1238
}
1245
1239
1246
1240
/*
@@ -1255,12 +1249,6 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
1255
1249
if (value == null ) {
1256
1250
return null ;
1257
1251
}
1258
- if (this .ignoreLazyDBRefProperties && property .isDbReference () && property .getDBRef ().lazy ()) { // lazy DBRef,
1259
- // BasicDBList are
1260
- // resolved later
1261
- // by default
1262
- return null ;
1263
- }
1264
1252
1265
1253
return readValue (value , property .getTypeInformation (), path );
1266
1254
}
@@ -1324,24 +1312,39 @@ private <T> T readValue(Object value, TypeInformation<?> type, ObjectPath path)
1324
1312
1325
1313
@ SuppressWarnings ("unchecked" )
1326
1314
private <T > T potentiallyReadOrResolveDbRef (DBRef dbref , TypeInformation <?> type , ObjectPath path , Class <?> rawType ) {
1315
+
1327
1316
if (rawType .equals (DBRef .class )) {
1328
1317
return (T ) dbref ;
1329
1318
}
1319
+
1330
1320
Object object = dbref == null ? null : path .getPathItem (dbref .getId (), dbref .getCollectionName ());
1331
1321
return (T ) (object != null ? object : readAndConvertDBRef (dbref , type , path , rawType ));
1332
1322
}
1333
1323
1334
1324
private <T > T readAndConvertDBRef (DBRef dbref , TypeInformation <?> type , ObjectPath path , Class <?> rawType ) {
1325
+
1335
1326
Document readRef = readRef (dbref );
1327
+
1336
1328
final String collectionName = dbref .getCollectionName ();
1337
- if (canPublishEvent ())
1338
- ((ApplicationEventPublisher ) this .applicationContext )
1339
- .publishEvent (new AfterLoadEvent <T >(readRef , (Class <T >) rawType , collectionName ));
1340
- T t = (T ) read (type , readRef , path );
1341
- if (canPublishEvent ())
1342
- ((ApplicationEventPublisher ) this .applicationContext )
1343
- .publishEvent (new AfterConvertEvent <T >(readRef , t , collectionName ));
1344
- return t ;
1329
+
1330
+ if (readRef != null ) {
1331
+ maybeEmitEvent (new AfterLoadEvent <T >(readRef , (Class <T >) rawType , collectionName ));
1332
+ }
1333
+
1334
+ final T target = (T ) read (type , readRef , path );
1335
+
1336
+ if (target != null ) {
1337
+ maybeEmitEvent (new AfterConvertEvent <T >(readRef , target , collectionName ));
1338
+ }
1339
+
1340
+ return target ;
1341
+ }
1342
+
1343
+ private void maybeEmitEvent (MongoMappingEvent <?> event ) {
1344
+
1345
+ if (canPublishEvent ()) {
1346
+ this .applicationContext .publishEvent (event );
1347
+ }
1345
1348
}
1346
1349
1347
1350
private boolean canPublishEvent () {
0 commit comments