Dagger 2 - 使用@Named无法正常注入多个相同类型的对象 [英] Dagger 2 - injecting multiple objects of same type using @Named not working

查看:256
本文介绍了Dagger 2 - 使用@Named无法正常注入多个相同类型的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模块:

@Module
public class TcpManagerModule {
    private ITcpConnection eventsTcpConnection;
    private ITcpConnection commandsTcpConnection;

    public TcpManagerModule(Context context) {
        eventsTcpConnection = new EventsTcpConnection(context);
        commandsTcpConnection = new CommandsTcpConnection(context);
    }

    @Provides
    @Named("events")
    public ITcpConnection provideEventsTcpConnection() {
        return eventsTcpConnection;
    }

    @Provides
    @Named("commands")
    public ITcpConnection provideCommandsTcpConnection() {
        return commandsTcpConnection;
    }
}

组件:

@Component(modules = TcpManagerModule.class)
public interface TcpManagerComponent {
    void inject(ITcpManager tcpManager);
}

注射发生的类:

public class DefaultTcpManager implements ITcpManager {
    private TcpManagerComponent tcpComponent;

    @Inject @Named("events") ITcpConnection eventsTcpConnection;

    @Inject @Named("commands") ITcpConnection commandsTcpConnection;

    public DefaultTcpManager(Context context){
        tcpComponent = DaggerTcpManagerComponent.builder().tcpManagerModule(new TcpManagerModule(context)).build();
        tcpComponent.inject(this);
    }

    @Override
    public void startEventsConnection() {
        eventsTcpConnection.startListener();
        eventsTcpConnection.connect();
    }
}

当我打电话给 startEventsConnection ,我得到 NullPointerException - 意味着注入没有填充字段。

When I call startEventsConnection, I get NullPointerException - meaning the injection didn't populate the fields.

I按照示例的说明完全按照文档的方式,问题是什么?

I followed the example exactly the way it is on the Docs, what is the issue?

注意:构建器上 line

tcpComponent = DaggerTcpManagerComponent.builder().tcpManagerModule(new TcpManagerModule(context)).build();

我有一个警告说tcpManagerModule已被弃用。我在此处上阅读了有关此问题的答案及其说法

I have a warning saying "tcpManagerModule is deprecated". I read the answer here about this issue, and its saying


可以肯定地说,你可以忽略弃用。它旨在通知您未使用的方法和模块。一旦你在子图中的某个地方实际需要/使用应用程序,就需要使用该模块,并且弃用警告将会消失。

It is safe to say that you can just ignore the deprecation. It is intended to notify you of unused methods and modules. As soon as you actually require / use Application somewhere in your subgraph the module is going to be needed, and the deprecation warning will go away.



<那么,我不需要/使用实例吗?这里有什么问题?

So, am I not requiring/using the instances? What is the issue here?

推荐答案

您可以尝试更改组件定义特定的注入类:

You could try changing your Component defining the specific class for injection:

@Component(modules = TcpManagerModule.class)
public interface TcpManagerComponent {
    void inject(DefaultTcpManager tcpManager);
}

因此Dagger完全知道 DefaultTcpManager.class

So that Dagger knows exactly about DefaultTcpManager.class.

这篇关于Dagger 2 - 使用@Named无法正常注入多个相同类型的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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