【问题标题】:Dagger2: how to inject retrofit module in presenter classDagger2:如何在演示者类中注入改造模块
【发布时间】:2018-11-08 11:02:01
【问题描述】:

Dagger2 学习仍然很困难,正在努力学习。已经使用新的 dagger android 设置了一个项目,以避免在活动类中注入。到目前为止它正在工作,但需要使用在演示者类中注入的改造。在 AppComponent 中添加了改造模块,但 apiService 类方法在从演示者类调用时为空。需要知道如何正确注射。

AppComponent.java

@Singleton
@Component(modules = {
    /* Use AndroidInjectionModule.class if you're not using support library */
    AndroidSupportInjectionModule.class,
    AppModule.class,
    BuildersModule.class,
    RetrofitModule.class})
public interface AppComponent {

@Component.Builder
interface Builder {
    @BindsInstance
    Builder application(BaseApplication application);

    Builder retrofitModule(RetrofitModule retrofitModule);

    AppComponent build();
}

  void inject(BaseApplication app);
}

AppModule.java

@Module
class AppModule {

    @Provides
    Context provideContext(BaseApplication application) {
        return application.getApplicationContext();
    }
}

BuildersModule.java

@Module
public abstract class BuildersModule {

    @ContributesAndroidInjector(modules = {SplashViewModule.class, SplashModule.class})
    abstract SplashActivity bindSplashActivity();

    // Add bindings for other sub-components here
}

BaseApplication.java

public class BaseApplication extends Application implements HasActivityInjector {

    @Inject
    DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;

    @Override
    public void onCreate() {
        super.onCreate();

        //configure timber for logging
        if (BuildConfig.DEBUG) {
            Timber.plant(new Timber.DebugTree());
        }

        DaggerAppComponent
                .builder()
                .application(this)
                .retrofitModule(new RetrofitModule())
                .build()
                .inject(this);
    }


    @Override
    public AndroidInjector<Activity> activityInjector() {
        return dispatchingAndroidInjector;
    }
}

RetrofitModule.java

@Module
public class RetrofitModule {

    //get retrofit instance
    @Singleton
    @Provides
    public UserAuthService getRestService() {

        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ApiConstants.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(getOkHttpClient())
                .build();

        return retrofit.create(UserAuthService.class);
    }
}

UserAuthService.java

public interface UserAuthService {

    @GET("v2/5be345062f00006b00ca22c4")
    Observable<Example> getExampleResponse();
}

SplashModule.java

@Module
public class SplashModule {

    @Provides
    SplashPresenter provideSplashPresenter(SplashView splashView) {
        return new SplashPresenter(splashView);
    }
}

SplashViewModule.java

@Module
public abstract class SplashViewModule {

    @Binds
    abstract SplashView provideSplashView(SplashActivity splashActivity);

}

SplashPresenter.java

class SplashPresenter extends BasePresenter<SplashView> {


    @Inject
    @Named(ValueConstants.MAIN_THREAD)
    Scheduler mMainThread;

    @Inject
    @Named(ValueConstants.NEW_THREAD)
    Scheduler mNewThread;

    @Inject
    UserAuthService userAuthService;

    SplashPresenter(SplashView view) {
        super(view);
    }

    //test purpose
    public void sayHello() {

        userAuthService.getExampleResponse()
                .observeOn(mMainThread)
                .subscribeOn(mNewThread)
                .subscribe(new Observer<Example>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(Example example) {
                        Timber.d("example %s", example.toString());
                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}

此处“userAuthService.getExampleResponse()”为空。我认为演示者需要了解 RetrofitModule 注入。所以我需要解决这个问题以及如何解决?

【问题讨论】:

    标签: java android dagger-2 dagger


    【解决方案1】:

    我会将这些依赖项添加到SplashPresenter 的构造函数中,并在其中添加@Inject 注释。 Dagger2 支持构造函数注入,所有你需要的依赖都是可解析的(也可以去掉SplashModule

    class SplashPresenter extends BasePresenter<SplashView> {
    
       private Scheduler mMainThread;
    
       private Scheduler mNewThread;
    
       private UserAuthService userAuthService;
    
      @Inject
      SplashPresenter(SplashView view, UserAuthService userAuthService, @Named(ValueConstants.NEW_THREAD) Scheduler newThread,  @Named(ValueConstants.MAIN_THREAD) Scheduler mainThread) {
            super(view);
            this.userAuthService = userAuthService;
            mNewThread = newThread;
            mMainThread = mainThread;
      }
    

    【讨论】:

    • 感谢您的建议。你能添加代码sn-p吗? (它也可能对其他人有帮助)。为什么你想摆脱 SplashModule?
    • 我确实添加了 sn-p
    • 想避免构造函数注入。需要知道使用 dagger2 android 组件在演示者类中注入模块的正确方法。
    • 构造函数是匕首2的正确方法。
    猜你喜欢
    • 2020-05-01
    • 1970-01-01
    • 2020-12-01
    • 2016-11-15
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 1970-01-01
    相关资源
    最近更新 更多