在匕首1.x的单身 [英] Singletons in Dagger 1.x

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

问题描述

在使用匕首,我发现,我得到一个单身的多个实例时,我把它注射的地方,我需要它。我已经诠释了类和提供方法 @Singleton 。谁能想到为什么会这样?

编辑:

如果有帮助,我都遵循相同的结构,我的应用程序在匕首的GitHub上的示例应用程序(<一href="https://github.com/square/dagger/tree/master/examples/android-activity-graphs">https://github.com/square/dagger/tree/master/examples/android-activity-graphs).我试图让辛格尔顿在基本活动和一对夫妇使用提供第三方类 @Provides 在自定义的应用程序类。是不是因为我在每个活动到原来的对象图加荷兰国际集团的模块?

(PS:我是新来的匕首和DI一般,所以我会很感激,如果你能提供一个解释,这样我可以学习谢谢)

解决方案

@Singleton ,在匕首1.x中,行为不同于你的想象。在 @Singleton Javadoc中的JSR-330规范的定义是每一个图形,这是怎样的匕首间$ P $点吧。

所以,如果你有一些被标记为 @Singleton ,它物化在应用程序图(而不是更短的寿命图),然后你每个应用程序的一个实例。

如果你有一个项目注释 @Singleton 这是在你用它来配置活动图(即,从图中由指定的部分获得的模块在加号()操作使用的模块),那么你会得到一个每活性图。

如果你需要的东西是一次每个应用程序,你需要确保它被创建为应用程序图的一部分。您可以通过以下两种方法之一做到这一点。无论是从您的应用程序模块(S),明确规定将其与@Provides方法,也可以将其列为在@Module在应用程序模块中的一个类(注入= ...)。

(如果你不使用 @Singleton 标记它比你会得到每注射部位之一。)

所以记住,通过加()创建的被视为一个指向它从中衍生出图中的单独的图形的图形,并把它封装,可以在它访问的实例,但是不一样的曲线图。

注 - 匕首2.X提高了这一点,并支持自定义作用域的注解,虽然该机制是相似的,每个范围标注一个图(部分),与更广泛/窄寿命曲线之间的父/子关系

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?

Edit:

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 : 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, 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.

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.

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.

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.

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

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.

Note - Dagger 2.x improves this, and supports custom scoping annotations, though the mechanism is similar, with one graph (component) per scope annotation, with a parent/child relationship between graphs of wider/narrower lifetimes

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

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