简体   繁体   English

具有运行时子组件的Dagger 2 Singleton组件

[英]Dagger 2 Singleton Component with RunTime SubComponent

I'm building an application which uses Dagger 2. 我正在构建一个使用Dagger 2的应用程序。

So, my application component looks like this: 因此,我的应用程序组件如下所示:

@Singleton
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {

    // Exported for child-components.
    Application application ();

    final class Initializer {
        public static ApplicationComponent init (Application app) {
            return DaggerApplicationComponent.builder()
                    .applicationModule(new ApplicationModule(app)).build();
        }

        private Initializer () {
        } // No instances.
    }


    ConnectorComponent plus(ConnectorModule module);

    ... injections

}

This is scoped with @Singleton, so it's unique for the app lifetime. 这在@Singleton范围内,因此对于应用程序生命周期而言是唯一的。
Then I have another component, which should be able to use all dependencies provided by the above component, but provide it's dependencies with a @PerActivity scope: 然后,我有另一个组件,该组件应该能够使用上述组件提供的所有依赖项,但要为它的依赖项提供@PerActivity范围:

@Scope
@Retention(RUNTIME)
public @interface PerActivity {}

And looks like this 看起来像这样

@PerActivity
@Subcomponent(modules = ConnectorModule.class)
public interface ConnectorComponent  {

    ... injections
}

The problem is. 问题是。 How can I make sure the everything that is provided with @Singleton will be passed as Singletons on the dependencies to ConnectorModule? 我如何确保@Singleton随附的所有内容都将作为依赖项上的Singletons传递到ConnectorModule? Another thing, how can I make sure that the method plus in ApplicationComponent will always add a new Module (the @PerActivity) that I want? 另一件事,如何确保ApplicationComponent中的plus方法将始终添加我想要的新模块(@PerActivity)?

Last of all, can anybody think of a strategy to use this in all activites (which makes sense to be used with @PerAcitivity scope) and not having to call plus every time with a new ConnectorModule? 最后,有人能想到一种在所有活动中都使用它的策略(与@PerAcitivity范围一起使用才有意义),而不必每次都使用新的ConnectorModule调用plus吗?

How can I make sure the everything that is provided with @Singleton will be passed as Singletons on the dependencies to ConnectorModule? 我如何确保@Singleton随附的所有内容都将作为依赖项上的Singletons传递到ConnectorModule?

Dagger will ensure that any @Singleton -annotated type or @Provides method in ApplicationModule will be provided only once during the lifetime of the DaggerApplicationComponent you build, even when a binding in ConnectorComponent uses it. 匕首将确保任何@Singleton -annotated类型或@Provides的方法ApplicationModule将在的一生中只有一次提供DaggerApplicationComponent你建立,即使在结合ConnectorComponent使用它。

how can I make sure that the method plus in ApplicationComponent will always add a new Module (the @PerActivity) that I want? 如何确保ApplicationComponent中的plus方法将始终添加我想要的新模块(@PerActivity)?

I'm not sure what you're asking here. 我不确定您在这里问什么。 When you call plus , you'll have to pass in an instance of ConnectorModule yourself. 调用plus ,您必须自己传入一个ConnectorModule实例。

can anybody think of a strategy to use this in all activites (which makes sense to be used with @PerAcitivity scope) and not having to call plus every time with a new ConnectorModule? 有谁能想到一种在所有活动中都使用它的策略(将其与@PerAcitivity范围一起使用是有意义的),而不必每次都使用新的ConnectorModule进行调用吗?

Does ConnectorModule take arguments in its constructor? ConnectorModule是否在其构造函数中接受参数? If not, you can leave it out of the plus method's arguments, and Dagger will construct one for you. 如果没有,您可以将其放在plus方法的参数之外,而Dagger将为您构造一个。

However, you still have to call the plus method in order to get an instance of the subcomponent for the activity. 但是,您仍然必须调用plus方法才能获取活动的子组件实例。 That's how you get the @PerActivity scope to work: there's an instance of ConnectorComponent , and that scope is tied to the lifetime of that instance. 这就是使@PerActivity范围起作用的方式:有一个ConnectorComponent实例,并且该范围与该实例的生命周期相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM