匕首的自定义范围,如何?

时间:2018-08-20 12:51:45

标签: android dependency-injection dagger-2

我需要遵循匕首2.16中的作用域结构-> Custom Scope

我该怎么做?

我不想使用dagger android支持,因为它的Beta版

我尝试:

//ApplicationComponent.java
@Singleton
@Component(modules = {
    ApplicationModule.class,
})
public interface ApplicationComponent {
    UserComponent.Builder userComponentBuilder();
    ActivityComponent.Builder activityComponentBuilder();
 ...
}

//ApplicationModule.java
@Module(subcomponents = {
        ActivityComponent.class,
        UserComponent.class
})
public class ApplicationModule {
...
}

//UserComponent.java
@PerUser
@Subcomponent(modules = {
        UserModule.class
})
public interface UserComponent {
    ActivityComponent.Builder activityComponentBuilder();
    ...

    @Subcomponent.Builder
    interface Builder {
        UserComponent build();
        Builder userModule(UserModule userModule);
    }
}

//UserModule.java
@Module(subcomponents = {
        ActivityComponent.class,
        FragmentComponent.class
})
public class UserModule {
...
}

//ActivityComponent.java
@PerActivity
@Subcomponent(modules = {
        ActivityModule.class,
})
public interface ActivityComponent {
...

    @Subcomponent.Builder
    interface Builder {
        ActivityComponent build();
        Builder activityModule(ActivityModule activityModule);
    }
}

//ActivityModule.java
@Module(subcomponents = FragmentComponent.class)
public class ActivityModule {
...
}

//FragmentComponent.java
@PerFragment
@Subcomponent(modules = {
        FragmentModule.class
})
public interface FragmentComponent {
...

    @Subcomponent.Builder
    interface Builder {
        FragmentComponent build();
        Builder fragmentModule(FragmentModule fragmentModule);
    }
}

//FragmentModule.java
@Module
public class FragmentModule {
...
}

但是无法将提供的对象注入UserModule并获得错误。

  

错误:[Dagger / MissingBinding]

匕首无法正确检测组件路径:

  

... ApplicationComponent吗? ... ActivityComponent

它必须是:

  

... ApplicationComponent吗? ... UserComponent? ... ActivityComponent

0 个答案:

没有答案