我有一个看起来像的课程
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
函数的绑定?