Kazuya Tomita
Kazuya Tomita

Reputation: 697

Guice:Binding Annotations with Attributes

Now I am reading Guice's official document, but I have some questions related to the chapter of Binding Annotation.

This explains "Annotation with Attributes". But, I'm not sure of the explanation.

Binding Annotations with Attributes

Guice supports binding annotations that have attribute values. In the rare case that you need such an annotation:

Create the annotation @interface. Create a class that implements the annotation interface. Follow the guidelines for equals() and hashCode() specified in the Annotation Javadoc. Pass an instance of this to the annotatedWith() binding clause.

I don't understand that explanation. For what is the explanation intended? I learned two annotations such as @Paypal(in this document) and @name. But perhaps we can't achieve only with these two annotations when I want to use some dependencies more than two toward a same class? Now I am confused, could anyone explain?

Upvotes: 3

Views: 5367

Answers (1)

Jeff Bowman
Jeff Bowman

Reputation: 95654

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.)

Upvotes: 6

Related Questions