匕首2:@Module(includes =)有什么作用?

编程入门 行业动态 更新时间:2024-10-18 18:21:47
本文介绍了匕首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注释类.包含中的模块的重复数据删除贡献以及它们的包含中的递归贡献都对对象图有所贡献.

简而言之,是的,Module.includes是一种从包含封闭模块(示例中为CoreModule)的内容中添加对所列模块(示例中为AddonModule,VersionModule和AboutModule)的依赖项的方法.

I'm working on a project and I'm trying to make it as modular as possible.

I'm trying to use the @Module(includes = {}) annotation to achieve my goals, and it's not working too well.

I have a gradle module for all my Java code and within that each section has a module (AboutModule for the About page dependencies, for example). Then, for the whole "Core" gradle module, I have one Dagger 2 module called "CoreModule" which looks like this:

@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 =)有什么作用?

本文发布于:2023-10-31 04:30:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545011.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么   匕首   作用   includes   Module

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!