Injecting object in Aggregate command Handler without spring boot

173 views
Skip to first unread message

David Stibbe

unread,
Feb 6, 2019, 9:51:51 AM2/6/19
to Axon Framework Users
Hi,

I have a configuration that looks as following : 

    @Bean("parameterResolverFactory")
    @Bean
    fun axonConfig(commandBus: CommandBus,
                   eventStore: EventStore,
                   projector: Projector): org.axonframework.config.Configuration {

        val configurer = DefaultConfigurer.defaultConfiguration()
        configurer.configureAggregate(MyAggregate::class.java)

        configurer.configureCommandBus { commandBus }
        configurer.configureEventStore { eventStore }


        val configuration = configurer
                .buildConfiguration()

        configuration.start()
        return configuration
    }

and 
configured a SpringParameterResolverFactoryBean:



@Bean("parameterResolverFactory")
fun parameterResolverFactory(): SpringParameterResolverFactoryBean {
log.info("INIT SpringParameterResolverFactoryBean")
return SpringParameterResolverFactoryBean()
}





However,

when I try to inject something in a command handler of my aggregate, eg:

@Aggregate
class MyAggregate() {
 ...
 @CommandHandler
fun doSomething(command: MyCommand, someInjectedThing:SomeThing) {
   ...
}
...
}




I get an exception during startup :


Caused by: org.axonframework.messaging.annotation.UnsupportedHandlerException: Unable to resolve parameter 1 (SomeThing) in handler public void nl.dstibbe.labs.axon.MyAggregate.doSomething(nl.dstibbe.labs.axon.MyCommand,nl.dstibbe.labs.axon.SomeThing).


which happens on configuration.start()

Any idea what I am missing?  I thought I had this working before, but it seems it isn't.

N.B.
This is really lacking in the docs


Kind regards,
David Stibbe















Robert Delgado

unread,
Feb 6, 2019, 4:27:53 PM2/6/19
to Axon Framework Users
I'm experiencing the same issue -- however, instead of autowiring in the function I autowire at initialization... for example:

@Aggregate(repository="bidAggregateRepository")

public class BidAggregate {

@AggregateIdentifier

    private String id;

@Autowired

private BidServicesHelper bidhelper;

@Autowired

private HashMap<String,String> participantCache;


Needless to say, all my autowires are null

Allard Buijze

unread,
Feb 8, 2019, 10:29:05 AM2/8/19
to Axon Framework Users
Autowires are null, because Spring doesn't manage your aggregates, hence, @Autowired is ignored.

David, the class I think you intend to use is the SpringBeanParameterResolverFactory. That's the ParameterResolver that allows you to resolve Spring Beans declared as parameters to @...Handler methods.
Note that when using Spring Boot, this is autoconfigured.

You may have to register the ParameterResolver with the configurer as a component:
configurer.registerComponent(ParameterResolverFactory.class, 
MultiParameterResolverFactory.ordered(ClasspathParameterResolverFactory.forClass(getClass()),                                                     new ConfigurationParameterResolverFactory(config),
 /* add your own here */)

Hope this helps.
Cheers,

Allard Buijze
CTO

T: +31 6 34 73 99 89


--
You received this message because you are subscribed to the Google Groups "Axon Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to axonframewor...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages