简体   繁体   English

是否可以在Dagger中混合构造函数和场注入?

[英]Is it possible to mix constructor and field injection in Dagger?

I'm trying to use Dagger for my android project but I encounter a Problem: Is it possible to mix constructor and field injection? 我正在尝试将Dagger用于我的android项目,但我遇到了一个问题:是否可以混合构造函数和字段注入? I have a module and two classes: 我有一个模块和两个类:

@Module(injects = fooActivity.class)
public class Module {
    private fooActivity activity;

    public Module(fooActivity activity) {
        this.activity = activity;
    }

    @Provides
    public bar provideBar() {
        return new bar(activity);
    } 
}

public class fooActivity extends Activity {

    @Inject Baz baz;
    public void onCreate(Bundle savedInstanceState) {
        ...
        ObjectGraph graph = ObjectGraph.create(new BasicModule()).plus(new Module(this));
        graph.inject(this);
    }
}

public class bar {

    @Inject Baz baz;
    @Inject FooActivity activity;
    public bar(FooActivity activity) {
        this.activity = activity;
    }
}

The problem is that 'baz' in the class 'Bar' is always null after the injection. 问题是注入后,“Bar”类中的'baz'始终为null。 It works though, if I don't use a provide method but only use field injection and handle all dependencies in 'Module'. 但它可以工作,如果我不使用提供方法但只使用字段注入并处理'模块'中的所有依赖项。 So from my point of view there are three possibilities: 1) Do field injection only and handle all dependencies in module classes. 因此,从我的观点来看,有三种可能性:1)仅进行字段注入并处理模块类中的所有依赖项。 2) Do constructor injection only and have all dependencies as constructor parameters. 2)仅进行构造函数注入并将所有依赖项作为构造函数参数。 3) Mix both but call .inject(this) on a ObjectGraph-object in the constructor to satisfy fields with @Inject annotation. 3)混合两者但在构造函数中的ObjectGraph对象上调用.inject(this)以满足带@Inject批注的字段。

Is this correct? 这个对吗?

EDIT: I cannot use complete constructor injection because 'baz' is declared in another module. 编辑:我不能使用完整的构造函数注入,因为'baz'在另一个模块中声明。 Editing the example. 编辑示例。

@Module(includes = Module.class)
public class BasicModule {
    @Provides 
    public baz provideBaz() {
        return new baz();
    }
}

I think what you are trying to do is possible. 我想你想做的事情是可能的。 I think you want to add your activity graph to your main application graph in dagger. 我想您想要将活动图添加到匕首的主应用程序图中。

Check out https://github.com/square/dagger/tree/master/examples/android-activity-graphs . 查看https://github.com/square/dagger/tree/master/examples/android-activity-graphs

Also your class bar, you should probably do complete constructor injection. 你的类吧,你应该做完整的构造函数注入。 But if you MUST do it the way you are doing, you need to call inject and register bar in a module. 但是如果必须按照自己的方式进行,则需要在模块中调用注入和注册条。 Here is the way I would create Bar. 这是我创建Bar的方式。

public class Bar {
    private final Activity activity;
    private final Baz baz;

    @Inject public bar(FooActivity activity, Baz baz) {
        this.activity = activity;
        this.baz = baz;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM