|
| 1 | +[[entity-callbacks]] |
| 2 | += Entity Callbacks |
| 3 | + |
| 4 | +The Spring Data infrastructure provides hooks for modifying an entity before and after certain methods are invoked. |
| 5 | +Those so called `EntityCallback` instances provide a convenient way to check and potentially modify an entity in a callback fashioned style. + |
| 6 | +An `EntityCallback` looks pretty much like a specialized `ApplicationListener`. |
| 7 | +Some Spring Data modules publish store specific events (such as `BeforeSaveEvent`) that allow modifying the given entity. In some cases, such as when working with immutable types, these events can cause trouble. |
| 8 | +Also, event publishing relies on `ApplicationEventMulticaster`. If configuring that with an asynchronous `TaskExecutor` it can lead to unpredictable outcomes, as event processing can be forked onto a Thread. |
| 9 | + |
| 10 | +Entity callbacks provide integration points with both synchronous and reactive APIs to guarantee in-order execution at well-defined checkpoints within the processing chain, returning a potentially modified entity or an reactive wrapper type. |
| 11 | + |
| 12 | +Entity callbacks are typically separated by API type. This separation means that a synchronous API considers only synchronous entity callbacks and a reactive implementation considers only reactive entity callbacks. |
| 13 | + |
| 14 | +[NOTE] |
| 15 | +==== |
| 16 | +The Entity Callback API has been introduced with Spring Data Commons 2.2. It is the recommended way of applying entity modifications. |
| 17 | +Existing store specific `ApplicationEvents` are still published *before* the invoking potentially registered `EntityCallback` instances. |
| 18 | +==== |
| 19 | + |
| 20 | +[[entity-callbacks.implement]] |
| 21 | +== Implementing Entity Callbacks |
| 22 | + |
| 23 | +An `EntityCallback` is directly associated with its domain type through its generic type argument. |
| 24 | +Each Spring Data module typically ships with a set of predefined `EntityCallback` interfaces covering the entity lifecycle. |
| 25 | + |
| 26 | +.Anatomy of an `EntityCallback` |
| 27 | +==== |
| 28 | +[source,java] |
| 29 | +---- |
| 30 | +@FunctionalInterface |
| 31 | +public interface BeforeSaveCallback<T> extends EntityCallback<T> { |
| 32 | +
|
| 33 | + /** |
| 34 | + * Entity callback method invoked before a domain object is saved. |
| 35 | + * Can return either the same or a modified instance. |
| 36 | + * |
| 37 | + * @return the domain object to be persisted. |
| 38 | + */ |
| 39 | + T onBeforeSave(T entity <2>, String collection <3>); <1> |
| 40 | +} |
| 41 | +---- |
| 42 | +<1> `BeforeSaveCallback` specific method to be called before an entity is saved. Returns a potentially modifed instance. |
| 43 | +<2> The entity right before persisting. |
| 44 | +<3> A number of store specific arguments like the _collection_ the entity is persisted to. |
| 45 | +==== |
| 46 | + |
| 47 | +.Anatomy of a reactive `EntityCallback` |
| 48 | +==== |
| 49 | +[source,java] |
| 50 | +---- |
| 51 | +@FunctionalInterface |
| 52 | +public interface ReactiveBeforeSaveCallback<T> extends EntityCallback<T> { |
| 53 | +
|
| 54 | + /** |
| 55 | + * Entity callback method invoked on subscription, before a domain object is saved. |
| 56 | + * The returned Publisher can emit either the same or a modified instance. |
| 57 | + * |
| 58 | + * @return Publisher emitting the domain object to be persisted. |
| 59 | + */ |
| 60 | + Publisher<T> onBeforeSave(T entity <2>, String collection <3>); <1> |
| 61 | +} |
| 62 | +---- |
| 63 | +<1> `BeforeSaveCallback` specific method to be called on subscription, before an entity is saved. Emits a potentially modifed instance. |
| 64 | +<2> The entity right before persisting. |
| 65 | +<3> A number of store specific arguments like the _collection_ the entity is persisted to. |
| 66 | +==== |
| 67 | + |
| 68 | +NOTE: Optional entity callback parameters are defined by the implementing Spring Data module and inferred from call site of `EntityCallback.callback()`. |
| 69 | + |
| 70 | +Implement the interface suiting your application needs like shown in the example below: |
| 71 | + |
| 72 | +.Example `BeforeSaveCallback` |
| 73 | +==== |
| 74 | +[source,java] |
| 75 | +---- |
| 76 | +class DefaultingEntityCallback implements BeforeSaveCallback<Person>, Ordered { <2> |
| 77 | +
|
| 78 | + @Override |
| 79 | + public Object onBeforeSave(Person entity, String collection) { <1> |
| 80 | +
|
| 81 | + if(collection == "user") { |
| 82 | + return // ... |
| 83 | + } |
| 84 | +
|
| 85 | + return // ... |
| 86 | + } |
| 87 | +
|
| 88 | + @Override |
| 89 | + public int getOrder() { |
| 90 | + return 100; <2> |
| 91 | + } |
| 92 | +} |
| 93 | +---- |
| 94 | +<1> Callback implementation according to your requirements. |
| 95 | +<2> Potentially order the entity callback if multiple ones for the same domain type exist. Ordering follows lowest precedence. |
| 96 | +==== |
| 97 | + |
| 98 | +[[entity-callbacks.register]] |
| 99 | +== Registering Entity Callbacks |
| 100 | + |
| 101 | +`EntityCallback` beans are picked up by the store specific implementations in case they are registered in the `ApplicationContext`. |
| 102 | +Most template APIs already implement `ApplicationContextAware` and therefore have access to the `ApplicationContext` |
| 103 | + |
| 104 | +The following example explains a collection of valid entity callback registrations: |
| 105 | + |
| 106 | +.Example `EntityCallback` Bean registration |
| 107 | +==== |
| 108 | +[source,java] |
| 109 | +---- |
| 110 | +@Order(1) <1> |
| 111 | +@Component |
| 112 | +class First implements BeforeSaveCallback<Person> { |
| 113 | +
|
| 114 | + @Override |
| 115 | + public Person onBeforeSave(Person person) { |
| 116 | + return // ... |
| 117 | + } |
| 118 | +} |
| 119 | +
|
| 120 | +@Component |
| 121 | +class DefaultingEntityCallback implements BeforeSaveCallback<Person>, |
| 122 | + Ordered { <2> |
| 123 | +
|
| 124 | + @Override |
| 125 | + public Object onBeforeSave(Person entity, String collection) { |
| 126 | + // ... |
| 127 | + } |
| 128 | +
|
| 129 | + @Override |
| 130 | + public int getOrder() { |
| 131 | + return 100; <2> |
| 132 | + } |
| 133 | +} |
| 134 | +
|
| 135 | +@Configuration |
| 136 | +public class EntityCallbackConfiguration { |
| 137 | +
|
| 138 | + @Bean |
| 139 | + BeforeSaveCallback<Person> unorderedLambdaReceiverCallback() { <3> |
| 140 | + return (BeforeSaveCallback<Person>) it -> // ... |
| 141 | + } |
| 142 | +} |
| 143 | +
|
| 144 | +@Component |
| 145 | +class UserCallbacks implements BeforeConvertCallback<User>, |
| 146 | + BeforeSaveCallback<User> { <4> |
| 147 | +
|
| 148 | + @Override |
| 149 | + public Person onBeforeConvert(User user) { |
| 150 | + return // ... |
| 151 | + } |
| 152 | +
|
| 153 | + @Override |
| 154 | + public Person onBeforeSave(User user) { |
| 155 | + return // ... |
| 156 | + } |
| 157 | +} |
| 158 | +---- |
| 159 | +<1> `BeforeSaveCallback` receiving its order from the `@Order` annotation. |
| 160 | +<2> `BeforeSaveCallback` receiving its order via the `Ordered` interface implementation. |
| 161 | +<3> `BeforeSaveCallback` using a lambda expression. Unordered by default and invoked last. Note that callbacks implemented by a lambda expression do not expose typing information hence invoking these with a non-assignable entity affects the callback throughput. Use a `class` or `enum` to enable type filtering for the callback bean. |
| 162 | +<4> Combine multiple entity callback interfaces in a single implementation class. |
| 163 | +==== |
0 commit comments