How to register custom AggregateFactory to a repository


I have an abstract aggregate root and other concrete aggregates roots which inherit that abstract AR. I have created custom aggregate factory, which can create concrete uninitialized AR instances based on first domain event.

I also have only one repository for all concrete ARs.

Now I have two problems:

  1. How can I register my custom aggregate factory to that repository in spring xml?

<axon:event-sourcing-repository id=“abstractRepository” aggregate-type="…AbstractAggregate"/> … this will automatically inject org.axonframework.eventsourcing.GenericAggregateFactory

I tried also this:

        <constructor-arg name="aggregateFactory" ref="myAggregateFactory"/>
        <constructor-arg name="eventStore" ref="eventStore"/>

  1. How can I do the same thing in test fixture when I want to test commands on one concrete type?

fixture = Fixtures.newGivenWhenThenFixture(ConcreteAggregate.class);

SomeGenericHandler handler = new SomeGenericHandler(fixture.getRepository()); // here is problem because fixture.getRepository() returns Repository but SomeGenericHandler expects Repository


Hi Adam,

  1. Simply add an attribute “aggregate-factory” to your axon:event-sourcing-repository with a reference to the AggregateFactory bean you want to use. You can omit the aggregate-type attribute in that case.
  2. in this case, your fixture has to be created using Fixtures.newGivenWhenThenFixture(AbstractAggregate.class);



Hello Allard,

thanks for your reply

  1. I used older axon schema, thanks
  2. If I create fixture using Fixtures.newGivenWhenThenFixture(AbstractAggregate.class); Exception is thrown … java.lang.IllegalArgumentException: Given aggregateType may not be abstract

Dne pátek 31. července 2015 15:28:16 UTC+2 Allard Buijze napsal(a):

  1. Ouch… :wink:
    Will check this…

Hi Adam,

I have checked, but the fixtures work as expected. The problem is that you must register the aggregate factory before retrieving the repository. The repository needs the aggregate factory, so it will create one on the fly. However, the GenericAggregateRepository doesn’t work for abstract types.
So the solution is to newGivenWhenThenFixture(AbstractAggregate.class) first, then registerAggregateFactory(), and only then do fixture.getRepository().



Now, it works :slight_smile:

Thank you very much, Allard.

Dne pondělí 3. srpna 2015 14:38:31 UTC+2 Allard Buijze napsal(a):