Guice:"找不到合适的构造函数"当在FactoryBuilder中提供合适的构造函数时

时间:2014-05-15 14:17:29

标签: java dependency-injection guice factory

我正在使用Guice进行依赖注入,但是我收到了这个错误:

1) Could not find a suitable constructor in java.lang.Void. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
  at java.lang.Void.class(Void.java:44)
  while locating java.lang.Void
  at com.myfeed.algorithm.clusterer.tree.wrapper.ItemSourceTreeWrapperFactory.create(ItemSourceTreeWrapperFactory.java:1)

特别奇怪的是它提到了java.lang.Void。

这是模块代码 :(我将类绑定到自身,而不是类的接口)

install(new FactoryModuleBuilder().implement(
     new TypeLiteral<ItemSourceTreeWrapper<Document>>(){}, ItemSourceTreeWrapper.class
).build(new TypeLiteral<ItemSourceTreeWrapperFactory<Document>>(){}));

这是 ItemSourceTreeWrapper构造函数

@Inject
public ItemSourceTreeWrapper(@Assisted Tree<Item<Source>> treeToWrap, @Assisted ItemGroup itemGroup, Itemizer<Source> itemizer, SourceItemConverterFactory sourceItemConverterFactory) {
    this.treeToWrap = treeToWrap;
    this.itemizer = itemizer;
    sourceItemConverter = sourceItemConverterFactory.create(itemGroup, itemizer);
}

这是工厂界面

public interface ItemSourceTreeWrapperFactory<Source> {
    public void create(Tree<Item<Source>> treeToWrap, ItemGroup<Source> itemGroup);
}

1 个答案:

答案 0 :(得分:4)

您对implement()的电话看起来很可疑 - 由于ItemSourceTreeWrapper是通用的,因此第二个参数可能使用TypeLiteral<>。此外,由于您要自己实现一个类,因此您应该能够完全省略implement()调用。

但您的实际问题是ItemSourceTreeWrapperFactorypublic void create(...)应该是public ItemSourceTreeWrapper<Source> create(...)