您的足迹:首页 > Android >Android Dagger2 笔记2 Component依赖另一个Component

Android Dagger2 笔记2 Component依赖另一个Component

当我们其中一个Component跟另外一个Component所提供的依赖有重复的时候,我们没有必要完全再写一遍,一个Component是可以依赖另外一个依赖的。 比如AppModule里面提供Context,那么我们的LoginModule里面就可以不需要provideContext,LoginModule可以依赖AppModule来获取context,如:
@Module
public class AppModule {

    Context context;

    public AppModule(Context context){
        this.context=context;
    }

    @Provides
    Context provideContext(){
        AWLogUtils.e("--- provideContext---");
        return this.context;
    }
}
@Component(modules = {AppModule.class})
public interface AppComponent {
    // 向下层提供Context
    Context getContext();
}
@Singleton
@Component(modules = {LoginModule.class},dependencies = {AppModule.class})
public interface LoginComponent {
    void inject(LoginActivity loginActivity);
}
@Module
public class LoginModule {

    @Singleton
    @Provides
    LocationInfo provideLocationInfo(Context context){
        LocationInfo locationInfo=new LocationInfo(context);
        AWLogUtils.e("--- provideLocationInfo---:"+locationInfo);
        return locationInfo;
    }
}
public class App extends Application {

    AppComponent appComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        appComponent=DaggerAppComponent.builder().appModule(new AppModule(this)).build();
    }

    public AppComponent getAppComponent() {
        return appComponent;
    }
}
public class App extends Application {
public class LoginActivity extends AppCompatActivity {
    @Inject
    UserInfo userInfo;

    @Inject
    UserInfo userInfo2;

    @Inject
    LocationInfo locationInfo;

    @Inject
    LocationInfo locationInfo2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);

        App app= (App) getApplication();

        DaggerLoginComponent.builder().appComponent(app.getAppComponent()).loginModule(new LoginModule()).build().inject(this);

        AWLogUtils.e(" userInfo:"+userInfo);
        AWLogUtils.e(" userInfo2:"+userInfo2);
        AWLogUtils.e(" locationInfo:"+locationInfo);
        AWLogUtils.e(" locationInfo2:"+locationInfo2);
    }
}
}
运行结果:
02-28 08:34:14.961 E/AWLogUtils:  --- UserInfo created:com.a1anwang.chihuo.entity.UserInfo@5d430ae
02-28 08:34:14.961 E/AWLogUtils: --- provideContext---
02-28 08:34:14.961 E/AWLogUtils:  ---create LocationInfo:com.a1anwang.chihuo.entity.LocationInfo@6c1ee4f
02-28 08:34:14.961 E/AWLogUtils: --- provideLocationInfo---:com.a1anwang.chihuo.entity.LocationInfo@6c1ee4f
02-28 08:34:14.961 E/AWLogUtils:  userInfo:com.a1anwang.chihuo.entity.UserInfo@5d430ae
02-28 08:34:14.961 E/AWLogUtils:  userInfo2:com.a1anwang.chihuo.entity.UserInfo@5d430ae
02-28 08:34:14.961 E/AWLogUtils:  locationInfo:com.a1anwang.chihuo.entity.LocationInfo@6c1ee4f
02-28 08:34:14.961 E/AWLogUtils:  locationInfo2:com.a1anwang.chihuo.entity.LocationInfo@6c1ee4f
上面AppComponent里面必须提供 返回Context的方法 如getContext(),否则在注入LocationInfo的时候会报错:无法注入Context
本博客所有文章如无特别注明均为原创。作者:AlanWang复制或转载请以超链接形式注明转自 AlanWang的博客-专注android和蓝牙BLE技术分享
原文地址《Android Dagger2 笔记2 Component依赖另一个Component

相关推荐

发表评论

路人甲 表情
看不清楚?点图切换 Ctrl+Enter快速提交

网友评论(0)