SpringPrototypeEventSourcingRepository and aggregate snapshots (Axon 1.1)

Hi,

I’m using the SpringPrototypeEventSourcingRepository for the first time and cannot track down why aggregates that are loaded from snapshot events don’t get their dependencies injected. I’m still using Axon 1.1 (and cannot migrate for the time being).

SpringPrototypeEventSourcingRepository is an AggregateFactory itself in 1.1 that loads the aggregate prototype from the application context. If the aggregate is loaded solely with simple domain events, everything works as expected, but as soon as the trigger creates a snapshot, the repository does not use the AggregateFactory, only the snapshot (see method createAggregate(identifier, firstEvent) in EventSourcingRepository). Dependencies don’t get injected resulting in nasty NPEs.

Any help would be appreciated, or do I simply miss something?

Best regards,
Hendrik

Hi Hendrik,

I’m affraid there is a limitation of the SpringPrototypeEventSourcingRepository, or a bug as you will. The problem is that the snapshot itself (by default) is the actual aggregate. So it is no longer created from the bean definition.

The solution is to “autowire” the aggregate using the bean defitinition after is has been created from the snapshot.

Since you cannot upgrade, a quick workaround would be to create a subclass of SpringPrototypeEventSourcingRepository and override the createAggregate method. In there, you should check whether the “firstEvent” is an instance of AggregateSnapshot. If so, grab the aggregate from it and call:
applicationContext.getAutowireCapableBeanFactory().configureBean(aggregate, aggregateBeanName);
That will call all the Spring specific methods and wire the bean according to the specification in your application context.

Hope this helps. Sorry for the inconvenience.
I will report an issue to have this problem fixed in 1.4 and 2.0.

Cheers,

Allard

Hi Allard,

thanks for your help! It’s actually what I did already.

Best regards
Hendrik