Guice:具有属性的绑定注释 [英] Guice:Binding Annotations with Attributes

查看:83
本文介绍了Guice:具有属性的绑定注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我正在阅读Guice的官方文档,但是我对绑定注释这一章有一些疑问.

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支持具有属性值的绑定注释.在里面 罕见的情况下,您需要这样的注释:

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

创建注释@interface.创建一个实现 注释界面.遵循equals()和 注释Javadoc中指定的hashCode().传递一个实例 到annotatedWith()绑定子句中.

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.

我不明白那个解释.解释的目的是什么?我学会了两个注释,例如@Paypal(在本文档中)和@name.但是,当我想对同一个类使用两个以上的依赖时,也许不能仅用这两个注释实现? 现在我很困惑,有人可以解释吗?

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?

推荐答案

Guice通过使用 @BindingAnnotation @Qualifier )和类型(如果需要,则带有参数).这些都是有效的密钥,彼此之间是不同的:

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>
  • 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
  • @Named("foo") YourClassOne
  • @Named("bar") YourClassOne
  • @AnotherBindingAnnotation(foo=3, bar=5) YourClassOne
  • @AnotherBindingAnnotation(foo=6, bar=1) YourClassOne

它们互不相同,都是提供给Guice并从Guice注入的有效物品.

They're all different from one another, and they're all valid things to provide to Guice and inject from Guice.

通常,您可能不需要创建具有属性的绑定注释:绑定注释最初并不常见,在大多数情况下,您可以使用它们来处理空的(无属性)绑定注释或使用内置的@Named注释(及其对应的

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

这篇关于Guice:具有属性的绑定注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆