Guice绑定动态原语引发错误

时间:2018-09-18 22:20:55

标签: java dependency-injection guice

我有一个看起来像的课程

class MyClass {
    private final int size;

    @Inject
    public MyClass(final int size) {
        this.a = a;
    }
}

我提供MyClass实例的模块类看起来像

public class MyClassModule extends AbstractModule {

@Override
protected void configure() {

}

@Provides
@Named("size")
public int providesSize() {
    return 1000; //will be dynamic later
}

@Provides
@Singleton
public MyClass providesMyClass(@Named("size") int size) {
    return new MyClass(size)
}

当我的应用程序类使用MyClass对象时,我看到此错误-

com.google.inject.ConfigurationException: Guice configuration errors: 
| 1) Could not find a suitable constructor in java.lang.Integer. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.   
|   at java.lang.Integer.class(Integer.java:52) |   while locating java.lang.Integer|     for parameter 0 at myPackage.MyClass.<init>

哪里出问题了?即使注入的值来自bind()Provides,我是否需要在此处提供带有Named函数的绑定?

0 个答案:

没有答案