【问题标题】:Guice/Eclipse TypeLiteral issueGuice/Eclipse TypeLiteral 问题
【发布时间】:2017-09-02 02:36:54
【问题描述】:

这是我遇到的问题。我有一个界面:

public interface MyInterface <AMessageTuple>{

public ConnectableObservable<AMessageTuple> getTokenConnectableObservable();
}

我有一个希望成为单身人士的实现:

public class MyImpl implements MyInterface<ImmutablePair<String, Message>> {

...
//irrelevant members omitted

@Override
public ConnectableObservable<ImmutablePair<String, Message>> getTokenConnectableObservable() {
    return tokenObservable;
}

...
}

当我尝试将接口绑定到扩展 Guice 的 AbstractModule 的类中的实现时:

public class MyServiceModule extends AbstractModule {

    @Override
    protected void configure() {
        TypeLiteral <MyInterface<ImmutablePair<String, Message>>> t = new TypeLiteral<MyInterface<ImmutablePair<String,Message>>>() {
        };  
        bind(t).annotatedWith(Names.named("firstOne")).to(MyImpl.class).in(Singleton.class);
    }
}

bind... ligne 带有红色下划线(作为编译错误),这是我在悬停时看到的消息:

AbstractModule类型中的方法bind(Key&lt;T&gt;)不适用 对于论点 (TypeLiteral&lt;MyInterface&lt;ImmutablePair&lt;String, Message&gt;&gt;&gt;)

奇怪的是,按 Ctr+Space 确实识别出方法 bind(TypeLiteral) 存在......所以有点困惑,希望有人能给我提示我错过了什么。

上下文:我使用 Eclipse Neon 作为我的 IDE,我使用(显然)Guice(4.0 版)作为 DI,项目的 JDK 是 oracle-7。我目前在 Guice 中绑定泛型类的方法是基于 this question。

【问题讨论】:

  • 你找到解决方案了吗?

标签: java eclipse generics guice


【解决方案1】:

确保您使用import com.google.inject.TypeLiteral;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 2011-10-07
    相关资源
    最近更新 更多