Skip to content

Commit 5fd8687

Browse files
committed
Polishing.
Extract adding the actual entity to the MappingContext into its own method along with error handling spanning the entire entity initialization process. See #2329 Original pull request: #2367.
1 parent 2511b1f commit 5fd8687

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java

+24-15
Original file line numberDiff line numberDiff line change
@@ -364,23 +364,42 @@ protected Optional<E> addPersistentEntity(TypeInformation<?> typeInformation) {
364364
read.unlock();
365365
}
366366

367-
Class<?> type = typeInformation.getType();
368-
E entity = null;
367+
E entity;
369368

370369
try {
371370

372371
write.lock();
372+
entity = doAddPersistentEntity(typeInformation);
373373

374-
entity = createPersistentEntity(typeInformation);
374+
} catch (BeansException e) {
375+
throw new MappingException(e.getMessage(), e);
376+
} finally {
377+
write.unlock();
378+
}
379+
380+
// Inform listeners
381+
if (applicationEventPublisher != null) {
382+
applicationEventPublisher.publishEvent(new MappingContextEvent<>(this, entity));
383+
}
384+
385+
return Optional.of(entity);
386+
}
387+
388+
private E doAddPersistentEntity(TypeInformation<?> typeInformation) {
375389

390+
try {
391+
392+
Class<?> type = typeInformation.getType();
393+
394+
E entity = createPersistentEntity(typeInformation);
376395
entity.setEvaluationContextProvider(evaluationContextProvider);
377396

378397
// Eagerly cache the entity as we might have to find it during recursive lookups.
379398
persistentEntities.put(typeInformation, Optional.of(entity));
380399

381400
PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(type);
382401

383-
final Map<String, PropertyDescriptor> descriptors = new HashMap<>();
402+
Map<String, PropertyDescriptor> descriptors = new HashMap<>();
384403
for (PropertyDescriptor descriptor : pds) {
385404
descriptors.put(descriptor.getName(), descriptor);
386405
}
@@ -395,21 +414,11 @@ protected Optional<E> addPersistentEntity(TypeInformation<?> typeInformation) {
395414
entity.setPersistentPropertyAccessorFactory(persistentPropertyAccessorFactory);
396415
}
397416

398-
} catch (BeansException e) {
399-
throw new MappingException(e.getMessage(), e);
417+
return entity;
400418
} catch (RuntimeException e) {
401419
persistentEntities.remove(typeInformation);
402420
throw e;
403-
} finally {
404-
write.unlock();
405421
}
406-
407-
// Inform listeners
408-
if (applicationEventPublisher != null && entity != null) {
409-
applicationEventPublisher.publishEvent(new MappingContextEvent<>(this, entity));
410-
}
411-
412-
return Optional.of(entity);
413422
}
414423

415424
/*

0 commit comments

Comments
 (0)