【发布时间】:2014-07-25 06:10:58
【问题描述】:
我有一个接口 A,它有多个实现,比如 B、C 和 D。 我正在使用 guice 辅助注入来创建这些类。类 B 和 C 在构造函数中使用相同的辅助参数(例如 b、c),而 D 具有一个不同的参数(例如 d)。 我实现这一点的方式是有一个工厂 AFactory 有两种方法: create(int b, int c) 和 createD(int d) 在我的模块中,我创建了一个 PrivateModule 来将具体类绑定到工厂。
代码如下所示:
public static PrivateModule getAFactoryModule(final String factoryBindingKey,final Class<? extends A> targetClassToCreate) {
return new PrivateModule() {
@Override
public void configure() {
install(new FactoryModuleBuilder().implement(
A.class, targetClassToCreate).build(
AFactory.class));
bind(AFactory.class).annotatedWith(Names.named(factoryBindingKey)).to(
Key.get(AFactory.class));
expose(Key.get(AFactory.class, Names.named(factoryBindingKey)));
}
};
}
我这样调用 PrivateModule:
install(getAFactoryModule("B", B.class));
install(getAFactoryModule("C", C.class));
install(getAFactoryModule("D", D.class));
但是,这给出了一个错误提示:
com.guice.test.B has @AssistedInject constructors but none of them match the parameters in method com.guice.test.AFactory.createD(). Unable to create assisted inject
com.guice.test.C has @AssistedInject constructors but none of them match the parameters in method com.guice.test.AFactory.createD(). Unable to create assisted inject
com.guice.test.D has @AssistedInject constructors but none of them match the parameters in method com.guice.test.AFactory.create(). Unable to create assisted inject
Guice 似乎正在尝试使用与预期不同的创建方法。知道如何解决这个问题吗?任何指针将不胜感激!
谢谢!
【问题讨论】:
标签: java dependency-injection guice