我不明白如何在绑定(bind)类时简单地绑定(bind)任何值。我总是收到此错误:
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);//这个只是给出错误,我不能这样做。
我如何在其中输入一个值,以便它使用 setLength 方法和我选择的值?..我阅读了 Guice 文档,但没有找到它。在 Spring 框架中,这样的事情似乎更容易做和理解(也许它有更好的文档,至少对我来说)。另外,如果我错过了文档中显示此类内容的内容,您也可以链接它。
最佳答案
bind(Integer.class).annotatedWith(Names.named("triangle")).toInstance(1);
您不想将 Triangle
绑定(bind)到 1 - 您想将 Integer
绑定(bind)到 1。最好使用 @Named("triangleLength ")
甚至切换到绑定(bind)注释 (@IndicatesTriangleLength
) 以明确意图。
关于Java Guice - 如何将整数值绑定(bind)到我的类/对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20731500/