Guice配置错误:找不到合适的构造函数

时间:2018-04-12 05:43:22

标签: java guice

我有课程,我试图通过Guice实例化,但我得到了:

Could not find a suitable constructor in com.workspace.myclass. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.

我的代码类似于:

public class MyModule extends AbstractModule {

    @Override
    protected void configure() { }

    myClass<obj> getMyClass(arg1, arg2) {
        Object someObj = new someObj(arg1);
        return myClass<>(someObj, s -> new Obj(arg2, s.getMethod()));
    }
}

public myClass<T extends someClass> {

    public myClass(arg1, Function<obj<T>, obj2<T>> arg2) {

    }

}

public static void main(args[]) {

    Injector injector = createInjector(new MyModule());
    MyClass myClass = injector.getInstance(MyClass.class);
}

Guice无法将lambda解析为MyClass的构造函数吗?

2 个答案:

答案 0 :(得分:0)

Guice不喜欢我在模块中给出了来自getMyClass的返回类型myClass<obj>,只需返回myClass

答案 1 :(得分:0)

我相信你错过了getMyClass方法中的@Provides注释 您是否尝试使用configure方法中的binder进行绑定?