首页 > 代码库 > dagger2记录篇
dagger2记录篇
作为一个码农,什么都不用多讲,贴代码
build project
build module
Application
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; }}
AppComponent
@Singleton@Component(modules = { AppModule.class })public interface AppComponent { @ContextLife("Application") App getApplication(); RetrofitHelper retrofitHelper(); //提供http的帮助类}
AppModule
@Modulepublic 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(); }}
ActivityComponent
@ActivityScope@Component(dependencies = AppComponent.class, modules = ActivityModule.class)//关联上下public interface ActivityComponent { Activity getActivity(); void inject(LoginActivity loginActivity);//要在类中使用注入功能 void inject(MainActivity mainActivity);}
ActivityModule
@Modulepublic 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); }}
Base里面使用注入,否则无法注入
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); }}
此时前提条件都有了
点击运行自动生成注入代码(没问题的前提)
使用注入:
public class MainActivity extends PresenterActivity { @Inject protected IPresenterFactory mPresenter;//没有创建对象,inject标签注入类型 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);//注入成功会打印出success } } @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); }}
dagger2记录篇
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。