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

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

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&lt;Integer, CommandMessage&lt;?&gt;, CommandMessage&lt;?&gt;&gt; handle(
		List&lt;? extends CommandMessage&lt;?&gt;&gt; messages) {
	return (index, command) -&gt; {
		 (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&lt;CommandMessage&lt;?&gt;&gt; {

	private final EntityManager entityManager;

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

	@Override
	public BiFunction&lt;Integer, CommandMessage&lt;?&gt;, CommandMessage&lt;?&gt;&gt; handle(
			List&lt;? extends CommandMessage&lt;?&gt;&gt; messages) {
		return (index, command) -&gt; {
			 (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!

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:

确定