Java Guice - how to bind integer value to my class/object?

I don't understand how can I simply bind any value when I bind a class. I always get this error:

No implementation for test.Triangle annotated with @com.google.inject.name.Named(value=triangle) was bound.

I tried this:

Triangle class

@Inject
public void setLength(@Named("triangle") int length) {
    this.length = length;
}

Configuration class

bind(Triangle.class).annotatedWith(Names.named("triangle")).toInstance(1); //this one just gives error that I can't do that.

How can I enter a value into it, so it would use setLength method with my chosen value?.. I read Guice documentation, but didn't find it. In Spring framework such things like these seemed much easier to do and understand (maybe it has better documentation, at least for me). Also if I missed in documentation where it shows this kind of thing you can link it too.

Answers


bind(Integer.class).annotatedWith(Names.named("triangle")).toInstance(1);

You don't want to bind Triangle to 1 - you want to bind Integer to 1. It might be better to use @Named("triangleLength") or even switch to a binding annotation (@IndicatesTriangleLength) to make the intention clear.


Need Your Help

ASP.NET MVC 2 - How do I pass a querystring back on response?

c# asp.net-mvc

I need to pass a querystring back on response - so that via means of jQuery I can do something with it.