匕首2:@Module(includes =)有什么作用? [英] Dagger 2: What does @Module(includes =) do?

查看:86
本文介绍了匕首2:@Module(includes =)有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个项目,并且正在尝试使其尽可能模块化.

我正在尝试使用@Module(includes = {})注释来实现我的目标,但效果不是很好.

我有一个用于所有Java代码的gradle模块,并且在每个部分中都有一个模块(例如,About页面依赖项的AboutModule).然后,对于整个"Core" gradle模块,我有一个名为"CoreModule"的Dagger 2模块,如下所示:

@Module(includes = {
        AddonModule.class,
        VersionModule.class,
        AboutModule.class}
)
public class CoreModule {
}

我是否正确地认为,在所有应用程序代码都到达的Android gradle模块中,我应该能够将该模块包含在Component中,然后该Component然后可以@Inject从CoreModule中列出的所有那些模块中注入任何东西?

此刻,我从这些组件中收到编译错误,表明它们要注入的文件是针对不在模块中的完全不同的类询问"的.

例如,我有一个GitHubComponent,它被注入到GitHubActivity中,并且还尝试使用@Inject批注GithubService进行注入,但是编译器抛出了此类正在尝试注入另一个类的错误.在目标文件的任何地方都没有提及.我已经简化了这个尝试以清除错误,但是我看不出它出了什么问题.

我在这方面找不到任何文档.包含部分实际上有什么作用?我使用的方式正确吗,因此错误在其他地方?

解决方案

请参阅

@Module(includes = {
        AddonModule.class,
        VersionModule.class,
        AboutModule.class}
)
public class CoreModule {
}

Am I right in thinking that in the Android gradle module where all the app code goes, I should be able to include that module in a Component and that Component can then @Inject anything from all those modules listed in CoreModule?

At the moment I'm getting compile errors from those Components suggesting that the files they're injecting to are "asking" for a totally different class that isn't in the module.

For instance, I have a GitHubComponent that is injected into a GitHubActivity and also tries to inject, using the @Inject annotation, GithubService, but the compiler is throwing out errors that this class is trying to inject another class... That isn't mentioned anywhere in the target file. I've sjmplfied this to try and weed out the error but I'm not seeing where it's going wrong.

I can't find any documentation on this aspect. What does the includes part actually do? Am I using it right and therefore the error is elsewhere?

解决方案

See the docs for Module.includes:

Additional @Module-annotated classes from which this module is composed. The de-duplicated contributions of the modules in includes, and of their inclusions recursively, are all contributed to the object graph.

In short, yes, Module.includes is a way of adding a dependency on the listed modules (AddonModule, VersionModule, and AboutModule in your example) from anything that includes the enclosing Module (CoreModule in your example).

这篇关于匕首2:@Module(includes =)有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆