|
23 | 23 | import java.util.*;
|
24 | 24 | import java.util.concurrent.TimeUnit;
|
25 | 25 | import java.util.function.BiPredicate;
|
| 26 | +import java.util.function.Supplier; |
26 | 27 | import java.util.stream.Collectors;
|
27 | 28 | import java.util.stream.Stream;
|
28 | 29 |
|
@@ -174,6 +175,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
174 | 175 | private final EntityOperations operations;
|
175 | 176 | private final PropertyOperations propertyOperations;
|
176 | 177 | private final QueryOperations queryOperations;
|
| 178 | + private final EntityLifecycleEventDelegate eventDelegate; |
177 | 179 |
|
178 | 180 | private @Nullable WriteConcern writeConcern;
|
179 | 181 | private WriteConcernResolver writeConcernResolver = DefaultWriteConcernResolver.INSTANCE;
|
@@ -228,6 +230,7 @@ public MongoTemplate(MongoDatabaseFactory mongoDbFactory, @Nullable MongoConvert
|
228 | 230 | this.propertyOperations = new PropertyOperations(this.mongoConverter.getMappingContext());
|
229 | 231 | this.queryOperations = new QueryOperations(queryMapper, updateMapper, operations, propertyOperations,
|
230 | 232 | mongoDbFactory);
|
| 233 | + this.eventDelegate = new EntityLifecycleEventDelegate(); |
231 | 234 |
|
232 | 235 | // We always have a mapping context in the converter, whether it's a simple one or not
|
233 | 236 | mappingContext = this.mongoConverter.getMappingContext();
|
@@ -266,6 +269,7 @@ private MongoTemplate(MongoDatabaseFactory dbFactory, MongoTemplate that) {
|
266 | 269 | this.operations = that.operations;
|
267 | 270 | this.propertyOperations = that.propertyOperations;
|
268 | 271 | this.queryOperations = that.queryOperations;
|
| 272 | + this.eventDelegate = that.eventDelegate; |
269 | 273 | }
|
270 | 274 |
|
271 | 275 | /**
|
@@ -308,12 +312,25 @@ public void setReadPreference(@Nullable ReadPreference readPreference) {
|
308 | 312 | this.readPreference = readPreference;
|
309 | 313 | }
|
310 | 314 |
|
| 315 | + /** |
| 316 | + * Configure whether lifecycle events such as {@link AfterLoadEvent}, {@link BeforeSaveEvent}, etc. should be |
| 317 | + * published or whether emission should be suppressed. Enabled by default. |
| 318 | + * |
| 319 | + * @param enabled {@code true} to enable entity lifecycle events; {@code false} to disable entity lifecycle events. |
| 320 | + * @since 4.0 |
| 321 | + * @see MongoMappingEvent |
| 322 | + */ |
| 323 | + public void setEntityLifecycleEventsEnabled(boolean enabled) { |
| 324 | + this.eventDelegate.setEventsEnabled(enabled); |
| 325 | + } |
| 326 | + |
311 | 327 | @Override
|
312 | 328 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
313 | 329 |
|
314 | 330 | prepareIndexCreator(applicationContext);
|
315 | 331 |
|
316 | 332 | eventPublisher = applicationContext;
|
| 333 | + eventDelegate.setPublisher(eventPublisher); |
317 | 334 |
|
318 | 335 | if (entityCallbacks == null) {
|
319 | 336 | setEntityCallbacks(EntityCallbacks.create(applicationContext));
|
@@ -2168,11 +2185,7 @@ protected MongoDatabase prepareDatabase(MongoDatabase database) {
|
2168 | 2185 | }
|
2169 | 2186 |
|
2170 | 2187 | protected <E extends MongoMappingEvent<T>, T> E maybeEmitEvent(E event) {
|
2171 |
| - |
2172 |
| - if (eventPublisher != null) { |
2173 |
| - eventPublisher.publishEvent(event); |
2174 |
| - } |
2175 |
| - |
| 2188 | + eventDelegate.publishEvent(event); |
2176 | 2189 | return event;
|
2177 | 2190 | }
|
2178 | 2191 |
|
|
0 commit comments