Class AnnotationBasedEventSourcedEntityFactory<E,ID>

java.lang.Object
org.axonframework.eventsourcing.annotation.reflection.AnnotationBasedEventSourcedEntityFactory<E,ID>
Type Parameters:
E - The type of entity to create.
ID - The type of identifier used by the entity.
All Implemented Interfaces:
EventSourcedEntityFactory<ID,E>

public class AnnotationBasedEventSourcedEntityFactory<E,ID> extends Object implements EventSourcedEntityFactory<ID,E>
Reflection-based implementation of the EventSourcedEntityFactory interface. This factory will look for EntityCreator-annotated constructors and static methods on the entity type and its supertypes to find a suitable constructor or static method to create an entity instance.

This class implements the requirements as per the EntityCreator annotation. It also honors ForcedEntityCreator-annotated constructors and static methods, invoking them regardless of whether a first event is present, as described on ForcedEntityCreator. This class is thread-safe.

Since:
5.0.0
Author:
Mitchell Herrijgers
  • Constructor Details

    • AnnotationBasedEventSourcedEntityFactory

      public AnnotationBasedEventSourcedEntityFactory(Class<E> entityType, Class<ID> idType, ParameterResolverFactory parameterResolverFactory, MessageTypeResolver messageTypeResolver, EventConverter converter)
      Instantiate an annotation-based EventSourcedEntityFactory for the given concrete entityType. When using a polymorphic entity type, you can use the AnnotationBasedEventSourcedEntityFactory(Class, Class, Set, ParameterResolverFactory, MessageTypeResolver, EventConverter), so that all subtypes of the entity type will be scanned for static methods and constructors.
      Parameters:
      entityType - The type of the entity to create. Must be concrete.
      idType - The type of the identifier used by the entity.
      parameterResolverFactory - The factory to use to resolve parameters.
      messageTypeResolver - The factory to use to resolve the payload type.
      converter - The converter to use for converting event payloads to the handler's expected type.
    • AnnotationBasedEventSourcedEntityFactory

      public AnnotationBasedEventSourcedEntityFactory(Class<E> entityType, Class<ID> idType, Set<Class<? extends E>> subTypes, ParameterResolverFactory parameterResolverFactory, MessageTypeResolver messageTypeResolver, EventConverter converter)
      Instantiate a reflection-based EventSourcedEntityFactory for the given super entityType, with the given subTypes. The subTypes must be concrete types that extend the entityType. The factory will look for static methods and constructors on the subTypes and their supertypes to find a suitable constructor or static method to create an entity instance.
      Parameters:
      entityType - The type of the entity to create. Can be abstract.
      idType - The type of the identifier used by the entity.
      subTypes - The concrete types that extend the entityType.
      parameterResolverFactory - The factory to use to resolve parameters.
      messageTypeResolver - The factory to use to resolve the payload type.
      converter - The converter to use for converting event payloads to the handler's expected type.
  • Method Details

    • create

      public @Nullable E create(ID id, @Nullable EventMessage firstEventMessage, ProcessingContext context)
      Description copied from interface: EventSourcedEntityFactory
      Creates an entity of type E with the given identifier. The identifier is guaranteed to be non-null. The supplied firstEventMessage is the first event message that is present in the stream of the entity. If no event messages are present this method should return null, signaling this factory was not able to instantiate the entity. Using EventSourcingRepository.load(Object, ProcessingContext) would never call this method with a null firstEventMessage.

      Invocations with a non-null firstEventMessage must always return a non-null entity, while invocations with a null firstEventMessage may return null.

      Whether to return null from a null firstEventMessage invocation depends on the type of command handler which should be invoked when the entity does not exist. If this is a creational command handler, this should return null. If this is a instance command handler, this should return the non-null initial state of the entity. For example, using the no-argument constructor of the entity, or a constructor that takes the identifier as a parameter.

      It is recommended to use EventSourcedEntityFactory.fromNoArgument(Supplier), EventSourcedEntityFactory.fromIdentifier(Function) or EventSourcedEntityFactory.fromEventMessage(BiFunction) to create a factory that creates the entity based on the constructor of the entity. This will ensure that the right factory is created.

      Specified by:
      create in interface EventSourcedEntityFactory<E,ID>
      Parameters:
      id - the identifier of the entity to create. This is guaranteed to be non-null
      firstEventMessage - the first event message that is present in the stream of the entity. This may be null if no event messages are present
      context - the processing context
      Returns:
      the entity to create. This may be null if no entity could or should be created