0

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;
        };
    }

}
Aymen Kanzari
  • 1,765
  • 7
  • 41
  • 73

1 Answers1

1

The ContainerManagedEntityManagerProvider instance created by Axon, if you are using the Spring Boot Starter, through the JpaAutoConfiguration looks as follows:

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

Hence my first try would be to wire in a EntityManagerProvider instead of the ContainerManagedEntityManagerProvider. If that doesn't work, then you're dealing with a Spring bean ordering issue, which is somewhat out of the (axon) framework's scope I think. You could always just create the ContainerManagedEntityManagerProvider yourself of course, which i am pretty certain of will solve the problem at hand.

Hope either solution helps you out Aymen!

Steven
  • 6,936
  • 1
  • 16
  • 31
  • thnx Steven for your answer, it works :) i have another question, do you have an idea how i can check the type of PayloadType, i used isInstance as indicated in above code, but it's not working. thnx – Aymen Kanzari Aug 11 '20 at 19:46
  • This essentially is not an Axon question, so I will be referring you to this StackOverflow issue for that -> https://stackoverflow.com/questions/541749/how-to-determine-an-objects-class – Steven Aug 12 '20 at 14:28