使用Dagger2

說明

依賴注入

依賴庫

Module

build.gradle

dependencies {
  compile 'com.google.dagger:dagger:2.0.2'
  //Required by Dagger2
  compile 'javax.annotation:javax.annotation-api:1.2'
  or
  compile 'org.glassfish:javax.annotation:10.0-b28' 

  apt 'com.google.dagger:dagger-compiler:2.0.2'
}

Project

build.gradle

dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'    
}

構(gòu)建依賴

@Module public class AppModule { 
   App mApplication; 
   public AppModule(Application application) {    
     mApplication = (App) application;    
  }    
  @Provides    
  App provideApplication() { return mApplication; }    
  @Provides   
  UserCache provideUserCache() { return new UserCacheWithFile(mApplication); }   
  @Provides   
  ECGCache provideECGCache(){ return new ECGCacheWithFile(mApplication); }  
}

構(gòu)建注入器

@Component(modules = AppModule.class)
public interface AppComponent {    
    //exposed to sub graphs    
    //暴露給子圖(設(shè)置 dependencies 的Component),使得子圖可以繼承使用    
    App app();   
    UserCache userCache();   
    ECGCache ecgCache();    
    ECGPrinter ecgPrint();
}
@Component(dependencies = AppComponent.class, modules = ActivityModule.class)
public interface ActivityComponent {    
    //不能使用基類,必須指定具體示例    
    //void inject(BaseActivity activity);    
    void inject(Test4Activity activity);    
    void inject(LoginActivity activity);    
    void inject(HistoryActivity activity);    
    void inject(EcgDetailActivity ecgDetailActivity);
}

ActivityComponent繼承AppComponent的依賴,注入依賴時,會先到指定的Modules尋找,若modules沒有找到需要的依賴關(guān)系,則根據(jù)@Inject標注聲明尋找,若還是沒有,則編譯報錯.

AppComponent的這段代碼

    App app(); 
    UserCache userCache();
    ECGCache ecgCache(); 
    ECGPrinter ecgPrint();

是為繼承了AppComponent 的 ActivityComponent 服務,把這些依賴關(guān)系暴露出來,讓后者可以直接使用.

封裝基類

  • Application
public class App extends Application {    
    private AppComponent mAppComponent;    
    @Override    
    public void onCreate() {        
        super.onCreate();          
        initialize();    
    }      
    private void initialize() {       
        mAppComponent = DaggerAppComponent.builder().appModule(new AppModule(this)).build();    
    }    
    public AppComponent getAppComponent(){        
        return mAppComponent;   
    }
}
  • Activity
public class BaseActivity extends AppCompatActivity {    
    ActivityComponent mActivityComponent;   
    protected ActivityComponent getActivityComponent() {        
        if (mActivityComponent == null) {            
              mActivityComponent = DaggerActivityComponent.builder().appComponent(getAppComponent())                    .activityModule(getActivityModule()).build();        
        }        
        return mActivityComponent;    
    }    
    protected ActivityModule getActivityModule(){
        return new ActivityModule();    
    }    
    protected AppComponent getAppComponent() {        
        return ((App) getApplication()).getAppComponent();    
    }
}
  • Fragment

public class BaseFragment extends Fragment {    
    protected FragmentComponent getFragmentComponent() {       
        return DaggerFragmentComponent.builder().appComponent(getAppComponent()).build();    
    }    
    protected AppComponent getAppComponent() {        
        return ((App) getActivity().getApplication()).getAppComponent();    
    }
}

依賴注入

  • Activity

public class LoginActivity extends BaseActivity implements LoginPresenter.MvpView {
    @Inject
    LoginPresenter loginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //重要! 依賴注入,來自BaseActivity
        getActivityComponent().inject(this);

        loginPresenter.setView(this);
        loginPresenter.initialize();
    }
}
  • Fragment

public class BlankFragment extends BaseFragment {
    @Inject
    ECGCache mECGCache;
    @Inject
    UserCache userCache;

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //重要! 依賴注入,來自BaseFragment
        getFragmentComponent().inject(this);
    }
}
參考
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末厚宰,一起剝皮案震驚了整個濱河市饲握,隨后出現(xiàn)的幾起案子嘱吗,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件脊僚,死亡現(xiàn)場離奇詭異拣宰,居然都是意外死亡,警方通過查閱死者的電腦和手機放典,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門逝变,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人奋构,你說我怎么就攤上這事壳影。” “怎么了弥臼?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵宴咧,是天一觀的道長。 經(jīng)常有香客問我径缅,道長掺栅,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任芥驳,我火速辦了婚禮柿冲,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘兆旬。我一直安慰自己假抄,他們只是感情好,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布丽猬。 她就那樣靜靜地躺著宿饱,像睡著了一般。 火紅的嫁衣襯著肌膚如雪脚祟。 梳的紋絲不亂的頭發(fā)上谬以,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天,我揣著相機與錄音由桌,去河邊找鬼为黎。 笑死,一個胖子當著我的面吹牛行您,可吹牛的內(nèi)容都是我干的铭乾。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼娃循,長吁一口氣:“原來是場噩夢啊……” “哼炕檩!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起捌斧,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤笛质,失蹤者是張志新(化名)和其女友劉穎泉沾,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體妇押,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡跷究,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了舆吮。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片揭朝。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡队贱,死狀恐怖色冀,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情柱嫌,我是刑警寧澤锋恬,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站编丘,受9級特大地震影響与学,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜嘉抓,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一索守、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧抑片,春花似錦卵佛、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至植捎,卻和暖如春衙解,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背焰枢。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工蚓峦, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人济锄。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓暑椰,卻偏偏與公主長得像,于是被迫代替她去往敵國和親拟淮。 傳聞我的和親對象是個殘疾皇子干茉,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

推薦閱讀更多精彩內(nèi)容