[英]Guice: “Could not find suitable constructor” when suitable constructor supplied in FactoryBuilder
我正在使用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);
}
你對implement()
調用看起來很可疑 - 因為ItemSourceTreeWrapper
是通用的,第二個參數應該可能是使用TypeLiteral<>
。 此外,由於您自己實現了一個類,因此您應該能夠完全省略implement()
調用。
但是你的實際問題是在ItemSourceTreeWrapperFactory
, public void create(...)
應該是public ItemSourceTreeWrapper<Source> create(...)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.