Java Dagger 2不生成子组件实现
我已经开始安装匕首2,并且面临着一个奇怪的问题,对我来说这看起来像一个bug 我有1个主要组件和2个子组件,我在父组件中加上了它们。我为每个子组件使用不同的Java Dagger 2不生成子组件实现,java,android,dependency-injection,dagger-2,Java,Android,Dependency Injection,Dagger 2,我已经开始安装匕首2,并且面临着一个奇怪的问题,对我来说这看起来像一个bug 我有1个主要组件和2个子组件,我在父组件中加上了它们。我为每个子组件使用不同的范围。问题是,我可以很容易地为第一个子组件进行字段注入,但我不能为第二个子组件进行相同的操作。注入的字段保持nulls 主要组成部分: @Singleton @Component(modules = { WalletSaverAppModule.class }) public interface MyAppComponent { Trac
范围。问题是,我可以很容易地为第一个子组件进行字段注入,但我不能为第二个子组件进行相同的操作。注入的字段保持null
s
主要组成部分:
@Singleton
@Component(modules = { WalletSaverAppModule.class })
public interface MyAppComponent {
TrackingComponent plus(TrackingModule module);
DashboardComponent plus(DashboardModule module);
}
public class TrackingService extends IntentService {
@Inject CallCase mCallCase;
@Inject CallModelMapper mCallModelMapper;
...
@Override protected void onHandleIntent(Intent intent) {
((MyApp) getApplication()).getAppComponent().plus(new TrackingModule(this)).inject(this);
// ---> here the both fields are null
...
第一个子组件(工作正常):
第二个子组件(字段注入->空):
我如何为第二个子组件执行字段注入:
@Singleton
@Component(modules = { WalletSaverAppModule.class })
public interface MyAppComponent {
TrackingComponent plus(TrackingModule module);
DashboardComponent plus(DashboardModule module);
}
public class TrackingService extends IntentService {
@Inject CallCase mCallCase;
@Inject CallModelMapper mCallModelMapper;
...
@Override protected void onHandleIntent(Intent intent) {
((MyApp) getApplication()).getAppComponent().plus(new TrackingModule(this)).inject(this);
// ---> here the both fields are null
...
我正在注入的对象:
@Singleton public class CallCase {
private CallRepository mCallRepository;
@Inject public CallCase(final CallRepository userRepository) {
mCallRepository = userRepository;
}
public Observable<Call> execute() {
...
}
}
@Singleton public class CallModelMapper {
@Inject CallModelMapper() {
}
public CallModel transform(@NonNull final Call callEntity) {
...
}
}
第二:
私有最终类TrackingComponentMPL实现TrackingComponent{
专用最终跟踪模块跟踪模块;
专用跟踪组件MPL(跟踪模块跟踪模块){
this.trackingModule=前提条件.checkNotNull(trackingModule);
//-->听着,TrackingComponent中缺少call initialize()为什么要注入到IntentService。也许更改为TrackingService会有所帮助
void inject(TrackingService context);
在TrackingComponent中,为什么要注入IntentService。也许更改为TrackingService会有所帮助
void inject(TrackingService context);
如果将TrackingComponent的范围更改为PerActivity,会发生什么情况?如果将TrackingComponent的范围更改为PerActivity,会发生什么情况?+1。请参阅“关于协方差的说明”在上:如果您将成员注入方法编写为接受IntentService
,您将只为IntentService及其超类注入字段,因此不会发生任何事情。如果您接受TrackingService
,Dagger可以为TrackingService和那里的@inject
字段执行编译时魔法。谢谢!Golden eyes;)+1.参见“关于协方差的注释”在上:如果您将成员注入方法编写为接受IntentService
,您将只为IntentService及其超类注入字段,因此不会发生任何事情。如果您接受TrackingService
,Dagger可以为TrackingService和那里的@inject
字段实现编译时魔法。谢谢!Golden eyes;)
void inject(TrackingService context);