Help needed injecting spring beans into @CommandHandler constructor or method on AggregateRoot

Hi All,

I have been scratching my head all day on this one and after much googling, I have finally resorted to sending my first question to the forum!

I have an aggregate root that looks so something like this

public class ExtractJob extends AbstractAnnotatedAggregateRoot
{
private static final long serialVersionUID = -5719618724217467458L;

@AggregateIdentifier
private String id;

public ExtractJob()

{
}

@CommandHandler
public ExtractJob(CreateExtractJobCommand command, IServerConfiguration serverConfiguration, DataSource dataSource)
{
// Stuff
}
}

and I wish to inject some Spring beans (serverConfiguration and dataSource) into @CommandHandler constructor above.

According to what I have read, I should just need to the following in my Spring application context file

<axon:annotation-config event-bus=“axonEventBus” command-bus=“axonCommandBus”/>

but I am still getting:

Constructor threw exception; nested exception is org.axonframework.common.annotation.UnsupportedHandlerException: On method public au.com.calibreFT.extracts.domain.ExtractJob(au.com.calibreFT.extracts.commands.CreateExtractJobCommand,au.com.calibreFT.serverConfig.IServerConfiguration,javax.sql.DataSource), parameter 2 is invalid. It is not of any format supported by a providedParameterValueResolver.

I am using Axon 2.1.2 and was wondering if anybody can provide some guidance in getting this working?

A few extra points which may be helpful:

  1. I can confirm that the two Spring Beans that I am trying to inject are valid as they are being injected into an Event Handler without any issues.

  2. I have tried to debug the Axon source code and its appears that the SpringBeanParameterResolverFactory is not available in the collection of resolvers when the ParameterValueResolver tries to validate the Constructor signature causing the “parameter 2 is invalid. It is not of any format supported by a providedParameterValueResolver” error to be thrown

Best Regards,

David

Hi David,

are you using the namespace config to define the aggregate command handler?

<axon:aggregate-command-handler aggregate-type=“org.axonframework.quickstart.annotated.ToDoItem”
repository=“toDoRepository”
command-bus=“commandBus”/>

or do you define a Spring bean yourself?
I tried to reproduce the problem in 2.1.2, but it seems to work fine here (using the axon namespace).

Cheers,

Allard

Hi Allard,

Thanks for the prompt response to my question.

I am using the axon namespace as below:

<axon:event-sourcing-repository id=“extractJobRepository” aggregate-type=“au.com.calibreFT.extracts.domain.ExtractJob” event-store=“eventStore” event-bus=“axonEventBus” locking-strategy=“OPTIMISTIC”/>
<axon:aggregate-command-handler id=“extractJobCommandHandler” aggregate-type=“au.com.calibreFT.extracts.domain.ExtractJob” repository=“extractJobRepository” command-bus=“axonCommandBus” />

While debugging the exception, I noticed that the SpringBeanParameterResolverFactory was not in the collection that was used to resolve parameters. Would anything preclude this resolver being added?

Best Regards,
David

Hi David,

do happen to have more than one implementation of the IServerConfiguration in your application context? If applicationContext.getBean(IServerConfiguration.class) returns more than one instance, the Spring parameter resolver factory ignores it.

Note that it’s normal that the SpringBeanParameterResolverFactory is not in the list of ParamterResolvers. The factory resolves the parmeter value at startup and creates an instance of FixedValueParameterResolver for a specific hander’s parameter.

If you have the Axon source code, you could try to debug the SpringBeanParameterResolverFactory, and put a breakpoint on if line 51. That should show how Axon tries to resolve the parameter of your constructor.

Hope this helps.
Cheers,

Allard

Hi Allard,

Thanks very much for your pointers, the problem ended up being multiple spring beans of the same type being available.

I should have seen a log message telling me the issue but unfortunately it was buried in a log with many other messages from our system.

Best Regards,

David

Hi David,

I’m glad the issue is resolved.
Cheers,

Allard

Hi Allard,

I have a similar Issue with axonframework 2.2 as soon as I have axonframework-testing on my classpath. I filed a bug at http://issues.axonframework.org/youtrack/issue/AXON-254 for the issue.

Kind regards,
Michael.