Android开发Android知识Android技术知识

我所了解的 Dagger2(二)

2017-05-27  本文已影响0人  DthFish

我所了解的 Dagger2(一)之后,这篇文章主要进行以下几个方面的补充:

  1. @Scope 实现一个单例;
  2. 自定义 @Scope
  3. Component 之间的拓展和依赖。

APP 内实现单例

上一篇文章中讲到了真正创建单例的方法,简单的回顾一下:

  1. 在 AppModule 中定义创建全局类实例的方法;
  1. 创建 AppComponent 来管理 AppModule;
  2. 保证 AppComponent 只有一个实例;
声明

现在我们要在项目的 Application 中创建 AppComponent,保证我们 app 全局只有一个 OkHttpClient,来演示一下单例:

@Singleton
@Component(modules = {AppModule.class})
public interface AppComponent {
    void inject(MyApplication application);
    OkHttpClient getOKHttpClient(); //非必要的,这里可以暴露方法供我们直接用 AppComponent 实例调用
}
------ 分割线 ------
@Module
public class AppModule {
    @Provides
    @Singleton
    OkHttpClient provideOkHttpClient(Interceptor interceptor) {
        return new OkHttpClient.Builder().connectTimeout(30 * 1000, TimeUnit.MILLISECONDS)
                .addInterceptor(interceptor)
                .readTimeout(30 * 1000, TimeUnit.MILLISECONDS)
                .build();
    }
    @Provides
    Interceptor provideCommonParamsInterceptor() {
        return new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                final HttpUrl newUrl = request.url().newBuilder()
                        .addQueryParameter("channel", "android")
                        .addQueryParameter("version", "1.0.0")
                        .build();
                Request newRequest = request.newBuilder().url(newUrl).build();
                return chain.proceed(newRequest);
            }
        };
    }
}
------ 分割线 ------
public class MyApplication extends Application {
    @Inject
    AppComponent mAppComponent;

    private static MyApplication sInstance;

    public static MyApplication get(){
        return sInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        DaggerAppComponent.create().inject(this);
    }

    public AppComponent getAppComponent(){
        return mAppComponent;
    }
}

这里我们保证只要 Application 中只有 AppComponent,那么我们的单例就已经完成了,唯一可能感到疑惑的是 MyApplication 中的这种写法:

@Inject
AppComponent mAppComponent;

我们并没有用 @Inject 标注哪个构造函数,也没有在 Module 中提供,mAppComponent 怎么初始化?事实上,Dagger2 生成的 DaggerAppComponent 继承了 AppComponent,inject() 的时候把自身赋值给了 mAppComponent,有兴趣的同学可以看下这样写的时候 Dagger2 生成的代码,还是比较容易读懂的。

使用

那么单例已经实现了,我们看下如何在 Activity 中使用,顺便验证一下是不是单例的。现在我们需要用到的 OkHttpClient 已经包含在 AppComponent 中,当然不能再定义一个 Component 去把 AppModule 直接添加进来,所以我们需要用到 Component 的拓展和依赖。

方法一:拓展(@Subcomponent)

用于拓展原有的 Component。这里我把 @SubComponent 称作拓展,主要就是与接口继承做一下区分。

//简单的实现
@Subcomponent
public interface OkHttpSingleComponent {
    void inject(OkHttpSingleActivity secondActivity);
}
------ 分割线 ------
@Singleton
@Component(modules = {AppModule.class})
public interface AppComponent {
    void inject(MyApplication application);
    OkHttpSingleComponent plus();
    OkHttpClient getOKHttpClient();
}

实现了一个最简单的 @Subcomponent,然后在 AppComponent 中加一个方法把 OkHttpSingleComponent 返回出去,继续看一下 OkHttpSingleActivity 的实现:

public class OkHttpSingleActivity extends BaseActivity {
    @Inject
    OkHttpClient mOkHttpClient;
    @Inject
    OkHttpClient mOkHttpClient2;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_okhttp_single);
        MyApplication.get().getAppComponent().plus().inject(this);//

        TextView tvTime = (TextView) findViewById(R.id.tv_time);
        tvTime.setText("time:" + System.currentTimeMillis());
        TextView tv = (TextView) findViewById(R.id.tv);
        tv.setText("mOkHttpClient == mOkHttpClient2 ? " + (mOkHttpClient == mOkHttpClient2) + "\n" +
                mOkHttpClient.toString());
    }
}

这里我们直接拿到 Application 中的 AppComponent,调用方法拿到刚刚定义的 OkHttpSingleComponent,然后就完成注入了,至此我们就完成了单例的 OkHttpClient。
这里为了简单没有给 @Subcomponent 添加 Module,事实上它其他的用法和 @Component 一样。当然在父 Component 中也可以添加多个 Subcomponent。下面看一下第二种实现方法。

方法二:依赖

在说 @Component 的依赖之前,要说两个关于 @Scope 的注意点

  1. 如果 Component 的 Module 中有被 @Scope 标注,则 Component 也需要用相同的 @Scope 标注;
  2. 如果 Component 依赖其他的 OtherComponent 已经被 @Scope 标注,则该 Component 也需要用 @Scope 标注,且不能与被依赖的 OtherComponent 的 @Scope 相同。

如果没有遵循以上两点,编译期间就会报错。
既然已经提到依赖需要不同的 @Scope 我们就先来自定义一个:
仿照 @Singleton 我们定义一个 @PreActivity

@Scope
@Documented
@Retention(RUNTIME)
public @interface Singleton {}
------ 分割线 ------
@Scope
@Documented
@Retention(RUNTIME)
public @interface PerActivity {}

接下来看一下依赖和使用的代码:

@PerActivity
@Component(dependencies = {AppComponent.class})
public interface OkHttpSingle2Component {
    void inject(OkHttpSingleActivity okHttpSingleActivity);
}
------ 分割线 ------
public class OkHttpSingleActivity extends BaseActivity {
    private static final String TAG = OkHttpSingleActivity.class.getSimpleName();
    @Inject
    OkHttpClient mOkHttpClient;
    @Inject
    OkHttpClient mOkHttpClient2;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_okhttp_single);
        //MyApplication.get().getAppComponent().plus().inject(this);//方法1
        DaggerOkHttpSingle2Component.builder().appComponent(MyApplication.get().getAppComponent()).build().inject(this);//方法2
        TextView tvTime = (TextView) findViewById(R.id.tv_time);
        tvTime.setText("time:" + System.currentTimeMillis());
        TextView tv = (TextView) findViewById(R.id.tv);
        tv.setText("mOkHttpClient == mOkHttpClient2 ? " + (mOkHttpClient == mOkHttpClient2) + "\n" +
                mOkHttpClient.toString());
    }
}

我们只要保证无论在哪个 Activity 或者 Fragment 中,我们最终传进去的 AppComponent 都是 MyApplication 中的实例,那么我们就能确保最终的 OkHttpClient 为单例。

关于 Component 的组织

其实上边的例子就是 Component 的组织了,把多个页面不同的 Component 划分开来,通过 @Scope 的两个注意点 来更好的管理 Component 与 Component 以及 Component 与 Module 之间的关系。

Component.png

因为不同 @Scope 标注 Component 与 Module 组织到一起会报错,依赖关系的 Component 不能用相同的 @Scope,体现了 @Scope 的含义。
如果觉得这里没有表达清楚,强烈建议看一下 Android:dagger2 让你爱不释手-重点概念讲解、融合篇 相信会有更深的认识。

最后

如果之前有看过我所了解的 Dagger2(一)再来看这篇文章的同学,看到这里相信对 Dagger2 以及它的使用有了初步的认识。虽然公司的项目中没有用到,但是对我自身来说,阅读有使用 Dagger2 的开源项目也不再会是一头雾水。这两篇文章的举例我尽量做得循序渐进了,如果有写的不对的地方请提醒我,避免误导了后来的同学,如果觉得我整理的还不错请为我点个赞,谢谢!

已经被改的不成样的源码

参考资料

Android:dagger2 让你爱不释手-重点概念讲解、融合篇
Android 常用开源工具(2)-Dagger2 进阶
详解 Dagger2

上一篇 下一篇

猜你喜欢

热点阅读