【发布时间】:2017-11-04 14:16:34
【问题描述】:
我是 Guice 的新手,正在为以下用例寻求帮助:
我开发了一个包(PCKG),其中该包的入口类依赖于其他类,例如:
A : Entry point class --> @Inject A(B b) {}
B in turn is dependent on C and D like --> @Inject B(C c, D d) {}
在我正在做的绑定模块中:
bind(BInterface).to(Bimpl);
bind(CInterface).to(CImpl);
...
注意我没有为 A 提供绑定信息,因为我想通过它的消费者类来提供它的绑定。 (这就是设计的方式,所以我的要求是继续讨论主要问题而不是设计)。
现在我的消费者类正在做这样的事情:
AModule extends PrivateModule {
protected void configure() {
bind(AInterface.class).annotatedWith(AImpl.class);
}
}
也在我的消费包中:
.(new PCKGModule(), new AModule())
第一季度。我在消费类中正确地进行绑定吗?我很困惑,因为当我在我的消费者包中进行如下内部测试时:
class testModule {
bind(BInterface).to(Bimpl);
bind(CInterface).to(CImpl)...
}
class TestApp {
public static void main(..) {
Guice.createInstance(new testModule());
Injector inj = Guice.createInstance(new AModule());
A obj = inj.getInstance(A.class);
}
}
它正在抛出 Guice 创建异常。请帮助我摆脱这种情况。 我的一位对 Guice 也很幼稚的朋友也建议我需要使用提供注释在 AModule 中创建 B 的实例。但我真的没明白他的意思。
【问题讨论】:
标签: dependency-injection guice