详解Dagger2在Android开发中的新用法

作者:荔枝我大哥 时间:2021-08-23 22:39:40 

本文假设读者已经有一定Dagger2使用经验

使用疑惑

之前工作中一直在使用dagger2进行开发,用起来确实很爽,但是我从我第一次使用我就一直有一个问题或者说疑问(本人才疏学浅脑子不够使),通常情况下我们有如下清单

MyApplication,MyAppComponent,MyAppModule
ActActivity,ActComponent,ActModule

简单解释下,MyAppModule提供全局单例功能,比如打印日志,ActModule提供Activity级别的功能比如发起网络请求(只是举个栗子),现在我们希望在发起网络请求的时候打印日志,那么解决方法也很简单——SubComponent或者Component(dependencies=X.class)

于是我们首先在MyApplication中初始化MyAppcomponent(使用抽象类实现单例)


@Component(modules = MyAppModule.class)
public abstract class MyAppComponent {
......
//使用SubComponent功能来完成component的组合
abstract ActComponent plus();
}

@Subcomponent(modules = ActModule.class)
public interface ActComponent {
void inject(ActActivity act);
}

public class MyApplication extends Application {
@Override
public void onCreate() {
 super.onCreate();
 MyAppComponent.getInstance().inject(this);
}
}

然后就是就在Activity中使用ActComponent来提供注入功能,代码看上去就像如下...


 MyAppComponent.getInstance()
   .plus()
   .inject(this);

为神马我使用的明明是ActComponent,关MyAppComponent什么事?(我最开始学习使用dagger2的时候完全无法接受这种写法),而且这似乎不太符合依赖注入的一个根本原则a class shouldn't know anything about how it is injected.

新用法

谷歌爸爸很明显也注意到了这个问题,谁叫Dagger2在Android开发中也那么火呢,于是在Dagger2新版本中我们有了一个新东西dagger.android

Gradle引入方式


//dagger2
compile 'com.google.dagger:dagger:2.11'
compile 'com.google.dagger:dagger-android:2.11'
compile 'com.google.dagger:dagger-android-support:2.11'
annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'

Demo地址在 https://github.com/hanliuxin5/Dagger2-demo

结合Demo和官方文档粗略翻译如下

1、在AppComponent中安装AndroidInjectionModule


@Component(modules = {AndroidInjectionModule.class})
public interface AppComponent {
//....
}

2.编写实现了AndroidInjector<YourActivity>的Lychee3Activity


@Subcomponent(modules = ...)
public interface ActSubComponent extends AndroidInjector<Lychee3Activity> {
@Subcomponent.Builder
public abstract class Builder extends AndroidInjector.Builder<Lychee3Activity> {
}
}

3.定义了ActSubComponent后,将其安装在绑定了ActSubComponent.Builder的Module中,并且将该Module安装在我们的AppComponent中


@Module(subcomponents = {ActSubComponent.class})
public abstract class BuildersModule {
@Binds
@IntoMap
@ActivityKey(Lychee3Activity.class)
abstract AndroidInjector.Factory<? extends Activity> lychee3Activity(ActSubComponent.Builder builder);
}

@Component(modules = {AndroidInjectionModule.class,
 BuildersModule.class})
public interface AppComponent {
//....
}

但是如果你的ActSubComponent若同我们在步骤2中定义的一样,不管在类中还是在其Builder中没有的方法和超类型,你可以用下面的代码跳过2,3步骤

原文 Pro-tip: If your subcomponent and its builder have no other methods or supertypes than the ones mentioned in step #2, you can use @ContributesAndroidInjector to generate them for you


@ContributesAndroidInjector
abstract Lychee2Activity lychee2Activity();

4.让你的MyApplication实现HasActivityInjector,并且注入DispatchingAndroidInjector,


public class MyApplication extends Application implements HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;

@Override
public void onCreate() {
 super.onCreate();
   DaggerAppComponent.builder().AppContent(this).build().inject(this);//最好结合demo来看,不然AppContent是啥你不知道
}

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

5.最后,在你Lychee3Activity和Lychee2Activity中的onCreate中,调super.onCreate()之前调用AndroidInjection.inject(this);


public class Lychee2Activity extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
AndroidInjection.inject(this);
super.onCreate(savedInstanceState);
}
}

至此,新东西的使用差不多就到这了,但是为什么我会有一种“天,怎么越来越复杂啦”的感觉呢...

参考文章

https://google.github.io/dagger//android.html

https://android.jlelse.eu/android-and-dagger-2-10-androidinjector-5e9c523679a3

来源:https://segmentfault.com/a/1190000010016618?utm_source=tuicool&utm_medium=referral

标签:Android,dagger2
0
投稿

猜你喜欢

  • java利用Future实现多线程执行与结果聚合实例代码

    2023-09-24 11:07:18
  • SpringBoot设置编码UTF-8的两种方法

    2022-05-04 00:09:08
  • linux系统 java环境变量的配置方法

    2022-12-10 09:34:11
  • 关于easyExcel中读取Excel表头的实例说明

    2023-10-11 17:48:43
  • java中的类为什么只能用public修饰?

    2023-10-09 20:23:54
  • java中的HashMap多层嵌套

    2023-11-27 07:34:52
  • java中删除 数组中的指定元素方法

    2023-02-02 12:45:59
  • Java日常练习题,每天进步一点点(61)

    2021-07-17 06:56:13
  • c# 字符串操作总结

    2022-03-16 05:59:25
  • Spark JDBC操作MySQL方式详细讲解

    2021-05-24 12:41:51
  • 基于@PathVariable注解的用法说明

    2023-10-01 17:22:55
  • Mybatis @SelectKey用法解读

    2022-04-18 18:01:35
  • Android实现简单卡片布局

    2023-05-22 11:43:37
  • 命令提示符编译java的方法(必看篇)

    2022-01-28 08:34:38
  • JAVA基础之注解与反射的使用方法和场景

    2022-02-23 19:50:00
  • Java四种权限修饰符知识点详解

    2023-11-11 06:12:59
  • springBoot整合rabbitMQ的方法详解

    2022-08-19 02:28:33
  • 为什么阿里要慎重使用ArrayList中的subList方法

    2023-12-07 19:03:34
  • 解读JSONArray删除元素的两种方式

    2022-10-31 11:40:50
  • java中LinkedBlockingQueue与ArrayBlockingQueue的异同

    2023-08-19 02:56:49
  • asp之家 软件编程 m.aspxhome.com