系列文章第一篇:http://www.jianshu.com/p/33b739bd721a
系列文章第二篇:http://www.jianshu.com/p/46fa16860f28
由于最近一直比较忙(lan),距离上篇已经两个多月时间。我们先对上篇做个回顾:
1:Module - 提供依赖,相当于水源;
2:Component - 负责注入,相当于踩水车,导水管;
3:在Dagger自动生成的DaggerXXXComponent中,会持有其所依赖的Module,根据Module中@Provider注解生成相应的XXXModule_ProviderXXXFactory类。该类实现了Factory接口,Factory继承自Provider接口XXXModule_ProviderXXXFactory重写了get()方法,get()方法中会调用传入的Module中具体的获取实例的方法(该方法必须被@Provider注解)。
1.注入实现:
磨磨唧唧了这么久,终于要讲到最重要的环节-实现注入了。实现注入的方式也很简单,在你要注入的对象紧跟上一行加上@Inject注解。然后build一下项目,注入就完成了。看到这里你一定会心中暗骂:这么简单那你之前磨磨唧唧说了那么多是干啥,浪费感情啊。莫急,我们系列既然是拆解分析dagger注入的原理,那么注入实现是重中之重,我们将会花大篇幅来介绍。
以Activity举例:
下面是一个完整的注入实现过程:
public class OrderConfirmActivity extends BaseMvpActivity<OrderConfirmPresenter>{
}
public class BaseMvpActivity<T extends BasePresenter> extends BasicActivity{
@Inject
T mPresenter;
}
@ActivityScope
@Component(dependencies = AppComponent.class, modules = ActivityModule.class)
public interface ActivityComponent {
void inject(OrderConfirmActivity activity);
}
public final class DaggerActivityComponent implements ActivityComponent {
private Provider<OrderConfirmPresenter> orderConfirmPresenterProvider;
private MembersInjector<OrderConfirmActivity> orderConfirmActivityMembersInjector;
private MembersInjector<BaseMvpActivity<OrderConfirmPresenter>> baseMvpActivityMembersInjector;
private void initialize(final Builder builder) {
this.orderConfirmPresenterProvider = OrderConfirmPresenter_Factory.create(orderConfirmPresenterMembersInjector, retrofitHelperProvider);
this.baseMvpActivityMembersInjector = BaseMvpActivity_MembersInjector.create((MembersInjector) MembersInjectors.noOp(), orderConfirmPresenterProvider);
this.orderConfirmActivityMembersInjector = MembersInjectors.delegatingTo(baseMvpActivityMembersInjector);
}
@Override
public void inject(OrderConfirmActivity activity) {
orderConfirmActivityMembersInjector.injectMembers(activity);
}
}
逐步分析代码:
一:我们有一个BaseMvpActivity,持有相应的Presenter,为了减少耦合,我们想将Presenter直接注入进来,而不是通过实例化,所以我们用@Inject注解我们想注入的Presenter(代码中为OrderConfirmPresenter)。
二:我们将OrderConfimActivity注入到ActivityComponent中。
三:最重要的DaggerActivityComponent类中:
· 1:首先会生成Provider<OrderConfirmPresenter>对象
· 2:将生成的Provider<OrderConfirmPresenter>对象作为参数,创建基类Activity的MemberInjector对象MembersInjector<BaseMvpActivity<OrderConfirmPresenter>>
看一下其具体的过程:
public final class BaseMvpActivity_MembersInjector<T extends BasePresenter> implements MembersInjector<BaseMvpActivity<T>> {
private final MembersInjector<BasicActivity> supertypeInjector;
private final Provider<T> mPresenterProvider;
public BaseMvpActivity_MembersInjector(MembersInjector<BasicActivity> supertypeInjector, Provider<T> mPresenterProvider) {
assert supertypeInjector != null;
this.supertypeInjector = supertypeInjector;
assert mPresenterProvider != null;
this.mPresenterProvider = mPresenterProvider;
}
@Override
public void injectMembers(BaseMvpActivity<T> instance) {
if (instance == null) {
throw new NullPointerException("Cannot inject members into a null reference");
}
supertypeInjector.injectMembers(instance);
instance.mPresenter = mPresenterProvider.get();
}
public static <T extends BasePresenter> MembersInjector<BaseMvpActivity<T>> create(MembersInjector<BasicActivity> supertypeInjector, Provider<T> mPresenterProvider) {
return new BaseMvpActivity_MembersInjector<T>(supertypeInjector, mPresenterProvider);
}
}
可以看到关键代码:instance.mPresenter = mPresenterProvider.get();这时候BaseMvpActivity中的mPresenter已经被赋值实例化了。所以剩下要做的就仅仅是调用injectMembers方法,完成赋值。
·3:生成MembersInjector<OrderConfirmActivity> orderConfirmActivityMembersInjector对象,只是简单的调用了
MembersInjectors.delegatingTo方法将父类MembersInjector转为子类MembersInjector:
public static <T> MembersInjector<T> delegatingTo(MembersInjector<? super T> delegate) {
return (MembersInjector<T>) delegate;
}
```
·4:调用injectMembers实现实例化Presenter:
```
@Override
public void inject(OrderConfirmActivity activity) {
orderConfirmActivityMembersInjector.injectMembers(activity);
}
```
根据第三部我们知道将MembersInjector<BaseMvpActivity<OrderConfirmPresenter>> 转化为 MembersInjector<OrderConfirmActivity> orderConfirmActivityMembersInjector,我们调用子类的injectMembers方法,实际上是调用的父类的injectMembers方法,具体不再赘述。
通过以上分析,已经清晰明了的知道了通过@Inject注解,注入相应的对象到类中的过程。至此,Dagger注入的精髓我们都已经知道怎么实现了。下面会讲一下其余的注解,但不会再对源码进行分析,只是讲解一下怎么使用。
2:@Qualifier:
Qualifier主要用于认证作用,对于同一个对象,但是不同实现方式,以确定使用哪个对象:
```
@Singleton
@Provides
@TestUrl
Retrofit provideTestRetrofit(Retrofit.Builder builder, OkHttpClient client) {
return createRetrofit(builder, client, TestApis.HOST);
}
@Singleton
@Provides
@ProductUrl
Retrofit provideVerRetrofit(Retrofit.Builder builder, OkHttpClient client) {
return createRetrofit(builder, client, ProductApis.HOST);
}
@Singleton
@Provides
ProductApis provideVerService(@ProductUrl Retrofit retrofit) {
return retrofit.create(ProductApis.class);
}
@Singleton
@Provides
TestApis provideTestService(@TestUrl Retrofit retrofit) {
return retrofit.create(TestApis.class);
}
```
简单明了,限定了测试环境和生产环境使用的Retrofit对象。
3:@Scope
限定作用域,一般用于FragemtModule或ActivityModule中所提供对象的作用范围。
至此,Dagger系列结束~~~~。算是对daager使用的一个总结,避免各种编译不过的产生。