Guice figures out what you want to inject by using a Key
, which is just a name for the combination of a binding annotation (an annotation that itself is annotated with @BindingAnnotation
or @Qualifier
) and a type (with parameters if needed). These are all valid keys, distinct from one another:
YourClassOne
YourClassTwo
List<Integer>
List<String>
@Paypal YourClassOne
@Paypal YourClassTwo
@YourBindingAnnotation YourClassOne
@YourBindingAnnotation List<String>
However, annotations are allowed to have attributes, like with @Named("your name here")
. This means that keys differ not only on which binding annotation you have, but what its attributes are. Let's add some keys using annotations with attributes to the list above:
@Named("foo") YourClassOne
@Named("bar") YourClassOne
@AnotherBindingAnnotation(foo=3, bar=5) YourClassOne
@AnotherBindingAnnotation(foo=6, bar=1) YourClassOne
They're all different from one another, and they're all valid things to provide to Guice and inject from Guice.
In general, you probably don't need to create your own binding annotations with attributes: Binding annotations are not that common in the first place, and most cases where you want them can be handled with empty (no-attribute) binding annotations or use of the built-in @Named
annotation (along with its counterpart, Names.named
, which helps you create a compatible instance of your annotation that you can use in your AbstractModule). However, if you DO want to create your own binding annotation with attributes, you can use the part of the docs you quoted in order to create that, particularly in conforming to the Annotation.equals and Annotation.hashCode requirements. (If this is something you expect to do a lot, consider using a library like Apache Commons AnnotationUtils or a code generator like Google Auto's AutoAnnotation.)