Dagger 2-如何创建/提供EagerSingleton [英] Dagger 2 - how to create/provide a EagerSingleton

查看:103
本文介绍了Dagger 2-如何创建/提供EagerSingleton的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Dagger 2依赖项注入框架时遇到麻烦。我想创建一个 EagerSingleton 。我假设使用 @Singleton 批注时,匕首2将创建延迟加载的单例。如何使用Dagger 2框架创建 EagerSingleton

I am having trouble with the Dagger 2 dependency injection framework. I would like to create an EagerSingleton. I assume that dagger 2 creates lazy loaded singletons when I use the @Singleton annotation. How do I create EagerSingleton using the Dagger 2 framework ?

推荐答案

我知道如何执行此操作:

I figured out how to do this:

根据 docs dagger所做的操作不直接支持eagerSingletons,所以:

as per the docs dagger does not support eagerSingletons directly so:

通过创建一个EagerSingletons类来解决此问题,该类为每个渴望的单例声明静态字段。
方法注入。

因此,我在任何想使用渴望的单例的模块中都创建了EagerSingletons类。在那个eagerSingletons类中,我将声明要使用的任何单例。就是这样,匕首然后使单身者渴望。

So i created a EagerSingletons class in any module i want to use a eager singleton. In that eagerSingletons class i would declare any singleton i want to use. Thats it, dagger then makes the singletons eager.

更新:之所以以匕首1为例,是因为它是在匕首2中完成的。确实没有eagerSingletons的机制。您必须使用静态字段。让我举一个例子,说明如何创建一个eagerSingleton:

UPDATE: The reason i used dagger 1 as an example, is this is how its done in dagger 2. there really is no mechanism for eagerSingletons. you have to make it using a static field. Let me give you an example of how you might create a eagerSingleton:

In every module you need a eager singleton you could have this:
    //assume this file is called myModule.java



static EagerObjects eagerObjects;

    public static void initEagerObjects() {
        if (eagerObjects == null) {
            eagerObjects = new EagerObjects();
        }
    }
//so far so good, only one object will be created, lets inject what we need immediately
    public static class EagerObjects {

        public EagerObjects() {
          //inject right away,you'll have to figure out a way to pass the graph in. by constructor param should be fine
            getGraph().inject(this);
        }

        //make this injection static
        @Inject
        static CoffeePot coffeePot;
    }
}

现在想办法立即调用应用程序启动...。

Now to find a way to call this right away on application launch....

返回到您的dagger组件或您扩展的应用程序类中,您可以使用一个静态方法来调用每个模块中的每个模块:

Back in your dagger component or in the application class you extend you can have a static method to call each one of these per module:

  static void injectAllEagerObjects() {
            myModule.initEagerObjects();
            someOtherModule.initEagerObjects();
            //...all of them can be here since there static

}

现在我们差不多完成了,只需要在应用程序启动时调用它即可。
因此,从应用程序创建图形之后,就必须调用injectAllEagerObjects()(如果需要,可以在其中传递图形实例)。这将以正确的方式初始化渴望的单例并且仅初始化一次。

now we are almost done, just have to call it at application launch. So right after you create your graph from application you have to call injectAllEagerObjects() (and probably pass in your graph instance there if you want). This will initialize the eager singletons right way and only once.

话虽如此,我希望匕首仅具有您可以这样使用的注释:
@singleton(eager = true)但是静态字段,这是他们现在推荐什么。

all this being said, i wish dagger just had a annotation you could use like this: @singleton(eager=true) but static fields, this is what they recommend for now.

这篇关于Dagger 2-如何创建/提供EagerSingleton的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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