我已经有一个相当简单的CQRS设置使用轴突&春天。事件处理程序在一个单独的类轴突3.0.3
这是配置类。
@AnnotationDriven
@Configuration
public class AxonConfig {
@Bean
public EventStore eventStore() {
...
}
@Bean
public CommandBus commandBus() {
return new SimpleCommandBus();
}
@Bean
public EventBus eventBus() {
return new SimpleEventBus();
}
}
这是我的总结...
@Aggregate
public class ThingAggregate {
@AggregateIdentifier
private String id;
public ThingAggregate() {
}
public ThingAggregate(String id) {
this.id = id;
}
@CommandHandler
public handle(CreateThingCommand cmd) {
apply(new ThingCreatedEvent('1234', cmd.getThing()));
}
@EventSourcingHandler
public void on(ThingCreatedEvent event) {
// this is called!
}
}
这是我在一个单独的java文件事件处理程序...
@Component
public class ThingEventHandler {
private ThingRepository repository;
@Autowired
public ThingEventHandler(ThingRepository thingRepository) {
this.repository = conditionRepository;
}
@EventHandler
public void handleThingCreatedEvent(ThingCreatedEvent event) {
// this is only called if I publish directly to the EventBus
// apply within the Aggregate does not call it!
repository.save(event.getThing());
}
}
我使用CommandGateway送原始创作命令。我在Aggregate中的CommandHandler收到的命令很好,但是当我在Aggregate中调用apply
时,传递一个新的Event,我的EventHandler在外部类中不会被调用。只有直接在Aggregate类中的EventHandlers被调用。
如果我尝试直接向EventBus发布Event,则会调用我的外部EventHandler。
任何想法为什么我在外部java类中的EventHandler没有被调用,当我在Aggregate中调用apply
?