使用了实体管理器在命令调度拦截器中。

huangapple go评论101阅读模式
英文:

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 {

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

}

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

  1. private final EntityManager entityManager;
  2. public CatalogDispatchInterceptor(EntityManager entityManager) {
  3. this.entityManager = entityManager;
  4. }
  5. @Override
  6. public BiFunction&lt;Integer, CommandMessage&lt;?&gt;, CommandMessage&lt;?&gt;&gt; handle(
  7. List&lt;? extends CommandMessage&lt;?&gt;&gt; messages) {
  8. return (index, command) -&gt; {
  9. (CreateCatalogCommand.class.isInstance(command.getPayloadType())) { }
  10. return command;
  11. };
  12. }

}

英文:

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.

  1. @Configuration
  2. @AutoConfigureAfter(AxonAutoConfiguration.class)
  3. public class AxonConfig {
  4. @Bean
  5. public CommandBus configureCommandBus(org.axonframework.springboot.util.jpa.ContainerManagedEntityManagerProvider containerManagedEntityManagerProvider) {
  6. CommandBus commandBus = SimpleCommandBus.builder().build();
  7. commandBus.registerDispatchInterceptor(
  8. new CatalogDispatchInterceptor(containerManagedEntityManagerProvider.getEntityManager()));
  9. return commandBus;
  10. }
  11. }
  12. public class CatalogDispatchInterceptor implements MessageDispatchInterceptor&lt;CommandMessage&lt;?&gt;&gt; {
  13. private final EntityManager entityManager;
  14. public CatalogDispatchInterceptor(EntityManager entityManager) {
  15. this.entityManager = entityManager;
  16. }
  17. @Override
  18. public BiFunction&lt;Integer, CommandMessage&lt;?&gt;, CommandMessage&lt;?&gt;&gt; handle(
  19. List&lt;? extends CommandMessage&lt;?&gt;&gt; messages) {
  20. return (index, command) -&gt; {
  21. (CreateCatalogCommand.class.isInstance(command.getPayloadType())) { }
  22. return command;
  23. };
  24. }
  25. }

答案1

得分: 1

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

  1. @Bean
  2. @ConditionalOnMissingBean
  3. public EntityManagerProvider entityManagerProvider() {
  4. return new ContainerManagedEntityManagerProvider();
  5. }

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:

  1. @Bean
  2. @ConditionalOnMissingBean
  3. public EntityManagerProvider entityManagerProvider() {
  4. return new ContainerManagedEntityManagerProvider();
  5. }

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!

huangapple
  • 本文由 发表于 2020年8月10日 02:14:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63329765.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定