dagger2
2015-11-18 13:49:32 0 举报
AI智能生成
为你推荐
查看更多
Dagger2
作者其他创作
大纲/内容
Dagger2详解
依赖注入
控制反转原则
应用流程取决于运行时构建的对象图
通过抽象定义出对象交互使动态流成为可能
运行时绑定的途径有依赖注入或服务加载
依赖注入的诱惑
对象和协作者无需知晓创建及生命周期(依赖注入框架接管)
JSR-330
Java依赖注入
一套类注入的注释标准
重用性最大化
可测性最大化
维护性最大化
实现
Dagger2
Dagger1
Guice
原理
多点注入:依赖注入
多次绑定:提供依赖
多模块:一个功能由一套绑定实现
多个对象图:用一套模块实现一个范围
特点
编译时绑定
用反射来构建依赖图(不用在对象实例化)
运行时处理
问题
效率损耗
调试难
简介
Google在Dagger1上引出
加入google的AutoValue项目
最初想借代码生成来解决问题
进步与代价
好调试跟踪:完整具体的调用栈
高效:google自称13%的效率提升
概念
代码
build.gradle
apply plugin: 'com.neenbedankt.android-apt'\u00A0buildscript {\u00A0 repositories {\u00A0 \u00A0 jcenter()\u00A0 }\u00A0 dependencies {\u00A0 \u00A0 classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'\u00A0 }}\u00A0android {\u00A0 ...}\u00A0dependencies {\u00A0 apt 'com.google.dagger:dagger-compiler:2.0'\u00A0 compile 'com.google.dagger:dagger:2.0'\u00A0\u00A0\u00A0 ...}
初始化对象
本类需要知道太多对象创建和提供依赖的边角代码
依赖注入的方案
依赖图:Application Component
Application Module @Provide @Singleton
Activity Component
Activity Module @Provide @PerActivity
User Component
User Module @Provide @PerActivity
详解
@Singleton // Constraints this component to one-per-application or unscoped bindings.@Component(modules = ApplicationModule.class)public interface ApplicationComponent {\u00A0 void inject(BaseActivity baseActivity);\u00A0\u00A0 //Exposed to sub-graphs.\u00A0 Context context();\u00A0 ThreadExecutor threadExecutor();\u00A0 PostExecutionThread postExecutionThread();\u00A0 UserRepository userRepository();}
@Modulepublic class ApplicationModule {\u00A0 private final AndroidApplication application;\u00A0\u00A0 public ApplicationModule(AndroidApplication application) {\u00A0 \u00A0 this.application = application;\u00A0 }\u00A0\u00A0 @Provides @Singleton Context provideApplicationContext() {\u00A0 \u00A0 return this.application;\u00A0 }\u00A0\u00A0 @Provides @Singleton Navigator provideNavigator() {\u00A0 \u00A0 return new Navigator();\u00A0 }\u00A0\u00A0 @Provides @Singleton ThreadExecutor provideThreadExecutor(JobExecutor jobExecutor) {\u00A0 \u00A0 return jobExecutor;\u00A0 }\u00A0\u00A0 @Provides @Singleton PostExecutionThread providePostExecutionThread(UIThread uiThread) {\u00A0 \u00A0 return uiThread;\u00A0 }\u00A0\u00A0 @Provides @Singleton UserCache provideUserCache(UserCacheImpl userCache) {\u00A0 \u00A0 return userCache;\u00A0 }\u00A0\u00A0 @Provides @Singleton UserRepository provideUserRepository(UserDataRepository userDataRepository) {\u00A0 \u00A0 return userDataRepository;\u00A0 }}
@Scope@Retention(RUNTIME)public @interface PerActivity {}
能activity对象构建是注入
可在Activity范围内使用singleton模式
Activity Module: 暴露activity给依赖图
@Modulepublic class ActivityModule {\u00A0 private final Activity activity;\u00A0\u00A0 public ActivityModule(Activity activity) {\u00A0 \u00A0 this.activity = activity;\u00A0 }\u00A0\u00A0 @Provides @PerActivity Activity activity() {\u00A0 \u00A0 return this.activity;\u00A0 }}
使用activity作为context的fragment
User Component: 扩展自AcitvityComponent
User Module:用户相关的协作对象
@Modulepublic class UserModule {\u00A0 @Provides @PerActivity GetUserListUseCase provideGetUserListUseCase(GetUserListUseCaseImpl getUserListUseCase) {\u00A0 \u00A0 return getUserListUseCase;\u00A0 }\u00A0\u00A0 @Provides @PerActivity GetUserDetailsUseCase provideGetUserDetailsUseCase(GetUserDetailsUseCaseImpl getUserDetailsUseCase) {\u00A0 \u00A0 return getUserDetailsUseCase;\u00A0 }}
注入依赖及绑定依赖顺序
构造函数注入:类的构造函数前加@Inject
成员对象注入:非私有成员对象前加@Inject
方法注入:方法前加@Inject
实例
BaseActivity:Navigator
public abstract class BaseActivity extends Activity {\u00A0\u00A0 @Inject Navigator navigator;\u00A0\u00A0 @Override\u00A0 protected void onCreate(Bundle savedInstanceState) {\u00A0 \u00A0 super.onCreate(savedInstanceState);\u00A0 \u00A0 this.getApplicationComponent().inject(this);\u00A0 }\u00A0\u00A0 protected ApplicationComponent getApplicationComponent() {\u00A0 \u00A0 return ((AndroidApplication)getApplication()).getApplicationComponent();\u00A0 }\u00A0\u00A0 protected ActivityModule getActivityModule() {\u00A0 \u00A0 return new ActivityModule(this);\u00A0 }}
onCreate里调用getApplicationComponent()后inject注入
Application对象初始化ApplicationComponent
Activity-Fragment
private UserComponent userComponent;private void initializeInjector() {\u00A0 this.userComponent = DaggerUserComponent.builder()\u00A0 \u00A0 \u00A0 .applicationComponent(getApplicationComponent())\u00A0 \u00A0 \u00A0 .activityModule(getActivityModule())\u00A0 \u00A0 \u00A0 .build();}
@Override public UserComponent getComponent() {\u00A0 return userComponent;}
@Override public void onActivityCreated(Bundle savedInstanceState) {\u00A0 super.onActivityCreated(savedInstanceState);\u00A0 this.getComponent.inject(this);}
Fragment里调用Component的inject传入fragment参数
HasComponent接口类
public interface HasComponent {\u00A0 C getComponent();}
BaseFragment类
@SuppressWarnings(\"unchecked\")protected C getComponent(Class componentType) {\u00A0 return componentType.cast(((HasComponent)getActivity()).getComponent());}
Dagger2生成代码
DaggerApplicationComponent实现ApplicationComponent
0 条评论
回复 删除
下一页