即使绑定,原始对象的Guice注入也无法通过提供程序进行提供

时间:2018-09-18 23:01:49

标签: 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() {
    bind(Integer.class).annotatedWith(Names.named("size")).toInstance(1000);
}

@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

哪里出问题了? (不是this的副本,因为我不是通过提供程序将变量直接注入到类中)

0 个答案:

没有答案