Used entityManager in command dispatch interceptors

Hello everyone,

I’m using Axon 4.3 with JPA/Spring.
I want to inject entityManager in my interceptor, so i used ContainerManagedEntityManagerProvider in my configuration. but i have this error when i run my application

Description:
Parameter 0 of method configureCommandBus in AxonConfig required a bean of type ‘org.axonframework.springboot.util.jpa.ContainerManagedEntityManagerProvider’ that could not be found.

Action:
Consider defining a bean of type ‘org.axonframework.springboot.util.jpa.ContainerManagedEntityManagerProvider’ in your configuration.

`@Configuration
@AutoConfigureAfter(AxonAutoConfiguration.class)
public class AxonConfig {

@Bean
public CommandBus configureCommandBus(org.axonframework.springboot.util.jpa.ContainerManagedEntityManagerProvider containerManagedEntityManagerProvider) {
CommandBus commandBus = SimpleCommandBus.builder().build();
commandBus.registerDispatchInterceptor(
new CatalogDispatchInterceptor(containerManagedEntityManagerProvider.getEntityManager()));
return commandBus;
}

}

public class CatalogDispatchInterceptor implements MessageDispatchInterceptor<CommandMessage<?>> {

private final EntityManager entityManager;

public CatalogDispatchInterceptor(EntityManager entityManager) {
this.entityManager = entityManager;
}

@Override
public BiFunction<Integer, CommandMessage<?>, CommandMessage<?>> handle(
List<? extends CommandMessage<?>> messages) {
return (index, command) -> {
(CreateCatalogCommand.class.isInstance(command.getPayloadType())) { }
return command;
};
}

}`

Hi Eriksen,

If you are relying on the JpaAutoConfiguration to create the ContainerManagedEntityManagerProvider, it would be better to expect a bean of type EntityManagerProvider instead.
Stating this as the JpaAutoConfiguration does the following:

@Bean
@ConditionalOnMissingBean
public EntityManagerProvider entityManagerProvider() {
return new ContainerManagedEntityManagerProvider();
}

So, it returns an EntityManagerProvider, not a ContainerManagedEntityManagerProvider.
Could be wrong with my assumption here, but I am relatively confident Spring will see the former here as the thing to wire instead of the latter.

If this does not resolve your problem, then you are dealing with a spring ordering issue.
Might help to also add the JpaAutoConfiguration to you @AutoConfigureAfter annotation in that case.

Hoping this helps you further Aymen!

Cheers,
Steven

PS. It is important to note that this mailing list will be discontinued as specified in this thread.
Instead of this mailing list we have moved to use a forum style of communication, at https://discuss.axoniq.io/.

Hope to see you there!