Java Guice – 如何将整数值绑定到我的类/对象?
我不明白如何在绑定类时简单地绑定任何值。 我总是得到这个错误:
No implementation for test.Triangle annotated with @com.google.inject.name.Named(value=triangle) was bound.
我试过这个:
三角类
@Inject public void setLength(@Named("triangle") int length) { this.length = length; }
配置类
bind(Triangle.class).annotatedWith(Names.named("triangle")).toInstance(1); //this one just gives error that I can't do that.
如何在其中输入值,因此它将使用setLength方法和我选择的值?..我读了Guice文档,但没有找到它。 在Spring框架中,像这样的东西似乎更容易做和理解(也许它有更好的文档,至少对我来说)。 此外,如果我错过了文档,它显示了这种事情,你也可以链接它。
bind(Integer.class).annotatedWith(Names.named("triangle")).toInstance(1);
您不希望将Triangle
绑定到1 – 您希望将Integer
绑定到1.最好使用@Named("triangleLength")
或甚至切换到绑定注释( @IndicatesTriangleLength
)以使意图清晰。