dagger2是目前android端比較火的一款依賴注入框架,先來看下基本的用法吧:
首先提供module,類似于工廠:
@Module
public class ApiServiceModule {
private static final String ENDPOINT = "";
@Singleton
@Provides
public OkHttpClient providerOkHttpClient(){
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setConnectTimeout(60 * 1000, TimeUnit.MILLISECONDS);
okHttpClient.setReadTimeout(60 * 1000, TimeUnit.MILLISECONDS);
return okHttpClient;
}
@Singleton
@Provides
public RestAdapter providerRestAdapter(OkHttpClient okHttpClient){
RestAdapter.Builder builder = new RestAdapter.Builder();
builder.setClient(new OkClient(okHttpClient))
.setEndpoint(ENDPOINT);
return builder.build();
}
@Singleton
@Provides
public ApiService providerApiService(RestAdapter adapter){
return adapter.create(ApiService.class);
}
}
然后是component組件户侥,用來連接module與需求方:
@Singleton
@Component(modules = {AppModule.class , ApiServiceModule.class , AppServiceModule.class})
public interface AppComponent {
Application getApplication();
ApiService getService();
User getUser();
}
在activity中使用:
public class MainActivity extends BaseActivity {
private TextView tvName;
@Inject
MainPresenter mMainPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvName = findViewById(R.id.tvName);
mMainPresenter.showUserName();
}
@Override
protected void setupActivityComponent(AppComponent appComponent) {
DaggerMainActivityComponent.builder()
.appComponent(appComponent)
.mainActivityModule(new MainActivityModule(this))
.build().inject(this);
}
public void setTextName(String name){
tvName.setText(name);
}
}
使用是非常簡單的剔猿,下面來看一下dagger2是如何做到依賴注入的吧:
首先會(huì)調(diào)到 DaggerMainActivityComponent.builder()
,我們會(huì)想到構(gòu)建者模式爽撒,一起看下源碼
public static Builder builder() {
return new Builder();
}
Builder的構(gòu)造函數(shù)中沒有做任何處理入蛆,接下來看下.build方法:
public MainActivityComponent build() {
if (mainActivityModule == null) {
throw new IllegalStateException(
MainActivityModule.class.getCanonicalName() + " must be set");
}
if (appComponent == null) {
throw new IllegalStateException(AppComponent.class.getCanonicalName() + " must be set");
}
return new DaggerMainActivityComponent(this);
}
new DaggerMainActivityComponent(this)
會(huì)初始化component,然后將builder傳遞進(jìn)去硕勿,這也是建造者模式的一般寫法哨毁,下面看下其中的構(gòu)造函數(shù)會(huì)執(zhí)行哪些方法呢
private void initialize(final Builder builder) {
this.providerMainActivityProvider =
DoubleCheck.provider(
MainActivityModule_ProviderMainActivityFactory.create(builder.mainActivityModule));
this.getUserProvider =
new com_winning_mvp_dagger2_retrofit_master_di_component_AppComponent_getUser(
builder.appComponent);
this.providerMainPresenterProvider =
DoubleCheck.provider(
MainActivityModule_ProviderMainPresenterFactory.create(
builder.mainActivityModule, providerMainActivityProvider, getUserProvider));
}
下面我們來單獨(dú)看一下這句
this.providerMainActivityProvider =
DoubleCheck.provider(
MainActivityModule_ProviderMainActivityFactory.create(builder.mainActivityModule));
走到MainActivityModule_ProviderMainActivityFactory中看一下:
public final class MainActivityModule_ProviderMainActivityFactory implements Factory<MainActivity> {
private final MainActivityModule module;
public MainActivityModule_ProviderMainActivityFactory(MainActivityModule module) {
this.module = module;
}
@Override
public MainActivity get() {
return provideInstance(module);
}
public static MainActivity provideInstance(MainActivityModule module) {
return proxyProviderMainActivity(module);
}
public static MainActivityModule_ProviderMainActivityFactory create(MainActivityModule module) {
return new MainActivityModule_ProviderMainActivityFactory(module);
}
public static MainActivity proxyProviderMainActivity(MainActivityModule instance) {
return Preconditions.checkNotNull(
instance.providerMainActivity(),
"Cannot return null from a non-@Nullable @Provides method");
}
}
現(xiàn)在component中的屬性都是有值的了,最后看下inject做了哪些操作:
public static void injectMMainPresenter(MainActivity instance, MainPresenter mMainPresenter) {
instance.mMainPresenter = mMainPresenter;
}
到這里依賴注入就已經(jīng)完成了源武,這里只是分析了最簡單的注入方式扼褪。