Dagger 2: How to inject Map<Class<? extends Foo>, Provider<? extends Foo>>

孤者浪人 提交于 2019-12-05 14:14:13

Answering my own question in accordance to the guidelines.


First, you have to get rid of the wildcard in Provider<? extends Foo>.

Second, you need to declare an annotation for the map key:

@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@MapKey
public @interface FooKey {
    Class<? extends Foo> value();
}

Then, for each implementation of Foo you need to declare in your Module:

@Binds @IntoMap @FooKey(Bar.class)
abstract Foo bindBar(Bar bar)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!