我可以只使用时dagger2依赖注入注入超一流? [英] Can I just inject super class when use dagger2 for dependency injection?
问题描述
我用Dagger2为DI在我的Android应用程序。我发现,我必须写一个使用@Inject场每类注射方法。有没有办法,我只能注入父类,这样我就不用调用注入在每个子类中的方法吗?
以活动为例子。我有一个 BaseActivity
的每一个活动的延伸。有没有办法,我只能为BaseActivity组件创建注射方法,只是调用注入的BaseActivity的onCreate,并在子活动@Inject领域得到自动注入的方式?
它不能马上完成。 由格雷戈里踢说明:
这里下面是如何成员注射方法的工作:
- 您可以做一个成员注射方法的任何类型的具有
@Inject
的任何地方在它的类层次结构。如果不是这样,你会得到一个 错误。- 在整个类型层次结构全部
@Inject
ED成员将被注入:参数类型和所有超类型- 在没有成员将
@Inject
ED的参数类型的子类型。
这个问题进行了讨论和的此处,跟进这些更新。但它不太可能很快改变,原因匕首2是接近释放。
I use Dagger2 for DI in my android application. I found that I have to write inject method for every class that uses @Inject field. Is there a way that I can just inject the parent class so that I don't have to call inject on every subclass?
Take Activity for example. I have a BaseActivity
that that every Activity extends from. Is there a way that I can just create an inject method in the component for BaseActivity and just call inject in BaseActivity's onCreate, and @inject fields in sub activities get injected automatically?
It cannot be done right now. Explanation by Gregory Kick:
Here's how members injection methods work:
- You can make a members injection method for any type that has
@Inject
anywhere in its class hierarchy. If it doesn't, you'll get an error.- All
@Inject
ed members in the entire type hierarchy will be injected: the argument type and all supertypes.- No members will be
@Inject
ed for subtypes of the argument type.
This issue was discussed here and here, follow up these for updates. But it is unlikely to change soon, cause Dagger 2 is close to release.
这篇关于我可以只使用时dagger2依赖注入注入超一流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!