本文假设读者已经有必定Dagger2使用经验html
以前工做中一直在使用dagger2
进行开发,用起来确实很爽,可是我从我第一次使用我就一直有一个问题或者说疑问(本人才疏学浅脑子不够使),一般状况下咱们有以下清单android
MyApplication
,MyAppComponent
,MyAppModule
ActActivity
,ActComponent
,ActModule
git
简单解释下,MyAppModule
提供全局单例功能,好比打印日志,ActModule
提供Activity
级别的功能好比发起网络请求(只是举个栗子),如今咱们但愿在发起网络请求的时候打印日志,那么解决方法也很简单——SubComponent
或者Component(dependencies=X.class)
github
因而咱们首先在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
来提供注入功能,代码看上去就像以下...ide
MyAppComponent.getInstance() .plus() .inject(this);
为神马我使用的明明是ActComponent
,关MyAppComponent
什么事?(我最开始学习使用dagger2
的时候彻底没法接受这种写法),并且这彷佛不太符合依赖注入的一个根本原则a class shouldn’t know anything about how it is injected.
学习
谷歌爸爸很明显也注意到了这个问题,谁叫Dagger2
在Android
开发中也那么火呢,因而在Dagger2
新版本中咱们有了一个新东西dagger.android
ui
Gradle引入方式this
//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...google
结合Demo和官方文档粗略翻译以下
在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/dagg...
https://android.jlelse.eu/and...