我的模块:
@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
, I get NullPointerException
- 意味着注入没有填充字段。
我完全按照文档中的示例进行操作,有什么问题吗?
Note: on the builder line
tcpComponent = DaggerTcpManagerComponent.builder().tcpManagerModule(new TcpManagerModule(context)).build();
我收到一条警告,提示“tcpManagerModule 已弃用”。我读了答案here https://stackoverflow.com/a/36521366/1662033关于这个问题及其说法
可以肯定地说,您可以忽略弃用。它旨在通知您未使用的方法和模块。一旦您在子图中的某处实际需要/使用应用程序,则将需要该模块,并且弃用警告将消失。
那么,我不需要/使用这些实例吗?这里有什么问题呢?