我可以只使用时dagger2依赖注入注入超一流?超一流

2023-09-06 02:54:00 作者:沉默若是金那我当哑巴算了

我用Dagger2为DI在我的Andr​​oid应用程序。我发现,我必须写一个使用@Inject场每类注射方法。有没有办法,我只能注入父类,这样我就不用调用注入在每个子类中的方法吗? 以活动为例子。我有一个 BaseActivity 的每一个活动的延伸。有没有办法,我只能为BaseActivity组件创建注射方法,只是调用注入的BaseActivity的onCreate,并在子活动@Inject领域得到自动注入的方式?

解决方案

它不能马上完成。 由格雷戈里踢说明:

  

下面是如何成员注射方法的工作:

        您可以做一个成员注射方法的任何类型的具有 @Inject 的任何地方在它的类层次结构。如果不是这样,你会得到一个   错误。   在整个类型层次结构全部 @Inject ED成员将被注入:参数类型和所有超类型   在没有成员将 @Inject ED的参数类型的子类型。    这里

这个问题进行了讨论和的此处,跟进这些更新。但它不太可能很快改变,原因匕首2是接近释放。

飞行爱好者必看的10部电影

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 @Injected members in the entire type hierarchy will be injected: the argument type and all supertypes. No members will be @Injected 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.

相关推荐
 
精彩推荐
图片推荐