Skip to content

Commit 8f7a6d3

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 b05965f commit 8f7a6d3

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
@@ -357,23 +357,42 @@ protected Optional<E> addPersistentEntity(TypeInformation<?> typeInformation) {
357357
read.unlock();
358358
}
359359

360-
Class<?> type = typeInformation.getType();
361-
E entity = null;
360+
E entity;
362361

363362
try {
364363

365364
write.lock();
365+
entity = doAddPersistentEntity(typeInformation);
366366

367-
entity = createPersistentEntity(typeInformation);
367+
} catch (BeansException e) {
368+
throw new MappingException(e.getMessage(), e);
369+
} finally {
370+
write.unlock();
371+
}
372+
373+
// Inform listeners
374+
if (applicationEventPublisher != null) {
375+
applicationEventPublisher.publishEvent(new MappingContextEvent<>(this, entity));
376+
}
377+
378+
return Optional.of(entity);
379+
}
380+
381+
private E doAddPersistentEntity(TypeInformation<?> typeInformation) {
368382

383+
try {
384+
385+
Class<?> type = typeInformation.getType();
386+
387+
E entity = createPersistentEntity(typeInformation);
369388
entity.setEvaluationContextProvider(evaluationContextProvider);
370389

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

374393
PropertyDescriptor[] pds = BeanUtils.getPropertyDescriptors(type);
375394

376-
final Map<String, PropertyDescriptor> descriptors = new HashMap<>();
395+
Map<String, PropertyDescriptor> descriptors = new HashMap<>();
377396
for (PropertyDescriptor descriptor : pds) {
378397
descriptors.put(descriptor.getName(), descriptor);
379398
}
@@ -388,21 +407,11 @@ protected Optional<E> addPersistentEntity(TypeInformation<?> typeInformation) {
388407
entity.setPersistentPropertyAccessorFactory(persistentPropertyAccessorFactory);
389408
}
390409

391-
} catch (BeansException e) {
392-
throw new MappingException(e.getMessage(), e);
410+
return entity;
393411
} catch (RuntimeException e) {
394412
persistentEntities.remove(typeInformation);
395413
throw e;
396-
} finally {
397-
write.unlock();
398414
}
399-
400-
// Inform listeners
401-
if (applicationEventPublisher != null && entity != null) {
402-
applicationEventPublisher.publishEvent(new MappingContextEvent<>(this, entity));
403-
}
404-
405-
return Optional.of(entity);
406415
}
407416

408417
/*

0 commit comments

Comments
 (0)