Dependency injection and Aggregates

528 views
Skip to first unread message

Stanislav Kurilin

unread,
Jul 27, 2012, 11:41:04 AM7/27/12
to axonfr...@googlegroups.com
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?


bui...@gmail.com

unread,
Jul 28, 2012, 5:52:03 AM7/28/12
to axonfr...@googlegroups.com
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

Allard Buijze

unread,
Jul 28, 2012, 1:39:33 PM7/28/12
to axonfr...@googlegroups.com
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

Stanislav Kurilin

unread,
Jul 30, 2012, 7:32:38 AM7/30/12
to axonfr...@googlegroups.com
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. 

bui...@gmail.com

unread,
Jul 30, 2012, 3:57:47 PM7/30/12
to axonfr...@googlegroups.com
Hi,

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

Cheers,

Allard

Stanislav Kurilin

unread,
Jul 31, 2012, 4:13:17 AM7/31/12
to axonfr...@googlegroups.com
Hi. Thanks a lot.
Reply all
Reply to author
Forward
0 new messages