Dagger2使用

Dagger2的引入(与retrofit和rxjava一起使用):
在app的build.gradle中加入

    //network
    compile "com.squareup.retrofit2:retrofit:$rootProject.ext.retrofitVersion"
    compile "com.squareup.retrofit2:converter-gson:$rootProject.ext.retrofitVersion"
    compile "com.squareup.retrofit2:adapter-rxjava2:$rootProject.ext.retrofitVersion"
    compile "com.squareup.okhttp3:logging-interceptor:$rootProject.ext.okhttpVersion"
    compile "io.reactivex.rxjava2:rxjava:$rootProject.ext.rxjavaVersion"
    compile "io.reactivex.rxjava2:rxandroid:$rootProject.ext.rxandroidVersion"

    //inject
    compile "com.google.dagger:dagger:$rootProject.ext.daggerVersion"
    annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.ext.daggerVersion"

在整个项目的build.gradle中加入(有些不是相关的,请自行忽略):

ext{
    loggerVersion = "1.15"
    stethoVersion = "1.4.2"
    leakcanaryVersion = "1.5"
    blockcanaryVersion = "1.5.0"
    rxjavaVersion = "2.0.7"
    rxandroidVersion = "2.0.1"
    okhttpVersion = "3.6.0"
    retrofitVersion = "2.2.0"
    daggerVersion = "2.10-rc4"
}

Dagger2的核心是module提供实例,component提供注入。下面直接来看它的使用
首先创建一个类叫做AppModule,它的作用是对外提供全局的实例,
例如context,

    private Context mContext;

    public AppModule(Context mContext) {
        this.mContext = mContext;
    }

    @Singleton
    @Provides
    Context provideContext(){
        return mContext;
    }

retrofit对象,

    @Singleton
    @Provides
    Retrofit provideRetrofit(@Named("base_url") String baseUrl, OkHttpClient client)      {
        return new Retrofit.Builder()
                .baseUrl(baseUrl)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

Gson对象,

    @Singleton
    @Provides
    Gson provideGson() {
        return new Gson();
    }

以及Httpclient对象

@Provides
    OkHttpClient provideOkHttpClient(HttpLoggingInterceptor loggingInterceptor, Cache cache, @Named("cache_interceptor") Interceptor cacheInterceptor, @Named("parameter_interceptor") Interceptor parameterInterceptor) {
        return new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor)
                .cache(cache)
                .addInterceptor(cacheInterceptor)
                .addInterceptor(parameterInterceptor)
                .addNetworkInterceptor(cacheInterceptor)
                .addNetworkInterceptor(new StethoInterceptor())
                .build();
    }

等等需要放在全局的实例,都放在这里。。。。

接着需要写的是AppComponent: component的作用就是注入

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
    /**
     * 获取上下文对象
     *
     * @return context
     */
    Context getContext();

    /**
     * 获取Gson对象
     *
     * @return Gson实例
     */
    Gson getGson();

    /**
     * 获取Retrofit对象
     *
     * @return Retrofit实例
     */
    Retrofit getRetrofit();

    /**
     * 获取HttpHelper
     *
     * @return HttpHelper的实例
     */
    HttpManager getHttpHelper();
}

下面举个具体的例子来看:
在特定的activity中,一般来说是一个页面一个component

@PerActivity
@Component(modules = ExamplePresenterModule.class, dependencies = AppComponent.class)
interface ExampleComponent {

    void inject(ExampleActivity exampleActivity);

}
@Component(modules = ExamplePresenterModule.class,

是固定写法,modules = 这个页面需要的module

void inject(ExampleActivity exampleActivity);

这就是目标activity
接下来看看ExamplePresenterModule

@Module
class ExamplePresenterModule {
    private ExampleContract.View mView;

    public ExamplePresenterModule(ExampleContract.View mView) {
        this.mView = mView;
    }

    @PerActivity
    @Provides
    ExamplePresenter provideExamplePresenter(RetrofitService retrofitService, HttpManager httpManager){
        return new ExamplePresenter(mView,retrofitService,httpManager);
    }

 @Provides
    RetrofitService provideRetrofitService(Retrofit retrofit){
        return retrofit.create(RetrofitService.class);
    }
}

它在这里的作用主要就是提供一个ExamplePresenter实例,用于网络请求数据,它的参数中的RetrofitService也是一个对象,也是需要创建的,所以才会有下面的代码

 @Provides
    RetrofitService provideRetrofitService(Retrofit retrofit){
        return retrofit.create(RetrofitService.class);
    }

而另一个参数HttpManager httpManager已经在全局的module中创建过了,所以不需要再创建了

接着在ExampleActivity中注入(onCreate中):

    @Override
    protected void setUpInject() {
        DaggerExampleComponent.builder()
                .appComponent(getAppComponent())
                .examplePresenterModule(new ExamplePresenterModule(mFragment))
                .build()
                .inject(this);
    }

要想使用presenter,需要在类中注入它,注入之后在activity的范围内就可以尽情的使用它了

    @Inject
    ExamplePresenter mPresenter;

我是在activity下面的fragment中使用的

public class ExampleFragment extends BaseFragment<ExamplePresenter> implements ExampleContract.View

在需要发起请求的地方:

presenter.getExampleData("607","108.895002706077776","34.241995671795905","");

这个项目整体用的是MVP+Retrofit+RxJava+Dagger2
项目地址:https://github.com/ckwcc/MVP-Retrofit-RxJava-Dagger2

你可能感兴趣的:(Android)