Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Spring Beans as parameters in @MessageHandler methods #107

Open
frederikb opened this issue Nov 5, 2024 · 0 comments
Open

Support Spring Beans as parameters in @MessageHandler methods #107

frederikb opened this issue Nov 5, 2024 · 0 comments

Comments

@frederikb
Copy link

I was solving the same requirement for a client of mine when I stumbled upon your work here and gave it a try.

Scenario

  • extension version: 0.1.0
  • Axon Framework version: 4.10.2
  • Description of your use case:

I would like to declare custom Spring beans to be injected in the @MessageHandler event handling methods, e.g.:

@MessageHandler
  protected void apply(
      DomainEvents.MyEvent event, MyBean myBean) {
}

as supported for regular Axon event handlers ( https://docs.axoniq.io/axon-framework-reference/4.10/messaging-concepts/supported-parameters-annotated-handlers/ )

Current Behaviour

This fails with the following exception:

2024-11-05 | 09:47:44.782 | ERROR | http-nio-8080-exec-6 | o.a.c.c.C.[.[.[.[dispatcherServlet] | Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.axonframework.test.FixtureExecutionException: No resource of type [com.example.MyBean] has been registered. It is required for one of the handlers being executed.] with root cause

org.axonframework.test.FixtureExecutionException: No resource of type [com.example.MyBean] has been registered. It is required for one of the handlers being executed.
	at org.axonframework.test.FixtureResourceParameterResolverFactory$FailingParameterResolver.resolveParameterValue(FixtureResourceParameterResolverFactory.java:58)
	at org.axonframework.messaging.annotation.AnnotatedMessageHandlingMember.resolveParameterValues(AnnotatedMessageHandlingMember.java:177)
	at org.axonframework.messaging.annotation.AnnotatedMessageHandlingMember.handle(AnnotatedMessageHandlingMember.java:153)
	at io.holixon.axon.projection.adhoc.EventApplier.applyEvent(EventApplier.kt:33)

The underlying issues lies by directly instantiating ClasspathParameterResolverFactory here:

val parameterResolverFactory = ClasspathParameterResolverFactory.forClass(inspectedType)

where as for a usage with Spring we would need a MultiParameterResolverFactory which is set up with additional Spring specific resolvers (see how Axon does it here)

Solution: When running with Spring it should reuse the existing instance of ParameterResolverFactory in the application context.

Wanted Behaviour

The Spring bean (which is declared and registered in the application context) is resolved and the message handler method called.

Possible Workarounds

Use workaround such as a application context provider to statically gain access the Spring application context and thus retrieving the required beans from the handler method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant