Dagger 1.x 中的单身人士 [英] Singletons in Dagger 1.x

查看:25
本文介绍了Dagger 1.x 中的单身人士的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 Dagger 时,我发现当我在需要的地方注入它时,我会得到多个单例实例.我已经用 @Singleton 注释了类和提供方法.谁能想到为什么会这样?

When using Dagger, I found that I'm getting multiple instances of a singleton when I inject it wherever I need it. I've annotated the class and the provides method with @Singleton. Can anyone think of why this is happening?

如果有帮助,我的应用程序采用与 Dagger GitHub 中的示例应用程序相同的结构 (https://github.com/square/dagger/tree/master/examples/android-activity-graphs).我试图在基本活动中获取单例,以及在自定义 Application 类中使用 @Provides 提供的几个第三方类.是不是因为我在每个活动中都将模块添加到原始对象图中?

If it helps, I have followed the same structure for my app as the sample application in Dagger's GitHub (https://github.com/square/dagger/tree/master/examples/android-activity-graphs). I'm trying to get the Singleton in the base activity and a couple of third party classes provided using @Provides at the custom Application class. Is it because I'm plus-ing modules at each activity to the original object graph?

(PS:我是 Dagger 和 DI 的新手,所以如果你能提供解释以便我学习,我将不胜感激.谢谢.)

(PS : I'm new to Dagger and DI in general, so I'll be grateful if you could provide an explanation so that I may learn. Thanks.)

推荐答案

@Singleton 在 Dagger 1.x 中,其行为与您想象的不同.@Singleton javadoc 中的 JSR-330 规范定义是每个图一个",这就是 Dagger 解释它的方式.

@Singleton, in Dagger 1.x, acts differently than you might think. The JSR-330 spec definition in the @Singleton javadoc is "one per graph" and that is how Dagger interprets it.

因此,如果您有一些标记为 @Singleton 的内容,并且它在您的应用程序图中具体化(而不是生命周期较短的图),那么您将获得每个应用程序一个实例.

So if you have something that is marked as @Singleton, and it is materialized in your application graph (as opposed to a shorter-lifetime graph), then you get one instance per application.

如果您在用于配置活动图的模块中有一个注释 @Singleton 的项目(即,从 plus() 操作) 那么您将获得一个每个活动图.

If you have an item annotated @Singleton that's in the modules you use to configure your activity graph (i.e., that is obtained from the part of a graph specified by a module used in the plus() operation) then you will get one-per-activity-graph.

如果您需要每个应用程序一次,您需要确保将其创建为应用程序图的一部分.您可以通过以下两种方式之一执行此操作.要么从您的应用程序模块中明确地为它提供一个@Provides 方法,要么您可以将它列为应用程序模块中@Module(injects=...) 中的类之一.

If you need something to be once-per-application, you need to make sure it gets created as a part of the application graph. You can do this in one of two ways. Either explicitly provide it with an @Provides method from your application module(s), or you can list it as a one of the classes in @Module(injects=...) in an application module.

(如果您没有用 @Singleton 标记它,那么每个注射部位都会得到一个.)

(If you do not mark it with @Singleton than you will get one per injection site.)

所以请记住,由 plus() 创建的图被视为一个单独的图,它指向从中产生它的图,并将其包装起来,可以访问其中的实例,但不是同一个图.

So remember, the graph created by plus() is seen as a separate graph that points to the graph from which it was spawned, and wraps it, can access instances within it, but is not the same graph.

注意 - Dagger 2.x 改进了这一点,并支持自定义范围注释,尽管机制类似,每个范围注释一个图(组件),在更宽/更窄生命周期的图之间具有父/子关系

这篇关于Dagger 1.x 中的单身人士的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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