Guice将根据您在Module
中的配置方式注入B
。您不需要添加@Inject
以外的其他任何东西(您已拥有)。这里有你的类更完整的示例建筑:
public class GuiceExample {
static class N {
private final A a;
private B b;
@Inject
N(@Assisted final A a, final B b) {
this.a = a;
this.b = b;
}
}
static class A {}
static class B {}
static interface NFactory {
public N create(A a);
}
static class Module extends AbstractModule {
@Override
protected void configure() {
install(new FactoryModuleBuilder().implement(A.class, A.class).build(NFactory.class));
bind(B.class); // Or however you want B to be bound...
}
}
@Test
public void test() {
Injector i = Guice.createInjector(new Module());
N n = i.getInstance(NFactory.class).create(new A());
}
}
你应该在你configure
方法绑定B
你认为合适的。并且您将NFactory
注入您需要从A
s生产N
s的课程中。