Deletions are marked like this. | Additions are marked like this. |
Line 28: | Line 28: |
* @Target({FIELD, PARAMETER, METHOD}) is a courtesy to your users. It prevents @PayPal from being accidentally being applied where it serves no purpose. * @Retention(RUNTIME) makes the annotation available at runtime. |
* @Target({FIELD, PARAMETER, METHOD})はユーザ(訳注:この場合はプログラマのこと)に対する注意である。@Paypalが意味の無い使われ方をするのを避けるため。 * @Retention(RUNTIME)は実行時にアノテーションを利用できるようにするためのものである(訳注:必ずこれが必要) |
Line 31: | Line 31: |
To depend on the annotated binding, apply the annotation to the injected parameter: | アノテーションバインディングに依存させるには、注入されるパラメータにアノテーションを適用する。 |
Line 41: | Line 41: |
Lastly we create a binding that uses the annotation. This uses the optional annotatedWith clause in the bind() statement: |
最後にアノテーションを使ったバインディングを作成する。 annotatedWith句を使う。 |
Line 47: | Line 48: |
Line 49: | Line 51: |
Guice comes with a built-in binding annotation @Named that uses a string: | Guiceにはビルトインのバインディングアノテーションの@Namedが用意されている。 |
Line 59: | Line 61: |
To bind a specific name, use Names.named() to create an instance to pass to annotatedWith: | 特定の名前をバインドするには、Names.named()メソッドで得られたオブジェクトをannotatedWithに渡せばよい。 |
Line 71: | Line 73: |
1. Create the annotation @interface. 1. 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. |
1. アノテーション@interfaceを作成する。 1. アノテーションインタフェースを実装するクラスを作成する。AnnotationのJavaDocに記述されているequals()とhashCode()のガイドラインに従うこと。そのオブジェクトをannotatedWith()に引き渡すこと。 訳注:どのように使うのか不明 |
バインディングアノテーション
同じ型について、複数のバインディングを指定したいこともある。 例えば、PayPal credit card processor と Google Checkout processorを使いたいとしよう。 これができるように、バインディングはバインディングアノテーションのオプションを用意している。 アノテーションとタイプの組はバインディングを一意に識別する。 この組をキーと呼ぶ。
バインディングアノテーションの定義には、二行のコードと複数のimportが必要である。 これらをそれ自身の.javaファイルに入れるか、アノテートしたいタイプの中で定義する。
package example.pizza; import com.google.inject.BindingAnnotation; import java.lang.annotation.Target; import java.lang.annotation.Retention; import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.ElementType.PARAMETER; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.METHOD; @BindingAnnotation @Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME) public @interface PayPal {}
これらのメタアノテーションについて理解する必要は無いが、以下に説明する。
@BindingAnnotation はGuiceにバインディングアノテーションであることを伝える。Guiceは一つのメンバーに複数のバインディングアノテーションが指定されている場合はエラーを発生する。
- @Target({FIELD, PARAMETER, METHOD})はユーザ(訳注:この場合はプログラマのこと)に対する注意である。@Paypalが意味の無い使われ方をするのを避けるため。
- @Retention(RUNTIME)は実行時にアノテーションを利用できるようにするためのものである(訳注:必ずこれが必要)
アノテーションバインディングに依存させるには、注入されるパラメータにアノテーションを適用する。
public class RealBillingService implements BillingService {
@Inject public RealBillingService(@PayPal CreditCardProcessor processor, TransactionLog transactionLog) { ... }
最後にアノテーションを使ったバインディングを作成する。 annotatedWith句を使う。
bind(CreditCardProcessor.class) .annotatedWith(PayPal.class) .to(PayPalCreditCardProcessor.class);
@Named
Guiceにはビルトインのバインディングアノテーションの@Namedが用意されている。
public class RealBillingService implements BillingService { @Inject public RealBillingService(@Named("Checkout") CreditCardProcessor processor, TransactionLog transactionLog) { ... }
特定の名前をバインドするには、Names.named()メソッドで得られたオブジェクトをannotatedWithに渡せばよい。
bind(CreditCardProcessor.class) .annotatedWith(Names.named("Checkout")) .to(CheckoutCreditCardProcessor.class);
Since the compiler can't check the string, we recommend using @Named sparingly.
属性付バインディングアノテーション
Guiceは属性値付バインディングアノテーションをサポートしている。まれなケースであるが、このようなアノテーションが必要になるかもしれない。
- アノテーション@interfaceを作成する。
アノテーションインタフェースを実装するクラスを作成する。AnnotationのJavaDocに記述されているequals()とhashCode()のガイドラインに従うこと。そのオブジェクトをannotatedWith()に引き渡すこと。
訳注:どのように使うのか不明