commandhandler static factory methods

In the webinar for 3.2 it was mentioned, that a feature of 3.1 was the support of commandHandler factory methods. I couldn’t find an example and was not able to get it work.
I assumed I would do something like


@Aggregate
class AccountAggregate() {

  companion object : KLogging() {
    @CommandHandler
    fun create(c: Command.CreateAccount): AccountAggregate {
      apply(Event.AccountCreated(c.id))
      return AccountAggregate()
    }
  }

  @AggregateIdentifier
  lateinit var id: String
  var balance: Int = 0

  @EventSourcingHandler
  fun on(e: Event.AccountCreated) {
    id = e.id
    balance = e.balance
  }
}

but I get No handler was subscribed to command [org.springbootcamp.axon.querybus.Command$CreateAccount]

What did I get wrong?

Hi Jan,

Generally when working in Kotlin, to get a companion object method recognized as a static class method from a Java perspective, you need the @JvmStatic annotation:
https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#static-methods

The code for this feature uses a Modifier.isStatic call to check whether a method qualifies as a factory method, so you would need @JvmStatic to make that work.

Could you try again with this annotation?

Kind regards,

wow, great, thanks very much! Works.