DATAJDBC-454 - Adds generics to events.

Events now have a type parameter for the type of aggregate root they relate to.
In order to utilize this an react to only events relating to a specific type of entity `AbstractRelationalEventListener` was added.

Original pull request: #199.
This commit is contained in:
Jens Schauder
2020-03-18 09:56:28 +01:00
committed by Mark Paluch
parent 22f9eded60
commit 5fdfd3a9dc
19 changed files with 408 additions and 56 deletions

View File

@@ -609,20 +609,33 @@ For example, the following listener gets invoked before an aggregate gets saved:
[source,java]
----
@Bean
public ApplicationListener<BeforeSaveEvent> timeStampingSaveTime() {
public ApplicationListener<BeforeSaveEvent> loggingSaves() {
return event -> {
Object entity = event.getEntity();
if (entity instanceof Category) {
Category category = (Category) entity;
category.timeStamp();
}
LOG.info("{} is getting saved.";
};
}
----
====
If you want to handle events only for a specific domain type you may derive your listener from `AbstractRelationalEventListener` and overwrite one or more of the `onXXX` methods,
where `XXX` stands for an event type. It will only get invoked for events related to the domain type you used in the declaration and provides typed events, so no casting of the entity is required.
====
[source,java]
----
public class PersonLoadListener extends AbstractRelationalEventListener<Person> {
@Override
protected void onAfterLoad(AfterLoadEvent<Person> personLoad) {
LOG.info(personLoad.getEntity().setLoadTimeStamp(new Date());
}
}
----
====
The following table describes the available events:
.Available events