支持带注解的处理程序
Supported Parameters for Annotated Handlers
This chapter provides an exhaustive list of all the possible parameters for annotated message handling functions. The framework resolves the parameters for any message handling function through an internal mechanism, called the ParameterResolver. The ParameterResolver, built by a ParameterResolverFactory, is in charge of inserting the parameters for the command, event and query handlers.
The set of ParameterResolvers can be extended if custom (or not yet) supported parameters should be injected in to your annotated handlers. You can configure additional ParameterResolvers by implementing the ParameterResolverFactory interface and configuring the new implementation. For more specifics on configuring custom ParameterResolvers we suggest reading this section.
Supported Parameters for Command Handlers
By default, @CommandHandler annotated methods allow the following parameter types:
The first parameter is always the payload of the command message. It may also be of type
MessageorCommandMessage, if the@CommandHandlerannotation explicitly defined the name of the command the handler can process. By default, a command name is the fully qualified class name of the command its payload.Parameters of type
MetaDatawill have the entire metadata of aCommandMessageinjected.Parameters annotated with
@MetaDataValuewill resolve the metadata value with the key as indicated on the annotation. Ifrequiredisfalse(default),nullis passed when the metadata value is not present. Ifrequiredistrue, the resolver will not match and prevent the method from being invoked when the metadata value is not present.Parameters of type
Message, orCommandMessagewill get the complete message, with both the payload and the metadata. Resolving the entireMessageis helpful if a method needs several metadata fields or other properties of the message.Parameters of type
UnitOfWorkget the current unit of work injected. TheUnitOfWorkallows command handlers to register actions to be performed at specific stages of the unit of work or gain access to the resources registered with it.A parameter of type
Stringannotated with@MessageIdentifierwill resolve the identifier of the handledCommandMessage.Parameters of type
ConflictResolverwill resolve the configuredConflictResolverinstance. See the Conflict Resolution section for specifics on this topic.Parameters of type
InterceptorChainwill resolve the chain ofMessageHandlerInterceptors for aCommandMessage. You should use this feature in conjunction with a@CommandHandlerInterceptorannotated method. For more specifics on this it is recommended to read this section.The parameter resolvers can resolve a
ScopeDescriptortoo. The scope descriptor is helpful when scheduling a deadline through theDeadlineManager. Note that theScopeDescriptoronly makes sense from within the scope of an Aggregate or Saga.If the application runs in a Spring environment, any Spring Bean can be resolved. To that end, we should annotate the desired Spring bean with
@Autowired. We can extend the annotation with@Qualifierif a specific version of the bean should be wired.
Supported Parameters for Event Handlers
By default, @EventHandler annotated methods allow the following parameter types:
The first parameter is the payload of the event message. If the event handler does not need access to the payload of the message, you can specify the expected payload type on the
@EventHandlerannotation. Do not configure the payload type on the annotation if you want the payload passed as a parameter.Parameters of type
MetaDatawill have the entire metadata of anEventMessageinjected.Parameters annotated with
@MetaDataValuewill resolve the metadata value with the key as indicated on the annotation. Ifrequiredisfalse(default),nullis passed when the metadata value is not present. Ifrequiredistrue, the resolver will not match and prevent the method from being invoked when the metadata value is not present.We can resolve the
EventMessagein its entirety as well. If the first parameter is of type message, it effectively matches an event of any type, even if generic parameters suggest otherwise. Due to type erasure, Axon cannot detect what parameter the implementation expects. It is best to declare a parameter of the payload type in such a case, followed by a parameter of type message.Parameters of type
UnitOfWorkget the current unit of work injected. TheUnitOfWorkallows event handlers to register actions to be performed at specific stages of the unit of work or gain access to the resources registered with it.A parameter of type
Stringannotated with@MessageIdentifierwill resolve the identifier of the handledEventMessage.Parameters annotated with
@Timestampand of typejava.time.Instant(orjava.time.temporal.Temporal) will resolve to the timestamp of theEventMessage. The resolved timestamp is the time at which the event was generated.Parameters annotated with
@SequenceNumberand of typejava.lang.Longorlongwill resolve to thesequenceNumberof aDomainEventMessage. This parameter provides the order in which the event was generated (within the aggregate scope it originated from). It is important to note thatDomainEventMessagecan only originate from an Aggregate. Hence, events that have been published directly on theEventBus/EventGatewayare not implementations of theDomainEventMessage. As such, they will not resolve a sequence number.Parameters of type
TrackingTokenwill have the current token related to the processed event injected. Note that this will only work forStreamingEventProcessorinstances, as otherwise, there is no token attached to the events.Parameters annotated with
@SourceIdand of typejava.lang.Stringwill resolve to theaggregateIdentifierof aDomainEventMessage. This parameter provides the identifier of the aggregate from which the event originates. It is important to note thatDomainEventMessagecan only originate from an Aggregate. Hence, events that have been published directly on theEventBus/EventGatewayare not implementations of theDomainEventMessage. As such, they will not resolve a source id.If the application runs in a Spring environment, any Spring Bean can be resolved. To that end, we should annotate the desired Spring bean with
@Autowired. We can extend the annotation with@Qualifierif a specific version of the bean should be wired.
Supported Parameters for Query Handlers
By default, @QueryHandler annotated methods allow the following parameter types:
The first parameter is always the payload of the query message. It may also be of type
MessageorQueryMessage, if the@QueryHandlerannotation explicitly defined the name of the query the handler can process. By default, a query name is the fully qualified class name of the query its payload.Parameters of type
MetaDatawill have the entire metadata of aQueryMessageinjected.Parameters annotated with
@MetaDataValuewill resolve the metadata value with the key as indicated on the annotation. Ifrequiredisfalse(default),nullis passed when the metadata value is not present. Ifrequiredistrue, the resolver will not match and prevent the method from being invoked when the metadata value is not present.Parameters of type
Message, orQueryMessagewill get the complete message, with both the payload and the metadata. Resolving the entireMessageis helpful if a method needs several metadata fields or other properties of the message.Parameters of type
UnitOfWorkget the current unit of work injected. TheUnitOfWorkallows query handlers to register actions to be performed at specific stages of the unit of work or gain access to the resources registered with it.A parameter of type
Stringannotated with@MessageIdentifierwill resolve the identifier of the handledCommandMessage.If the application runs in a Spring environment, any Spring Bean can be resolved. To that end, we should annotate the desired Spring bean with
@Autowired. We can extend the annotation with@Qualifierif a specific version of the bean should be wired.
最后更新于