Связать класс обслуживания с универсальным - PullRequest
0 голосов
/ 29 апреля 2020

У меня есть один интерфейс с generi c

@FunctionalInterface
public interface GenericInterface<T> {

    Optional<T> doSmth(long Id);
}

У меня есть два реализуемых класса, например GenericInterfaceImpl и GenericInterfaceImplImpl, они оба используют собственный класс как generi c в одном методе

public class GenericInterfaceImpl
    implements GenericInterface<OwnClass> {
}

public class GenericInterfaceImplImpl
    implements GenericInterface<OwnClass2> {
}

У меня есть еще два класса, которые должны использовать зависимости вышеуказанных сервисов

public class Class1 {
private final GenericInterface<OwnClass> gen;
@Inject
public Class1(GenericInterface<OwnClass> gen) {
this.gen = gen;
}
}

public class Class2 {
private final GenericInterface<OwnClass2> gen2;
@Inject
public Class2(GenericInterface<OwnClass2> gen2) {
this.gen2 = gen2;
}
}

Класс ServiceBinder

bind(GenericInterfaceImpl.class).to(GenericInterface.class);
bind(GenericInterfaceImplImpl.class).to(GenericInterface.class);

После запуска приложения при условии, что я зарегистрировал ServiceBinder, у меня появляется эта ошибка

Caused by: org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=GenericInterface,parent=Class1 etc.)

Как правильно связать ее?

1 Ответ

0 голосов
/ 30 апреля 2020

После 8 часов поиска я нашел решение

   bind(GenericInterfaceImpl.class).to(new TypeLiteral<GenericInterface<OwnClass>>() {});
   bind(GenericInterfaceImplImpl.class).to(new TypeLiteral<GenericInterface<OwnClass2>>(){});
...