編輯:關於 Android 開發
作為搬磚的,廢話不多說,本文以代碼為主記錄 Dagger2 的完整搭建過程。
先在 Project 與 Module 兩級 build.gradle 中配置依賴(Dagger 需要 apt 註解處理器支持):




自定義 Application 中持有 AppComponent:
public class App extends Application {
private AppComponent appComponent;
@Override
public void onCreate() {
super.onCreate();
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.build();
}
public AppComponent getAppComponent() {
return appComponent;
}
}
用 @Singleton 標註作用域,聲明依賴的 Module 並暴露全局對象:
@Singleton
@Component(modules = { AppModule.class })
public interface AppComponent {
@ContextLife("Application") App getApplication();
RetrofitHelper retrofitHelper(); // 提供 http 的幫助類
}
負責提供全域依賴實例:
@Module
public class AppModule {
private App application;
public AppModule(App application) {
this.application = application;
}
@Provides
@Singleton
@ContextLife("Application")
public App provideApplication() {
return application;
}
@Provides
@Singleton
public RetrofitHelper provideRetrofitHelper() {
return new RetrofitHelper();
}
}
Activity 作用域的 Component 通過 dependencies 關聯 AppComponent,這樣就能直接使用 App 層提供的依賴:
@ActivityScope
@Component(dependencies = AppComponent.class, modules = ActivityModule.class)
public interface ActivityComponent {
Activity getActivity();
void inject(LoginActivity loginActivity); // 聲明需要注入的目標
void inject(MainActivity mainActivity);
}
@Module
public class ActivityModule {
private Activity mActivity;
public ActivityModule(Activity activity) {
this.mActivity = activity;
}
@Provides
@ActivityScope
public Activity provideActivity() {
return mActivity;
}
@Provides
@ActivityScope
public IPresenterFactory providePresenter(RetrofitHelper retrofitHelper) {
return new PresenterFactory(retrofitHelper);
}
}
把注入邏輯沉澱到基類,子類只需實現 initInject(),避免每個頁面重複寫:
public abstract class PresenterActivity extends BaseActivity {
private final static String TAG = "PresenterActivity";
protected abstract void initInject();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initInject();
}
protected ActivityComponent getActivityComponent() {
return DaggerActivityComponent.builder()
.appComponent(App.getInstance().getAppComponent())
.activityModule(getActivityModule())
.build();
}
private ActivityModule getActivityModule() {
return new ActivityModule(this);
}
}
前期準備齊全後,點擊 Rebuild 讓 Dagger 自動生成注入代碼(前提是編譯無錯),然後在頁面裡用 @Inject 標註字段即可:
public class MainActivity extends PresenterActivity {
@Inject
protected IPresenterFactory mPresenter; // 無需 new,由 Dagger 注入
private final int BASIC_PERMISSION_REQUEST_CODE = 100;
private TextView username;
@Override
public void findView() {
username = findView(R.id.username);
username.setText(App.getInstance().getUserEntity().getName() + " hello");
}
@Override
public void init() {
if (mPresenter != null) {
Toasts.showShort("mPresenter success", context);
}
}
@Override
public int layoutResID() {
return R.layout.activity_main;
}
@Override
protected void initInject() {
getActivityComponent().inject(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onEvent() {
Toasts.showShort("刷新UI", context);
}
}
1、Component 中必須聲明 inject 方法,否則 Dagger 不會為該類生成注入代碼,字段永遠為 null。
2、作用域註解要成對使用,@Singleton 與 @ActivityScope 只是標記依賴的生命週期,漏標不會報錯,但會導致同一對象被重複創建。
3、依賴必須通過 @Provides 提供,如果某個類型既沒有 @Inject 構造函數也沒有 @Provides 方法,編譯期就會直接報錯——這正是 Dagger 相比反射框架的優勢:問題在編譯時暴露,而不是運行時崩潰。
一次伺服器流量異常的排查實錄
記錄一次伺服器異常外發流量的排查過程 前兩天接到一位做開發的朋友來電,說他們客戶的一台伺服器開機後,整個網段的機器上網都變慢了,連他遠程操作這台伺服器也一卡一卡的。我的第
Fragment 開發實戰(一):Fragment 是什麼,又該怎麼用
Fragment 可以想成「一段模組化的 Activity」:它有自己的生命週期、會接收自己的輸入事件,也能在 Activity 執行期間被動態加入或移除。它通常作為宿主
Android 基礎入門教程——10.14 Android GPS 初涉
Android 基礎入門教程——10.14 Android GPS 初涉 1. 定位相關的一些 API 1)LocationManager 官方 API 文檔:Loca
Android App Widget 入門:把資訊小工具放上桌面
Widget 是桌面小工具的總稱,而 Android 的 App Widget 則是可放在桌面上的應用程式小元件。它能讓使用者不必開啟主應用,就能快速看到資訊或進行操作。