@EventHandler annotated function and Autowired parameters issue

Hello! This is @ Axon 3.0-M2

From this file:

https://github.com/AxonFramework/AxonFramework/blob/master/quickstart/src/main/java/org/axonframework/quickstart/annotated/ToDoEventHandler.java#L48

If I add an additional parameter, which is a Bean, to be Autowired, such as

public void handle(ToDoItemCompletedEvent event, SomeBean shouldBeAutowired) {
System.out.println(String.format(“We’ve completed the task with id %s”, event.getTodoId()));
}

It is throwing NullPointer Exception.
Am I doing it right? Should I create a ticket on youtrack?

Hi,

can you share the stacktrace you get?

Cheers,

Allard

Sure! Here it is:

Exception in thread “main” java.lang.NullPointerException
at org.axonframework.commandhandling.model.AbstractMessageHandler.(AbstractMessageHandler.java:45)
at org.axonframework.commandhandling.model.inspection.MethodEventHandlerDefinition$MethodMessageHandler.(MethodEventHandlerDefinition.java:50)
at org.axonframework.commandhandling.model.inspection.MethodEventHandlerDefinition.createHandler(MethodEventHandlerDefinition.java:39)
at org.axonframework.common.annotation.AnnotatedHandlerInspector.lambda$initializeMessageHandlers$138(AnnotatedHandlerInspector.java:78)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at org.axonframework.common.annotation.AnnotatedHandlerInspector.initializeMessageHandlers(AnnotatedHandlerInspector.java:77)
at org.axonframework.common.annotation.AnnotatedHandlerInspector.initialize(AnnotatedHandlerInspector.java:67)
at org.axonframework.common.annotation.AnnotatedHandlerInspector.lambda$createInspector$136(AnnotatedHandlerInspector.java:53)
at java.util.HashMap.computeIfAbsent(HashMap.java:1118)
at org.axonframework.common.annotation.AnnotatedHandlerInspector.createInspector(AnnotatedHandlerInspector.java:53)
at org.axonframework.common.annotation.AnnotatedHandlerInspector.inspectType(AnnotatedHandlerInspector.java:47)
at org.axonframework.eventhandling.AnnotationEventListenerAdapter.(AnnotationEventListenerAdapter.java:56)
at org.axonframework.eventhandling.AnnotationEventListenerAdapter.(AnnotationEventListenerAdapter.java:41)
at org.axonframework.eventhandling.SimpleEventHandlerInvoker.lambda$new$41(SimpleEventHandlerInvoker.java:39)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Spliterators$ArraySpliterator.forEachRemaining(Spliterators.java:948)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at org.axonframework.eventhandling.SimpleEventHandlerInvoker.(SimpleEventHandlerInvoker.java:42)
at org.axonframework.eventhandling.SimpleEventHandlerInvoker.(SimpleEventHandlerInvoker.java:34)
at org.axonframework.quickstart.RunAnnotatedAggregate.main(RunAnnotatedAggregate.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

Hi William,

I have just managed to look into this issue. It appears that you get the NullPointerException, because Axon cannot resolve the SomeBean parameter. Although it should not lead to a NPE (but a more descriptive exception), there is a mistake in your code. You need to tell Axon to look for Spring beans as candidates for parameter injection.

The easiest way to do so, is by adding @AnnotationDriven to your Spring @Configuration. Then, on the @Bean methods that create the AggregateAnnotationCommandHandler and EventSourcingRepository, pass a parameter ParameterResolverFactory. The @AnnotationDriven will make sure an appropriate instance is created.
I have just adapted the quickstart sample to show how to do this:
https://github.com/AxonFramework/AxonFramework/commit/4577274f309248d2f4148ae53c8c5e96fc86324c

Note that proper Spring support is work under construction. Soon, the @AnnotationDriven will allow you to annotated your aggregate using @AggregateRoot and all the Spring wiring is automatic…

Cheers,

Allard