具有通用的绑定服务类

问题描述 投票:0回答:1

我有一个通用接口

@FunctionalInterface
public interface GenericInterface<T> {

    Optional<T> doSmth(long Id);
}

我有两个实现类,例如GenericInterfaceImpl和GenericInterfaceImplImpl,它们都使用自己的类作为一种方法的泛型

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.)

如何正确绑定它?

java jersey jersey-2.0 hk2
1个回答
0
投票

经过8个小时的搜索,我找到了解决方法

© www.soinside.com 2019 - 2024. All rights reserved.