@EventHandler annotated function and Autowired parameters issue

73 views
Skip to first unread message

William Huang

unread,
Jul 14, 2016, 3:06:25 PM7/14/16
to Axon Framework Users
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



@EventHandler

public void handle(ToDoItemCompletedEvent event) {

System.out.println(String.format("We've completed the task with id %s", event.getTodoId()));

}

}

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?

Allard Buijze

unread,
Jul 14, 2016, 3:20:20 PM7/14/16
to Axon Framework Users
Hi,

can you share the stacktrace you get?

Cheers,

Allard
--
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.

William Huang

unread,
Jul 15, 2016, 2:23:10 PM7/15/16
to Axon Framework Users
Sure! Here it is:

Exception in thread "main" java.lang.NullPointerException
    at org.axonframework.commandhandling.model.AbstractMessageHandler.<init>(AbstractMessageHandler.java:45)
    at org.axonframework.commandhandling.model.inspection.MethodEventHandlerDefinition$MethodMessageHandler.<init>(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.<init>(AnnotationEventListenerAdapter.java:56)
    at org.axonframework.eventhandling.AnnotationEventListenerAdapter.<init>(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.<init>(SimpleEventHandlerInvoker.java:42)
    at org.axonframework.eventhandling.SimpleEventHandlerInvoker.<init>(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)

Allard Buijze

unread,
Aug 2, 2016, 8:33:53 AM8/2/16
to Axon Framework Users
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:

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
Reply all
Reply to author
Forward
0 new messages