[英]Guice generic binding interface to implementation bounded type parameter
I am developing a JAX-RS based Java application using Google Guice for dependency injection. 我正在使用Google Guice开发依赖于注入的基于JAX-RS的Java应用程序。 I have the following interface in my code:
我的代码中包含以下界面:
public interface LockProvider<L extends Lock> {
Optional<L> acquireLock(String lockId);
void releaseLock(L lock);
}
In the above interface, Lock is an interface defined as follows: 在上面的界面中,Lock是一个定义如下的界面:
public interface Lock {
String getLockId();
}
Lock interface is implemented by the following class: 锁接口由以下类实现:
public class DynamoDBLock implements Lock {
private final String lockId;
private final LockItem underLyingLock;
public DynamoDBLock(final String lockId, final LockItem underLyingLock) {
this.lockId = lockId;
this.underLyingLock = underLyingLock;
}
@Override
public String getLockId() {
return lockId;
}
public LockItem getUnderlyingLock() {
return underlyingLock;
}
}
LockProvider interface is implemented by the following class: LockProvider接口由以下类实现:
public class DynamoDBLockProvider implements LockProvider<DynamoDBLock> {
Optional<DynamoDBLock> acquireLock(String lockId) {
//implementation here
}
void releaseLock(DynamoDBLock lock) {
LockItem underlyingLockItem = lock.getUnderlyingLockItem();
//do something with underlyingLockItem
}
}
I don't want classes in my application other than LockProvider to know about underLying lock item, which is why I haven't included getUnderlyingLockItem in the Lock interface. 我不希望LockProvider之外的应用程序中的类了解underLying锁定项,这就是为什么我在Lock接口中未包含getUnderlyingLockItem的原因。
Now, when I try to bind LockProvider to DynamoDBLockProvider as follows: 现在,当我尝试将LockProvider绑定到DynamoDBLockProvider时,如下所示:
bind(new TypeLiteral<LockProvider<Lock>>() {}).to(DynamoDBLockProvider.class);
I get the following compilation error in Eclipse: 我在Eclipse中收到以下编译错误:
The method to(Class<? extends LockProvider<Lock>>) in the type LinkedBindingBuilder<LockProvider<Lock>> is not applicable for the arguments (Class<DynamoDBLockProvider>)
I understand that DynamoDBLockProvider<DynamoDBLock> is not a subclass of LockProvider<Lock>. 我了解DynamoDBLockProvider <DynamoDBLock>不是LockProvider <Lock>的子类。 Is it possible to accomplish what I am trying to do, ie bind LockProvider to DynamoDBLockProvider (in a clean and efficient way)?
是否有可能完成我想做的事情,即将LockProvider绑定到DynamoDBLockProvider(以一种干净有效的方式)?
Your instances of DynamoDBLockProvider
are not instances of LockProvider<Lock>
. 您的
DynamoDBLockProvider
实例不是 LockProvider<Lock>
实例。 But they are instances of LockProvider<? extends Lock>
但是它们是
LockProvider<? extends Lock>
实例LockProvider<? extends Lock>
LockProvider<? extends Lock>
. LockProvider<? extends Lock>
。
A parallel example would be instances of ArrayList<Integer>
are not instances of List<Number>
but are instances of List<? extends Number>
一个平行的例子是
ArrayList<Integer>
的实例不是 List<Number>
的实例,而是List<? extends Number>
实例List<? extends Number>
List<? extends Number>
as per this question . 根据此问题
List<? extends Number>
。
Apply that to Guice and you will get exactly the same compilation fail. 将其应用于Guice,您将获得完全相同的编译失败。 Assume you have a class like this:
假设您有一个像这样的课程:
public class IntegerList extends ArrayList<Integer> {}
Then binding it like this won't work: 然后像这样绑定它是行不通的:
binder.bind(new TypeLiteral<List<Number>>() {}).to(IntegerList.class); //won't compile
Is it possible to accomplish what I am trying to do, ie bind LockProvider to DynamoDBLockProvider (in a clean and efficient way)?
是否有可能完成我想做的事情,即将LockProvider绑定到DynamoDBLockProvider(以一种干净有效的方式)?
The following binding will work: 以下绑定将起作用:
binder.bind(new TypeLiteral<LockProvider<? extends Lock>>() {}).to(DynamoDBLockProvider.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.