參考地址 如下:
https://blog.csdn.net/soslinken/article/details/70231089
/**
* Component跟SubComponent的區(qū)別
* 1.方式一:如果全部都是使用Component 然后使用dependence屬性來依賴====子組件為主角 父組件暴露方法給子組件注入到容器
* 邏輯分析:
* 第一步:那就是父組件需要暴露 容器(Activity/Fragment)所需要的實例
* daggger里面會首先執(zhí)行以下代碼:===目標是先生成父組件的實例
* 父組件寫法如下:
* @Component(modules={AppModule.class})
* public interface AppComponent {
*//將AppModule中的SomeClassA1暴露出來,以便于其他依賴于AppComponent的Component調(diào)用
* SomeClassA1 someClassA1();
*//UserDaggerPresenterComponent依賴相當于繼承AppComponent 因為子組件 需要一個IBasePresenter 而子組件所對應
* 的module并沒有提供 如果需要父組件提供 那么父組件需要暴露一個Presenter
* IBasePresenter getPresenter();
*}
* Activity生成代碼如下:
* ApplicationModule applicationModule=new ApplicationModule(this);
* DaggerApplicationComponent.builder().applicationModule(applicationModule).build(); //生成父組件實例
* 第二步:生成子類的實例
* UserDaggerPresenterSubComponent userDaggerPresenterSubComponent=UserDaggerPresenterSubComponent.builder().appComponent(appComponent).studentModule(new StudentModule()).build().inject(this)
* 2. 方式二:如果是使用Component跟SubComponent的結(jié)合方式====這種方式父組件為主角 父組件添加 子組件做注入到容器里面
* 邏輯分析:
* 父組件添加一個返回子組件的實例
* UserDaggerPresenterSubComponent addSub(UserDaggerSubPresenterModule module);
* 子組件注入方法
* void inject(UserSubDaggerActivity activity);
*/