Spring-Boot Необходим общий тип в конструкторе - PullRequest
0 голосов
/ 17 марта 2019

Я столкнулся с проблемой в моем коде, и я ищу обходной путь.Это класс:

import org.axonframework.eventhandling.EventBus;
import org.axonframework.eventsourcing.EventSourcedAggregateRoot;
import org.axonframework.eventsourcing.EventSourcingRepository;
import org.axonframework.eventstore.EventStore;
import org.axonframework.repository.LockManager;
import org.axonframework.repository.PessimisticLockManager;
import org.axonframework.repository.Repository;
import org.springframework.stereotype.Component;

@Component
public class BaseCommandHandler<E extends EventSourcedAggregateRoot> {

  protected Repository<E> repository;
  protected LockManager lockManager;

  /**
   * @param eventBus eventBus.
   * @param eventStore eventStore.
   */
  public BaseCommandHandler(EventBus eventBus, EventStore eventStore) {
    this.lockManager = new PessimisticLockManager();
    EventSourcingRepository<E> newRepository =
        new EventSourcingRepository<>(E.class, eventStore, lockManager);
    newRepository.setEventBus(eventBus);
  }
}

Первая проблема, с которой я столкнулся, заключалась в том, что я не могу использовать E.class .Затем я изменил конструктор на следующий:

  public BaseCommandHandler(EventBus eventBus, EventStore eventStore, Class<E> type) {
    this.lockManager = new PessimisticLockManager();
    EventSourcingRepository<E> newRepository =
        new EventSourcingRepository<>(type, eventStore, lockManager);
    newRepository.setEventBus(eventBus);
  }
}

Но когда я запустил приложение, оно не запустилось из-за Parameter 2 of constructor in com.domain.handlers.BaseCommandHandler required a bean of type 'java.lang.Class' that could not be found.

Как я могу решить эту проблему?

1 Ответ

0 голосов
/ 17 марта 2019

Вы можете использовать GenericTypeResolver класс:

Class<E> type = (Class<E>) GenericTypeResolver.resolveTypeArgument(getClass(), BaseCommandHandler.class);
EventSourcingRepository<E> newRepository = new EventSourcingRepository<>(type, eventStore, lockManager);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...