3

我已经使用 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;
}
4

2 回答 2

2

由于某些未知原因,<T>从工厂接口中删除通用部分解决了该问题。我以前在泛型和 Guice 方面遇到过问题,所以也许这只是类型擦除导致的另一种怪癖。

所以工厂界面现在是:

public interface OptimalClusterEstimatorFactory {
    public OptimalClusterEstimator createFCA(int kValue, ItemPointReducer pointReducer);
}
于 2014-03-22T00:05:42.540 回答
2

Guice 辅助注入不适用于泛型。如果您想使用泛型,那么您已经编写了自己的工厂。

于 2014-03-29T02:26:21.857 回答