Dependency injection in @EventHandler class failing

Environment: Spring Boot 2.2.0.RELEASE, Axon 4.2, Kotlin 1.3.40

I’m trying to inject a logger into my @EventHandler class.

The logger is built as a spring bean like this:
@Bean
@Scope(“prototype”)
open fun buildLogger(ip: InjectionPoint): KLogger {
// Build the logger…
}

At first I tried injecting it as a constructor parameter in my class:
@Component
open class TestProjection(@Autowired val logger: KLogger) {}

Using the debugger I can see that the logger is created successfully during application startup, but when the @EventHandler method is invoked the logger field is null…

Changing approach, I tried injecting the logger into the event handler method:
@EventHandler
fun on(event: SystemCreatedEvent, logger: KLogger) {
// Event handler logic…
}

When I do method level injection I instead get the following error from Axon/Spring:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘testProjection’ defined in file [***com/tmp/TestProjection.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘buildLogger’ defined in class path resource [com/tmp/TestConfiguration.class]: Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: No current InjectionPoint available for method ‘buildLogger’ parameter 0
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:603)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
at com.tmp.TmpApplication$Companion.main(TmpApplication.kt:12)
at com.tmp.TmpApplication.main(TmpApplication.kt)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘buildLogger’ defined in class path resource [com/tmp/TmpConfiguration.class]: Unexpected exception during bean creation; nested exception is java.lang.IllegalStateException: No current InjectionPoint available for method ‘buildLogger’ parameter 0
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:530)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:341)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:617)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeansOfType(DefaultListableBeanFactory.java:605)
at org.springframework.context.support.AbstractApplicationContext.getBeansOfType(AbstractApplicationContext.java:1242)
at org.axonframework.spring.config.annotation.SpringBeanParameterResolverFactory.createInstance(SpringBeanParameterResolverFactory.java:79)
at org.axonframework.messaging.annotation.MultiParameterResolverFactory.createInstance(MultiParameterResolverFactory.java:113)
at org.axonframework.messaging.annotation.AnnotatedMessageHandlingMember.(AnnotatedMessageHandlingMember.java:61)
at org.axonframework.messaging.annotation.AnnotatedMessageHandlingMemberDefinition.lambda$createHandler$0(AnnotatedMessageHandlingMemberDefinition.java:51)
at java.base/java.util.Optional.map(Optional.java:265)
at org.axonframework.messaging.annotation.AnnotatedMessageHandlingMemberDefinition.createHandler(AnnotatedMessageHandlingMemberDefinition.java:48)
at org.axonframework.messaging.annotation.MultiHandlerDefinition.createHandler(MultiHandlerDefinition.java:182)
at org.axonframework.messaging.annotation.AnnotatedHandlerInspector.initializeMessageHandlers(AnnotatedHandlerInspector.java:144)
at org.axonframework.messaging.annotation.AnnotatedHandlerInspector.initialize(AnnotatedHandlerInspector.java:137)
at org.axonframework.messaging.annotation.AnnotatedHandlerInspector.createInspector(AnnotatedHandlerInspector.java:105)
at org.axonframework.messaging.annotation.AnnotatedHandlerInspector.inspectType(AnnotatedHandlerInspector.java:92)
at org.axonframework.queryhandling.annotation.AnnotationQueryHandlerAdapter.(AnnotationQueryHandlerAdapter.java:75)
at org.axonframework.spring.config.annotation.AnnotationQueryHandlerBeanPostProcessor.initializeAdapterFor(AnnotationQueryHandlerBeanPostProcessor.java:60)
at org.axonframework.spring.config.annotation.AnnotationQueryHandlerBeanPostProcessor.initializeAdapterFor(AnnotationQueryHandlerBeanPostProcessor.java:38)
at org.axonframework.spring.config.AbstractAnnotationHandlerBeanPostProcessor.postProcessAfterInitialization(AbstractAnnotationHandlerBeanPostProcessor.java:86)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:431)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1807)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
… 16 common frames omitted
Caused by: java.lang.IllegalStateException: No current InjectionPoint available for method ‘buildLogger’ parameter 0
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:869)
at org.springframework.beans.factory.support.ConstructorResolver.resolvePreparedArguments(ConstructorResolver.java:821)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:438)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
… 39 common frames omitted

Does anyone have any ideas on how to resolve this issue?

Best regards,
Magnus

Hi Magnus,

note that Axon will just ask the ApplicationContext for a bean of a specific type when using the parameter injection. InjectionPoint isn’t supported in that case.
Event Handler instances themselves are mere spring beans. Axon isn’t involved in the creation of these beans, so injecting loggers should just work there. That would be the recommended approach.

Cheers,