Singletons in Dagger 1.x

余生长醉 提交于 2019-11-28 23:16:14

@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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!