Dagger 2:从两个自定义独立范围注入bean

时间:2015-10-13 19:14:20

标签: android dependency-injection dagger-2

我在Dagger 2中有两个带有自定义范围的对象图。有3个组件:AppComponent和两个子组件ComponentA和ComponentB。

当我试图在一个地方从两个图表中注入对象时,我发现了一个错误:

  

错误:(12,10)错误:today.tarasov.daggerindependentscopes.ClassB不能在没有@Inject构造函数或@ Provide-或@ Produces-annotated方法的情况下提供。   today.tarasov.daggerindependentscopes.MainActivity.classB   [注入类型的字段:today.tarasov.daggerindependentscopes.ClassB classB]

这是我的配置:

零件

@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {

    ComponentA plus(ModuleA moduleA);

    ComponentB plus(ModuleB moduleB);

}

@ScopeA
@Subcomponent(modules = ModuleA.class)
public interface ComponentA {

    void inject(MainActivity mainActivity);

}

@ScopeB
@Subcomponent(modules = ModuleB.class)
public interface ComponentB {

    void inject(MainActivity mainActivity);

}

作用域

@Scope
public @interface ScopeA {
}
@Scope
public @interface ScopeB {
}

模块

@Module
public class AppModule {

    private final App app;

    public AppModule(App app) {
        this.app = app;
    }

    @Provides
    @Singleton
    App provideApp() {
        return app;
    }
}

@Module
public class ModuleA {
    @ScopeA
    @Provides
    ClassA provideClassA() {
        return new ClassA();
    }
}

@Module
public class ModuleB {
    @ScopeB
    @Provides
    ClassB provideClassB() {
        return new ClassB();
    }
}

public class ClassA {
}
public class ClassB {
}

应用

public class App extends Application {

    private static App instance;
    private AppComponent appComponent;
    private ComponentA componentA;
    private ComponentB componentB;

    public static App getInstance() {
        return instance;
    }

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

    public AppComponent getAppComponent() {
        return appComponent;
    }

    public ComponentA getComponentA() {
        return componentA;
    }

    public ComponentB getComponentB() {
        return componentB;
    }

    public ComponentA createComponentA() {
        componentA = appComponent.plus(new ModuleA());
        return componentA;
    }

    public ComponentB createComponentB() {
        componentB = appComponent.plus(new ModuleB());
        return componentB;
    }
}

最后尝试在一个地方注入所有东西

MainActivity

public class MainActivity extends AppCompatActivity {
    @Inject
    ClassA classA;
    @Inject
    ClassB classB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // injecting classA from ScopeA
        App.getInstance().createComponentA().inject(this);
        // injection classB from ScopeB
        App.getInstance().createComponentB().inject(this);

        if (classA == null || classB == null) {
            throw new RuntimeException("Injection failed");
        }
   }
}

我还创建了一个github回购来展示问题。我的问题是:

  • 有可能在Dagger 2中解决这个问题吗?
  • 如果可能,我的配置有什么问题?
  • 如果处理错误,如何处理这种情况?

0 个答案:

没有答案