我已经使用 AssistedInject 在 Google Guice 中安装了一个工厂,但出现以下错误(我正在使用 JUnit 运行单元测试):
com.google.inject.CreationException: Guice creation errors:
1) No implementation for clusterestimator.OptimalClusterEstimatorFactory was bound.
while locating clusterestimator.OptimalClusterEstimatorFactory
for parameter 0 at com.myfeed.algorithm.clusterer.tree.fca.BasicFCATreeFactory.<init>(BasicFCATreeFactory.java:16)
FCAModule.configure(FCAModule.java:29)
即使我install(new Factory...);
从模块中省略了该行,此错误也是相同的,这使我认为该行以某种方式被忽略了。
这是模块代码:
public class FCAModule extends AbstractModule {
@Override
protected void configure() {
install(new FactoryModuleBuilder() // <-- Factory line that's not working
.implement(OptimalClusterEstimator.class, FCAOptimalClusterEstimator.class)
.build(OptimalClusterEstimatorFactory.class));
bind(ValueWell.class).to(MapBackedValueWell.class).asEagerSingleton();
bind(FCATreeFactory.class).to(BasicFCATreeFactory.class).asEagerSingleton();
bind(ItemFactory.class).to(MapBackedItemFactory.class).asEagerSingleton();
bind(ClustererFactory.class).asEagerSingleton();
bind(ClusterFactory.class).to(MemoryBackedClusterFactory.class).asEagerSingleton();
}
}
这是工厂界面:
public interface OptimalClusterEstimatorFactory {
public <T> OptimalClusterEstimator createFCA(int kValue, ItemPointReducer<T> pointReducer);
}
这是FCAOPtimalClusterEstimator 的构造函数:
@AssistedInject
public FCAOptimalClusterEstimator(@Assisted int kValue, @Assisted ItemPointReducer<T> pointReducer) {
this.kValue = kValue;
this.pointReducer = pointReducer;
}
这是BasicFCATreeFactory 的构造函数,首先要调用另一个工厂。请注意,此工厂不是使用 AssistedInject 创建的,因为它使用泛型。
@Inject
public BasicFCATreeFactory(OptimalClusterEstimatorFactory optimalClusterEstimatorFactory, ClustererFactory clustererFactory, ClusterFactory clusterFactory) {
this.optimalClusterEstimatorFactory = optimalClusterEstimatorFactory;
this.clustererFactory = clustererFactory;
this.clusterFactory = clusterFactory;
}