Axon隐式使用无参数构造函数实例化另一个聚合
创始人
2024-09-29 09:00:16
0

在使用Axon框架中,可以通过使用聚合的无参数构造函数来实例化另一个聚合。以下是一个示例代码,演示了如何在Axon中隐式使用无参数构造函数实例化另一个聚合:

首先,定义聚合类AggreagateA和AggregateB:

public class AggregateA {

    private String id;
    private AggregateB aggregateB;

    public AggregateA() {
        // 无参数构造函数
    }

    // Getter和Setter方法省略
}

public class AggregateB {

    private String id;
    
    public AggregateB() {
        // 无参数构造函数
    }

    // Getter和Setter方法省略
}

接下来,定义聚合的命令和事件:

public class CreateAggregateACommand {

    @TargetAggregateIdentifier
    private String id;

    public CreateAggregateACommand(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }
}

public class AggregateACreatedEvent {

    private String id;

    public AggregateACreatedEvent(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    }
}

然后,创建聚合的命令处理器:

public class AggregateACommandHandler {

    @CommandHandler
    public void handle(CreateAggregateACommand command) {
        AggregateA aggregateA = new AggregateA();
        aggregateA.setId(command.getId());
        aggregateA.setAggregateB(new AggregateB());

        // 发布聚合创建事件
        AggregateACreatedEvent event = new AggregateACreatedEvent(command.getId());
        apply(event);
    }
}

最后,配置Axon的聚合和命令处理器:

@Configuration
public class AxonConfig {

    @Bean
    public AggregateACommandHandler aggregateACommandHandler() {
        return new AggregateACommandHandler();
    }

    @Bean
    public CommandBus commandBus() {
        SimpleCommandBus commandBus = SimpleCommandBus.builder().build();
        commandBus.registerDispatchInterceptor(new BeanValidationInterceptor<>());
        return commandBus;
    }

    @Bean
    public EventStorageEngine eventStorageEngine() {
        return new InMemoryEventStorageEngine();
    }

    @Bean
    public EventBus eventBus() {
        return SimpleEventBus.builder().build();
    }

    @Bean
    public EventSourcingRepository aggregateARepository(EventStore eventStore) {
        return EventSourcingRepository.builder(AggregateA.class)
                .eventStore(eventStore)
                .build();
    }

    @Bean
    public EventStore eventStore(EventStorageEngine eventStorageEngine) {
        return EmbeddedEventStore.builder()
                .storageEngine(eventStorageEngine)
                .build();
    }
}

通过以上配置和代码,当发送CreateAggregateACommand命令时,聚合AggregateA将被实例化,并且聚合AggregateB也会被隐式实例化。

相关内容

热门资讯

突发!随意玩辅助器视频透视挂!... 突发!随意玩辅助器视频透视挂!果然真的有辅助下载(有挂工具)-哔哩哔哩1、下载好随意玩辅助器视频透视...
截至发稿!微信卡五星辅助器!真... 截至发稿!微信卡五星辅助器!真是真的是有辅助挂(发现有挂)-哔哩哔哩1、截至发稿!微信卡五星辅助器!...
做出回应!衢州都莱破解器!真是... 做出回应!衢州都莱破解器!真是有挂辅助软件(有挂实锤)-哔哩哔哩小薇(辅助器软件下载)致您一封信;亲...
今天下午!八仙游戏辅助!一贯存... 今天下午!八仙游戏辅助!一贯存在有辅助软件(有挂讲解)-哔哩哔哩1、进入到八仙游戏辅助是否有挂之后,...
经调查!道游辅助脚本!确实是真... 经调查!道游辅助脚本!确实是真的辅助脚本(有挂存在)-哔哩哔哩1、全新机制【道游辅助脚本ai辅助工具...
据相关数据显示!陕麻圈黑科技!... 据相关数据显示!陕麻圈黑科技!切实是有辅助软件(有挂详细)-哔哩哔哩1、进入游戏-大厅左侧-新手福利...
不少玩家反映!福州十八扑有外g... 不少玩家反映!福州十八扑有外g挂吗!其实有挂辅助修改器(详细教程)-哔哩哔哩1、完成福州十八扑有外g...
记者获悉!福建兄弟13水辅助!... 记者获悉!福建兄弟13水辅助!总是是真的辅助安装(有挂方式)-哔哩哔哩1、完成福建兄弟13水辅助辅助...
截至目前!天天贵阳app破解版... 截至目前!天天贵阳app破解版!都是真的有辅助插件(有挂解惑)-哔哩哔哩1、实时天天贵阳app破解版...
截至发稿!掌中乐游戏中心云南辅... 截至发稿!掌中乐游戏中心云南辅助!一贯存在有辅助app(有挂规律)-哔哩哔哩掌中乐游戏中心云南辅助是...