Dependency injection and Aggregates

Could I really inject something in my Aggregate root using Spring?
By now, I have aggregate with 2 constructors, like

@CommandHandler
public Foo(MyCommand c){}

private Foo(){}

and tests with fixtures.

What I want is
@CommandHandler
public Foo(MyCommand c, Injected i){}

private Foo(Injected i){}

and some way to pass specified value for Injected in fixtures.

Or should I separate CommandHandler from aggregate and use setters for injection to make it work with axon?

Hi,

unfortunately, constructor injection in combination with aggregate command handlers is not supported.

You can currently only do setter injection, or use the a SpringPrototypeAggregateFactory to create aggregate instances. But that won't work with @commandhandler annotated aggregates.

Cheers,

Allard

Hi,

I just had an idea. In Axon 2, I use ParameterResolvers to get the values for the parameters of @CommandHandler (and @EventHandler) annotated method. I could create one that injects beans from an application context into these methods. That should solve this problem.

I’ve created issue AXON-25 to solve this.

Cheers,

Allard

Hi,

Thanks for your work. I was thinking about ParameterResolvers too. But I think more general solution needed than just creating ApplicationContextSpringResolver since it’s doesn’t solve fixture issue and could not be comparative with non spring application.

Hi,

I have created AXON-26 to address similar support in test fixtures.

Cheers,

Allard

Hi. Thanks a lot.