我正在尝试复制ViewModelFactory对我的演示者类所做的工作,这是我的Dagger2代码
public class App extends DaggerApplication { //Support import
@Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
return DaggerAppComponent.builder().create(this);
}
}
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@MapKey
public @interface PresenterKey {
Class<? extends PresenterClass> value();
}
@Module
public abstract class PresenterModule {
@Binds
@IntoMap
@PresenterKey(AuthPresenterImpl.class)
abstract PresenterClass bindsAuthPresenterImpl(AuthPresenterImpl authViewModel);
@Binds
abstract PresenterFactory bindsPresenterFactory(PresenterFactory viewModelFactory);
}
@Singleton
public class PresenterFactory {
private final Map<Class<? extends PresenterClass>, Provider<PresenterClass>> creators;
@Inject
public PresenterFactory(Map<Class<? extends PresenterClass>, Provider<PresenterClass>> creators) {
this.creators = creators;
}
@SuppressWarnings("unchecked")
public <T extends PresenterClass> T create(Class<T> modelClass) {
Provider<? extends PresenterClass> creator = creators.get(modelClass);
if (creator == null) {
for (Map.Entry<Class<? extends PresenterClass>, Provider<PresenterClass>> entry : creators.entrySet()) {
if (modelClass.isAssignableFrom(entry.getKey())) {
creator = entry.getValue();
break;
}
}
}
if (creator == null) {
throw new IllegalArgumentException("unknown model class " + modelClass);
}
try {
return (T) creator.get();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
当我尝试构建时,出现错误
import in.silentsudo.authflow.di.modules.DaggerAppComponent;
^
symbol: class DaggerAppComponent
location: package in.silentsudo.authflow.di.modules
/path/to/project/app/src/main/java/in/silentsudo/authflow/di/modules/AppComponent.java:17: error: [Dagger/BindingCycle] [dagger.android.AndroidInjector.inject(T)] Found a dependency cycle:
public interface AppComponent extends AndroidInjector<AuthFlow> {
^
in.silentsudo.authflow.viewmodels.PresenterFactory is injected at
in.silentsudo.authflow.di.modules.PresenterModule.bindsPresenterFactory(presenterFactory)
in.silentsudo.authflow.viewmodels.PresenterFactory is injected at
in.silentsudo.authflow.AbstractActivity.factory
in.silentsudo.authflow.auth.AuthActivity is injected at
dagger.android.AndroidInjector.inject(T)
component path: in.silentsudo.authflow.di.modules.AppComponent ? in.silentsudo.authflow.di.modules.ActivityModule_BindsAuthActivity.AuthActivitySubcomponent
2 errors
我可能在做任何错误/愚蠢的事情,请帮忙
已编辑
:具体课程
public class AuthPresenterImpl extends AbstractPresenter {
@Inject
public AuthPresenterImpl(AuthApi api) {
}
}
答案 0 :(得分:1)
这是您的依赖周期:
@Binds
@IntoMap
@PresenterKey(PresenterClass.class)
abstract PresenterClass bindsAuthPresenterClass(PresenterClass authViewModel);
您必须通过这样的具体实现:
@Binds
@IntoMap
@PresenterKey(ConcretePresenterClass.class)
abstract PresenterClass bindsAuthPresenterClass(ConcretePresenterClass authViewModel);