problem injecting Spring bean in an Aggregate command handler using custom Aggregate repository

Hi Steven,

Separating the @Qualifier annotation had no effect.
Adding a FixedValueParameterResolver would be a solution for the issue. However, I resolved it differently:

  • I no longer instantiate my Klas Eventsourcing repository but let axon do it (Removed KlasRepositoryConfig). If I would need more controle over the configuration of the klasRepository, using it with the FixedValueParameterResolver would be fine.
  • I changed the implementation of KlasFactory (= the class where the repository was supposed to be injected into)

`
@Component
open class KlasFactory {
@Autowired
private lateinit var commands: Commands
@Autowired
private lateinit var springContext : ApplicationContext

@CommandHandler
fun cmd(cmd: MaakKlasIndienDieNogNietBestaat): KlasId {
val klasId = klasId(cmd.klas)

try {
commands.execute(Ping(klasId))
} catch (exc: CommandExecutionException) {
this.maakKlas(cmd.klas)
}

return klasId
}

private fun maakKlas(klasGegevens: KlasGegevens) {
try {
val repository = loadRepository();
repository.newInstance { Klas(klasGegevens) }
} catch (exc: Exception) {
commands.execute(Ping(klasId(klasGegevens)))
}
}

private fun loadRepository(): EventSourcingRepository {
return springContext.getBean(
“klasRepository”, EventSourcingRepository::class.java) as EventSourcingRepository
}
}
`

Now, the klasRepository is not injected anymore, but I get is from the Spring context when needed, which is much later then when Axon’s AnnotationCommandHandlerBeanPostProcessor kicked in, so now that is fine.

Why using an AlbumIdGenerator? In my little project, a Klas is responsible for creating an album if it does not exist yet. Creating an album is also creating an ID for it. Creating the id using a static method is possible (AlbumId.generate()) but as a bean, it is just easier to test:

  • you can mock the bean to set its behaviour
  • by setting its behaviour, you can test the Klas aggregate easier using aggregate fixtures. It’s just easier to specifiy/compare the events if you can predict the ID that klas will generate

With this very simple case, I could expand the static code to alter the generator behaviour in tests. But this is also works as a sample case for injecting far more complex services.

thank you!