吉斯:“找不到合适的构造函数”;当FactoryBuilder中提供了合适的构造函数时 [英] Guice: "Could not find suitable constructor" when suitable constructor supplied in FactoryBuilder

查看:122
本文介绍了吉斯:“找不到合适的构造函数”;当FactoryBuilder中提供了合适的构造函数时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Guice进行依赖项注入,但出现此错误:

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)

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

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>>(){}));

这里是 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);
}


推荐答案

您对<$ c的呼叫$ c> implement()看起来可疑-由于 ItemSourceTreeWrapper 是通用的,因此第二个参数可能应该使用 TypeLiteral< > 。此外,由于您正在使用自己实现一个类,因此应该可以完全省略 implement()调用。

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.

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

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

这篇关于吉斯:“找不到合适的构造函数”;当FactoryBuilder中提供了合适的构造函数时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆