英文:
Used entityManager in command dispatch interceptors
问题
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;
};
}
}
英文:
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;
};
}
}
答案1
得分: 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!
英文:
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论