AutoConfigure

AxonAutoConfiguration

在 axon 的 spring-boot-autoconfigure 子项目下,找到 spring.factories 文件:

AutoConfigure - 图1

其内容如下:

  1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.axonframework.boot.AxonAutoConfiguration

自动配置的内容

Serializer

自动配置的 Serializer 是 XStreamSerializer

  1. @Bean
  2. @ConditionalOnMissingBean
  3. public Serializer serializer() {
  4. XStreamSerializer xStreamSerializer = new XStreamSerializer();
  5. xStreamSerializer.getXStream().setClassLoader(beanClassLoader);
  6. return xStreamSerializer;
  7. }

EventStore

自动配置的 EventStore 是 EmbeddedEventStore

  1. @Qualifier("eventStore")
  2. @Bean(name = "eventBus")
  3. @ConditionalOnMissingBean({EventBus.class, EventStore.class})
  4. @ConditionalOnBean(EventStorageEngine.class)
  5. public EventStore eventStore(EventStorageEngine storageEngine, AxonConfiguration configuration) {
  6. return new EmbeddedEventStore(storageEngine, configuration.messageMonitor(EventStore.class, "eventStore"));
  7. }

EventBus

自动配置的 EventBus 是 SimpleEventBus

  1. @Bean
  2. @ConditionalOnMissingBean({EventStorageEngine.class, EventBus.class, EventStore.class})
  3. public EventBus eventBus(AxonConfiguration configuration) {
  4. return new SimpleEventBus(Integer.MAX_VALUE, configuration.messageMonitor(EventStore.class, "eventStore"));
  5. }

CommandBus

自动配置的 CommandBus 是 SimpleCommandBus

  1. @ConditionalOnMissingBean(ignored = {DistributedCommandBus.class})
  2. @Qualifier("localSegment")
  3. @Bean
  4. public CommandBus commandBus(TransactionManager txManager, AxonConfiguration axonConfiguration) {
  5. SimpleCommandBus commandBus = new SimpleCommandBus(txManager, axonConfiguration.messageMonitor(CommandBus.class, "commandBus"));
  6. commandBus.registerHandlerInterceptor(new CorrelationDataInterceptor<>(axonConfiguration.correlationDataProviders()));
  7. return commandBus;
  8. }

TransactionManager

自动配置 TransactionManager 时,先看有没有 PlatformTransactionManager,如果有则配置为 SpringTransactionManager,如果没有,则取默认配置,结果为 NoTransactionManager:

  1. @AutoConfigureAfter(TransactionConfiguration.class)
  2. @Configuration
  3. public static class DefaultTransactionConfiguration {
  4. @Bean
  5. @ConditionalOnMissingBean(TransactionManager.class)
  6. public TransactionManager axonTransactionManager() {
  7. return NoTransactionManager.INSTANCE;
  8. }
  9. }
  10. @Configuration
  11. @ConditionalOnClass(PlatformTransactionManager.class)
  12. @AutoConfigureAfter(name = "org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration")
  13. public static class TransactionConfiguration {
  14. @Bean
  15. @ConditionalOnMissingBean
  16. @ConditionalOnBean(PlatformTransactionManager.class)
  17. public TransactionManager axonTransactionManager(PlatformTransactionManager transactionManager) {
  18. return new SpringTransactionManager(transactionManager);
  19. }
  20. }