Annotation Interface InjectEntity
MessageHandler annotated method that should receive an entity
loaded from the StateManager.
The parameter should be one of the following three options:
- The exact entity type to inject, marked nullable if null can be expected.
- An
Optionalof the entity type to inject. - A
ManagedEntityof the entity identifier and entity type to inject.
The idProperty attribute can be used to specify the property of the message payload that contains the
identifier of the entity to inject. If not specified, the idResolver is used to resolve the identifier of the
entity to inject.
Unless a specific idResolver is specified, the AnnotationBasedEntityIdResolver is used to resolve the
entity id from the message. This is based on finding a TargetEntityId annotation on a field or accessor
method of the message payload.
So, identifiers will be resolved in the following order:
- From the property specified in
idProperty. - From the
idResolver. - From the
TargetEntityIdannotation on the message payload.
When the parameter is typed as the entity itself, and no entity can be found for the resolved identifier, the
parameter's nullability determines the outcome. By default, an
EntityNotFoundException is propagated, failing the message being
handled. A parameter annotated with an annotation resembling "nullable" will instead resolve to null,
allowing the handler to deal with a missing entity itself. This nullability support is in place to support a
create-if-missing flow. A handler can check whether the injected entity is null, create the entity, and then
proceed with subsequent tasks.
Declaring the parameter as Optional<MyEntity> achieves the same outcome without needing a "nullable"
annotation: the parameter resolves to Optional.empty() instead of null. Kotlin nullability,
as in MyEntity?, is supported as well.
A ManagedEntity-typed parameter is unaffected by nullability. Whether
or not it is annotated "nullable", it is always passed through exactly as resolved by the
StateManager backing this annotation. Note that the
ManagedEntity.entity() is marked as nullable, thus supporting similar create-if-missing behavior as
through the entity type or Optional path.
- Since:
- 5.0.0
- Author:
- Mitchell Herrijgers
-
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionThe property of the message payload that contains the identifier of the entity to inject.Class<? extends EntityIdResolver> TheEntityIdResolverto resolve the identifier of the entity to inject.
-
Element Details
-
idProperty
String idPropertyThe property of the message payload that contains the identifier of the entity to inject. If not specified, theidResolveris used to resolve the identifier of the entity to inject.- Returns:
- The property of the message payload that contains the identifier of the entity to inject.
- Default:
""
-
idResolver
Class<? extends EntityIdResolver> idResolverTheEntityIdResolverto resolve the identifier of the entity to inject. Should have a no-arg constructor.- Returns:
- The
EntityIdResolverto resolve the identifier of the entity to inject.
- Default:
org.axonframework.modelling.annotation.AnnotationBasedEntityIdResolver.class
-