说明
依赖注入
依赖库
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'
}
构建依赖
@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); }
}
构建注入器
@Component(modules = AppModule.class)
public interface AppComponent {
//exposed to sub graphs
//暴露给子图(设置 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没有找到需要的依赖关系,则根据@Inject标注声明寻找,若还是没有,则编译报错.
AppComponent的这段代码
App app();
UserCache userCache();
ECGCache ecgCache();
ECGPrinter ecgPrint();
是为继承了AppComponent 的 ActivityComponent 服务,把这些依赖关系暴露出来,让后者可以直接使用.
封装基类
-
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);
}
}
参考
- https://github.com/hitherejoe/Android-Boilerplate
- https://github.com/android10/Android-CleanArchitecture
- http://codethink.me/2015/08/06/dependency-injection-with-dagger-2/
- http://www.cnblogs.com/tiantianbyconan/p/5095426.html
- http://www.open-open.com/lib/view/open1438910855489.html
- http://www.jianshu.com/p/c2feb21064bb
网友评论