0

次のように、特定の注釈を使用して Guice マルチバインドを実行できることを知っています。

Multibinder.newSetBinder(binder(), Bound.class, Annotation.class);

Annotation.classしかし、アノテーションが付けられているだけでなく、特定の値を持つクラスに対して、より具体的なマルチバインドを行うことはでき@Annotation("type1")ますか?

4

1 に答える 1

3

この場合、注釈を実装し、そのインスタンスを Multibinder 静的ファクトリ メソッドに渡すことができます。

  static class YourAnnotationImpl implements YourAnnotation {

    private final String value;


    YourAnnotationImpl(String value) {
      this.value = value;
    }

    @Override public String value() {
      return value;
    }

    @Override public Class<? extends Annotation> annotationType() {
      return YourAnnotation.class;
    }

    @Override public String toString() {
      return "@" + YourAnnotation.class.getName() + "(value=" + value + ")";
    }

    @Override public boolean equals(Object o) {
      return o instanceof YourAnnotationImpl
          && ((YourAnnotationImpl) o).value().equals(value());
    }

    @Override public int hashCode() {
      return (127 * "value".hashCode()) ^ value.hashCode();
    }

  }
   ...

   Multibinder.newSetBinder(binder(), Bound.class, new YourAnnotationImpl("type1");
于 2013-07-10T21:15:11.817 に答える