Inject Spring Bean in Aggregate @EventSourcedHandler

Hi all,

What is the correct means to inject Spring beans into my Aggregate @EventSourcedHandler methods? I am aware of the registerParameterResolverFactory method on the aggregate but am not sure when & how to register the SpringBeanParameterResolver.

@CommandHandler methods are injected successfully & are configured via AnnotationCommandHandlerFactoryBean.

My aggregate @EventSourcedHandler methods are not called on applying events within a @CommandHandler.

I am injecting Spring Beans into all @CommandHandler & @EventSourcedHandler methods.

Cheers,
Roscoe

Hi Roscoe,

there seems to be an issue with Spring bean injection in @Event(Sourcing)Handler. It’s recorded under AXON-265.
I’ll let you know once it’s fixed.

Cheers,

Allard

Hi Roscoe,

how did you define the EventSourcingRepository in the Spring context? If
you use the namespace support (axon:event-sourcing-repository), it defaults
to a parameter resolver factory that also looks at Spring beans. However, a
Spring bean is only eligible for autowiring if only a single bean of the
specified type is available. Otherwise, another parameter resolver should
provide a value.

Regarding the code generation, I'm not totally convinced that using
separate "EventHandler"s and "ValidationSpec"s are really going to make
aggregate design fundamentally easier. One will end up with many more
classes, that unless they are completely generated and maintained from some
UI, will complicate things more than they simplify. One of the situations,
for example is where a single command generates multiple events in between,
where subsequent events depend on the state after the first is applied. I
don't see how the current design/implementation could handle these events.
Maybe it's just my reluctance when it comes to code generation.

Cheers,

Allard

Hi Allard,

Thanks for your feedback, much appreciated.
I’m using Spring Boot & JavaConfig, declaring the repository as such:

@Bean
@Autowired
Repository<com.zailab.user.domain.model.user.api.User> repository(EventStore eventStore, EventBus eventBus)
throws Exception
{
EventSourcingRepository<com.zailab.user.domain.model.user.api.User> repository = new EventSourcingRepository<com.zailab.user.domain.model.user.api.User>(com.zailab.user.domain.model.user.api.User.class, eventStore);
repository.setEventBus(eventBus);
return repository;
}

Thanks for your feedback on the implementation, I will take all points into consideration. The end goal is to have the aggregate designed in a UI & be flexible, we’ll see if that is achievable :slight_smile: For now it’s more of a scaffolding tool for axon apps.

Any feedback on the @EventSourcingHandler issue would be appreciated.

Cheers,
Roscoe

As addition to the above question, can Spring beans be injected into AbstractAnnotatedEntity @Event(Sourcing)Handler methods?

Cheers,
Roscoe

Hi Roscoe,

once AXON-265 is fixed, that will definitely be possible. If you don’t use the (xml) namespace support, there is a littlebit of configuration you need to add. As soon as the fix is done, I will let you know what that is.

Cheers,

Allard

Hi Allard,

Thanks again.

Cheers,
Roscoe

Hi Allard,

I see this is now working in Axon 2.3.2 :slight_smile:

Regards,