How can I bind a factory to a annotation-qualified

2019-08-12 06:33发布

问题:

I asked and got an answer how to bind a named injection point.

And I don't know how to bind a factory to a qualified injection point.

class SomeResource {

    @Inject
    @Some // is a @Qualifier, of course.
    private MyType qualified;
}

I prepared a factory

class SomeFactory extends Factory<MyType> {
}

And I stuck on creating a binder for that

class SomeBinder extends AbstractBinder {
    @Override protected void configure() {
        // @@?
    }
}

I actually want to know how to use ServiceBindingBuilder#qualifiedBy.

回答1:

I need an implementation for the qualifier annotation.

public class Some_
    extends AnnotationLiteral<Some>
    implements Some {
}

So that I can use like this.

bindFactory(Someactory.class)
    .qualifiedBy(new Some_())
    .to(MyType.class);

I really don't understand why ServiceBindingBuilder doesn't have a method taking an annotation class not an instance.