30
30
import org .springframework .beans .BeansException ;
31
31
import org .springframework .context .ApplicationContext ;
32
32
import org .springframework .context .ApplicationContextAware ;
33
- import org .springframework .context .ApplicationEventPublisher ;
34
33
import org .springframework .core .CollectionFactory ;
35
34
import org .springframework .core .convert .ConversionException ;
36
35
import org .springframework .core .convert .ConversionService ;
57
56
import org .springframework .data .mongodb .core .mapping .MongoPersistentProperty ;
58
57
import org .springframework .data .mongodb .core .mapping .event .AfterConvertEvent ;
59
58
import org .springframework .data .mongodb .core .mapping .event .AfterLoadEvent ;
59
+ import org .springframework .data .mongodb .core .mapping .event .MongoMappingEvent ;
60
60
import org .springframework .data .util .ClassTypeInformation ;
61
61
import org .springframework .data .util .TypeInformation ;
62
62
import org .springframework .expression .spel .standard .SpelExpressionParser ;
@@ -872,7 +872,7 @@ protected DBRef createDBRef(Object target, MongoPersistentProperty property) {
872
872
@ Override
873
873
public Object getValueInternal (MongoPersistentProperty prop , DBObject dbo , SpELExpressionEvaluator evaluator ,
874
874
ObjectPath path ) {
875
- return new MongoDbPropertyValueProvider (dbo , evaluator , path , false ).getPropertyValue (prop );
875
+ return new MongoDbPropertyValueProvider (dbo , evaluator , path ).getPropertyValue (prop );
876
876
}
877
877
878
878
/**
@@ -1116,7 +1116,6 @@ private class MongoDbPropertyValueProvider implements PropertyValueProvider<Mong
1116
1116
private final DBObjectAccessor source ;
1117
1117
private final SpELExpressionEvaluator evaluator ;
1118
1118
private final ObjectPath path ;
1119
- private final boolean ignoreLazyDBRefProperties ;
1120
1119
1121
1120
/**
1122
1121
* Creates a new {@link MongoDbPropertyValueProvider} for the given source, {@link SpELExpressionEvaluator} and
@@ -1127,18 +1126,13 @@ private class MongoDbPropertyValueProvider implements PropertyValueProvider<Mong
1127
1126
* @param path can be {@literal null}.
1128
1127
*/
1129
1128
public MongoDbPropertyValueProvider (DBObject source , SpELExpressionEvaluator evaluator , ObjectPath path ) {
1130
- this (source , evaluator , path , true ); // ignoring by default
1131
- }
1132
1129
1133
- MongoDbPropertyValueProvider (DBObject source , SpELExpressionEvaluator evaluator , ObjectPath path ,
1134
- boolean ignoreLazyDBRefProperties ) {
1135
1130
Assert .notNull (source );
1136
1131
Assert .notNull (evaluator );
1137
1132
1138
1133
this .source = new DBObjectAccessor (source );
1139
1134
this .evaluator = evaluator ;
1140
1135
this .path = path ;
1141
- this .ignoreLazyDBRefProperties = ignoreLazyDBRefProperties ;
1142
1136
}
1143
1137
1144
1138
/*
@@ -1153,12 +1147,6 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
1153
1147
if (value == null ) {
1154
1148
return null ;
1155
1149
}
1156
- if (this .ignoreLazyDBRefProperties && property .isDbReference () && property .getDBRef ().lazy ()) { // lazy DBRef,
1157
- // BasicDBList are
1158
- // resolved later
1159
- // by default
1160
- return null ;
1161
- }
1162
1150
1163
1151
return readValue (value , property .getTypeInformation (), path );
1164
1152
}
@@ -1220,24 +1208,39 @@ private <T> T readValue(Object value, TypeInformation<?> type, ObjectPath path)
1220
1208
1221
1209
@ SuppressWarnings ("unchecked" )
1222
1210
private <T > T potentiallyReadOrResolveDbRef (DBRef dbref , TypeInformation <?> type , ObjectPath path , Class <?> rawType ) {
1211
+
1223
1212
if (rawType .equals (DBRef .class )) {
1224
1213
return (T ) dbref ;
1225
1214
}
1215
+
1226
1216
Object object = dbref == null ? null : path .getPathItem (dbref .getId (), dbref .getCollectionName ());
1227
1217
return (T ) (object != null ? object : readAndConvertDBRef (dbref , type , path , rawType ));
1228
1218
}
1229
1219
1230
- private <T > T readAndConvertDBRef (DBRef dbref , TypeInformation <?> type , ObjectPath path , Class <?> rawType ) {
1231
- DBObject readRef = readRef (dbref );
1220
+ @ SuppressWarnings ("unchecked" )
1221
+ private <T > T readAndConvertDBRef (DBRef dbref , TypeInformation <?> type , ObjectPath path , final Class <?> rawType ) {
1222
+
1223
+ final DBObject readRef = readRef (dbref );
1232
1224
final String collectionName = dbref .getCollectionName ();
1233
- if (canPublishEvent ())
1234
- ((ApplicationEventPublisher ) this .applicationContext )
1235
- .publishEvent (new AfterLoadEvent <T >(readRef , (Class <T >) rawType , collectionName ));
1236
- T t = (T ) read (type , readRef , path );
1237
- if (canPublishEvent ())
1238
- ((ApplicationEventPublisher ) this .applicationContext )
1239
- .publishEvent (new AfterConvertEvent <T >(readRef , t , collectionName ));
1240
- return t ;
1225
+
1226
+ if (readRef != null ) {
1227
+ maybeEmitEvent (new AfterLoadEvent <T >(readRef , (Class <T >) rawType , collectionName ));
1228
+ }
1229
+
1230
+ final T target = (T ) read (type , readRef , path );
1231
+
1232
+ if (target != null ) {
1233
+ maybeEmitEvent (new AfterConvertEvent <T >(readRef , target , collectionName ));
1234
+ }
1235
+
1236
+ return target ;
1237
+ }
1238
+
1239
+ private void maybeEmitEvent (MongoMappingEvent <?> event ) {
1240
+
1241
+ if (canPublishEvent ()) {
1242
+ this .applicationContext .publishEvent (event );
1243
+ }
1241
1244
}
1242
1245
1243
1246
private boolean canPublishEvent () {
0 commit comments