当前位置: 首页 > 知识库问答 >
问题:

了解Dagger2子组件

蒯安平
2023-03-14

我的应用程序中有以下Dagger2架构:

-- AppComponent (@PerApplication)
  -- UserComponent (@PerUser)
    -- ActivityComponent (@PerActivity)
      -- ChatComponent (@PerActivity) <-- 1

其中:AppComponent:

@PerApplication
@Component(modules = {ApplicationModule.class, StorageModule.class, NetworkModule.class})
public interface ApplicationComponent {
    UserComponent plus(UserModule userComponent);

    //Exposed to sub-graphs.
    Context application();
}

UserComponent:

@PerUser
@Subcomponent(modules = {UserModule.class, RosterModule.class})
public interface UserComponent {
    ActivityComponent plus(ActivityModule activityModule);

    User getMe();

    UserRepository userRepository();
}

ActivityComponent:

@PerActivity
@Subcomponent(modules = ActivityModule.class)
public interface ActivityComponent {

    ChatComponent plus(ChatModule chatComponent);

    //Exposed to sub-graphs.
    Context context();
}

ChatComponent:

@PerActivity
@Subcomponent(modules = {ChatModule.class})
public interface ChatComponent {
    void inject(ChatListFragment chatListFragment);
    void inject(ConversationFragment conversationFragment);
    void inject(NewConversationFragment newConversationFragment);
    void inject(CloudFilesFragment cloudFilesFragment);
    void inject(ChatActivity chatActivity);
    void inject(ConversationActivity conversationActivity);
    void inject(NewConversationActivity newConversationActivity);

    void inject(NewGroupActivity newGroupActivity);
    void inject(NewGroupFragment newGroupFragment);
}

首先,如何将不同的上下文注入到类中?要么是应用程序,要么是活动??

其次,我在编译代码时遇到了一个奇怪的问题,错误是:

错误:(23,10)错误:br.com.animaeducacao.ulife.domain.interactor.usecase不能在没有@provides-annotated方法的情况下提供。br.com.animaeducacao.ulife.presentation.view.fragment.chatlistfragment.chatListPresenter[注入类型的字段:br.com.animaeducacao.ulife.presentation.present.present.chatListPresenter chatListPresenter]br.com.animaeducacao.ulife.presentation.presentation.chatListPresenter(.domain.interactor.usecase chatDialogsUseCase]

我的ChatListFragment是:

@PerActivity
public class ChatListFragment extends BaseFragment implements ChatListView {

    @Inject
    ChatListPresenter chatListPresenter;
  ...
//called onActivityCreated()
private void initialize() {
        this.getComponent(ChatComponent.class).inject(this);
}

BaseFragment:

protected <C> C getComponent(Class<C> componentType) {
    return componentType.cast(((HasComponent<C>)getActivity()).getComponent());
  }

ChatListPresenter:

@PerActivity
public class ChatListPresenter implements Presenter {

    private final UseCase chatDialogsUseCase;
    private final UseCase adviceUserPresence;
    private final Context context;
    private ChatListView chatListView;

    @Inject
    public ChatListPresenter(@Named("getChatDialogs") UseCase chatDialogsUseCase,
                             @Named("adviceUserPresence") UseCase adviceUserPresence,
                             Context context) {
        this.chatDialogsUseCase = chatDialogsUseCase;
        this.adviceUserPresence = adviceUserPresence;
        this.context = context;
    }
@Provides
    @PerActivity
    @Named("getChatDialogs")
    public UseCase provideChatDialogs(@Named("transactionalChatRepository") ChatRepository chatRepository, ThreadExecutor threadExecutor, PostExecutionThread postExecutionThread) {
        return new GetUserChatDialogs(chatRepository, threadExecutor, postExecutionThread);
    }

这是个好办法吗?为什么这不是编译,我在这里错过了什么?很抱歉发了这么长的帖子,谢谢!

共有1个答案

姜献
2023-03-14

啊,你有多重问题。

1.)当您正确地使用子作用域时(您首先正确地使用@subcomponent),chatcomponent实际上并没有子作用域其父组件-基本上,chatcomponent不能是@peractivity,它需要是第四个作用域。

@subcomponent注释只是一种创建子范围组件的方法,而不必将其指定为组件依赖项。它仍然需要自己的“更具体”范围。

例如,您的ApplicationComponent没有为StorageModule中的内容提供方法,因此StorageModule提供的依赖项不能继承到子作用域组件。

但是,我不确定是否可以只指定所提供的类,如果它不在模块中,而是用@inject构造函数对其进行注释,并用作用域标记该类。

此外,为了在作用域层次结构a->b->C中允许C从a继承,那么B也需要具有a的提供方法。

3.)您应该使用@named(“application”)@named(“activity”)注释来指定两个不同的上下文,或者在模块中将它们称为applicationactivity,这样它们就不会混淆了。

 类似资料:
  • 问题内容: 我正在构建一个接受JSON数据源并创建可排序表的React组件。 每个动态数据行都有一个分配给它的唯一键,但是我仍然遇到以下错误: 数组中的每个子代都应具有唯一的“键”道具。 检查TableComponent的渲染方法。 我的渲染方法返回: 该组件是单行,并且还为其分配了唯一的键。 每个输入都是通过具有唯一键的组件构建的: 而看起来像这样: 是什么导致独特的按键道具错误? 问题答案:

  • 我正在构建一个React组件,它接受一个JSON数据源并创建一个可排序表。 每个动态数据行都有一个唯一的键分配给它,但我仍然得到一个错误: 数组中的每个子项都应该有一个唯一的“键”道具。 检查TableComponent的render方法。 我的呈现方法返回: 组件是单行,并且还为其分配了唯一的键。 中的每个都是由具有唯一键的组件生成的: 而如下所示: 是什么原因导致了唯一键道具错误?

  • 问题内容: 这是我目前拥有的并且可以正常工作: 现在,我想添加另一个依赖项。我将其更改为: 但是现在我收到此错误消息: FragmentFacebookLoginComponent依赖于多个作用域组件 我该如何解决?我如何有多个依赖关系? 如果我从一个组件中删除范围,则会收到此错误消息: AnotherComponent(无作用域)不能依赖范围内的组件 问题答案: 最后,我创建了一个具有正确范围的

  • 我正在尝试使用制作多模块项目。您可以通过链接查看我的代码。在分支是工作解决方案,其中所有匕首类都在模块中。 现在,我正在尝试为DI根创建单独的< code>app模块。您可以在< code>develop分支中看到最新的尝试。它不起作用。我想在< code>app模块中创建我的根< code > application component 组件,并从其他模块添加< code > presentat

  • 问题内容: 我试图把我的头缠在三维阵列上。我知道它们是二维数组的数组,但是我正在阅读的书说的话使我感到困惑。 在我正在阅读的书的练习中,它要求我为全彩色图像制作三维阵列。它给出了一个小例子,说明了这一点: 如果我们决定选择三维数组,则可以通过以下方式声明数组: 但是,这样会更有效吗? 其中3是rgb值,0是红色,1是绿色,2是蓝色。对于后者,每个二维数组将存储行和列的颜色值,对吗?我只想确保我了解

  • null 模拟UI元素的Robolectric 逻辑测试的单元测试 静态方法mock的PowerMock Robolectric+PowerMock集成问题已知,解决方案也已知--https://github.com/Robolectric/Robolectric/wiki/using-powerMock ,但使用此解决方案时,dagger2依赖项失败。 注意代码。 我的自定义视图: Progre