Struggling with Spring bean autowired in aggregate, Axon 3

How do I autowire a bean for use in an aggregate?

I’ve got the standard @Bean setup in a Java @Configuration

Like this:

public UtilityMethods utilityMethods() {
    return new UtilityMethods();

I need it in an Aggregate:

public class OrderAggr {

    private UtilityMethods util;

But it keeps being null.

Any help much appreciated

You can add it as a parameter to your @CommandHandler annotated method, wherever you need it. There’s also no need to store it in a field.
Field injection does not work, yet, as Spring doesn’t manage the creation of instances of this aggregate.



Thx Allard, now I understand :slight_smile: If Spring does not handle the instantiation.
I need this as a util for an @EventHandler method, same procedure as @CommandHandler?

Hi Jan,

yes, it works the same way. Just add it as a parameter.