PolishCivil
PolishCivil

Reputation: 141

Axon event handler and query handlers do not work together in kotlin

Code:

@Component
open class UserProjectionQuery {
    @Autowired
    private lateinit var repository: UserDocumentRepository
    @Autowired
    private lateinit var updateEmitter: QueryUpdateEmitter

    @QueryHandler
    fun handle(query: QueryUserIdForUsername): String? {
       //stuff
    }

    @EventHandler
    fun on(evt: UserAuthenticated) {
       //stuff
    }
}

Apparently it doesn't inject my repository nor update emitter. The event handler creates AnnotationQueryHandlerAdapter instead of AnnotationEventHandlerAdapter.

The solution atm is to move out the @EventHandler's to separate class and keep them there while @QueryHandler's in separate class too.

I remember it was working with java. I'm not sure what i'm missing here but it seems like it's a spring-kotlin bug? Any tip would be appreciated, thanks

Upvotes: 0

Views: 768

Answers (1)

Ivan Dugalic
Ivan Dugalic

Reputation: 661

This is weird. I'm sure that my example is working correctly with the similar use case

https://github.com/idugalic/digital-restaurant/blob/master/drestaurant-apps/drestaurant-microservices-rest/drestaurant-microservices-rest-restaurant/src/main/kotlin/com/drestaurant/query/handler/RestaurantHandler.kt

Consider using constructor to inject your dependencies (you will avoid var and lateinit)

class UserProjectionQuery(private val repository: UserDocumentRepository, private val updateEmitter: QueryUpdateEmitter)

You can also use maven plugin kotlin-maven-plugin to open all Spring configuration classes: https://github.com/idugalic/digital-restaurant/blob/master/pom.xml#L177

Upvotes: 3

Related Questions