android - 来自多个组件的现场注入(inject)

标签 android dagger-2

在 Dagger2 中是否可以像下面这样从两个模块注入(inject)?

public class MyActivity extends Activity {

  @Inject ProvidedByOne one;
  @Inject ProvidedByTwo two;

  public void onCreate(Bundle savedInstance) {
        ((App) getApplication()).getOneComponent().inject(this);
        ((App) getApplication()).getSecondComponent().inject(this);
    } 
}

我有两个独立的模块,无法使其工作。我有错误:

Error:(16, 10) error: com.test.dagger.module.TwoModule.Two cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. com.test.activity.MoreActivity.two [injected field of type: com.test.dagger.module.TwoModule.Two two]

Error:(16, 10) error: com.test.dagger.module.OneModule.One cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. com.test.activity.MoreActivity.one [injected field of type: com.test.dagger.module.OneModule.One one]

public class MoreActivity extends SingleFragmentActivity {

    @Inject OneModule.One one;
    @Inject TwoModule.Two two;

    @Override
    protected Fragment createFragment() {

        ((App)getApplication()).getOneComponent().inject(this);
        ((App)getApplication()).getTwoComponent().inject(this);

        return SimpleFragment.newInstance(MoreActivity.class.getSimpleName());
    }
}

@Module
public class OneModule {
    public class One {

    }

    @Provides
    @Singleton
    One provideOne() {
        return new One();
    }
}

@Module
public class TwoModule {

    public class Two {

    }

    @Provides
    @Singleton
    Two provideTwo() {
        return new Two();
    }
}

@Singleton
@Component(modules = OneModule.class)
public interface OneComponent {
    void inject(MoreActivity activity);
}

@Singleton
@Component(modules = TwoModule.class)
public interface TwoComponent {
    void inject(MoreActivity activity);
}

最佳答案

不,要使用字段注入(inject),您的组件需要能够提供所有用 @Inject 标记的依赖项。

如果您想在每个类中使用多个组件,您可以使用配置方法手动设置字段。

@Module
public class OneModule {
    public class One {

    }

    @Provides
    @Singleton
    One provideOne() {
        return new One();
    }
}

@Module
public class TwoModule {

    public class Two {

    }

    @Provides
    @Singleton
    Two provideTwo() {
        return new Two();
    }
}

@Singleton
@Component(modules = OneModule.class)
public interface OneComponent {
    OneModule.One one();
}

@Singleton
@Component(modules = TwoModule.class)
public interface TwoComponent {
    TwoModule.Two two();
}

public class MoreActivity extends SingleFragmentActivity {

    OneModule.One one;
    TwoModule.Two two;

    @Override
    protected Fragment createFragment() {

        one = ((App)getApplication()).getOneComponent().one();
        two = ((App)getApplication()).getTwoComponent().two();

        return SimpleFragment.newInstance(MoreActivity.class.getSimpleName());
    }
}

关于android - 来自多个组件的现场注入(inject),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38450765/

相关文章:

android - AlarmManager 在多个设备中不起作用

android - 如何将自定义按钮添加到 AlertDialog 布局中?

java - 什么决定了 Dagger 2 中组件(对象图)的生命周期?

android - Espresso、Dagger2 在 BaseActivity 上设置 ViemodelProvider.Factory

android - Android 中的 Dalvik 格式化失败错误

android - FileNotFoundException AndroidManifest.xml

android - 如果子组件与其中一个父组件存在冲突,Dagger 不会抛出异常

android - Dagger 2 错误子组件可能未引用作用域绑定(bind)

java - Dagger2 + gradle + intellij : generated classes are not found and treated as a compile error/won't autocomplete

android - 如何使用 sentry-cli 设置 Sentry.io 的默认组织