Guice: “Could not find suitable constructor” when suitable constructor supplied in FactoryBuilder

匿名 (未验证) 提交于 2019-12-03 00:59:01

问题:

I'm using Guice for dependency injection, but I'm getting this error:

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) 

What's especially odd is that it mentions java.lang.Void.

This is the module code: (I bind a class to itself, not an interface to a class)

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

Here is ItemSourceTreeWrapper constructor:

@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); } 

And here is the factory interface:

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

回答1:

Your call to implement() looks suspicious - since ItemSourceTreeWrapper is generic, the second parameter should likely be using a TypeLiteral<>. Furthermore, since you're implementing a class with itself, you should be able to omit the implement() call entirely.

But your actual problem is that in ItemSourceTreeWrapperFactory, public void create(...) should probably be public ItemSourceTreeWrapper<Source> create(...).



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!