简体   繁体   English

字段注入在Dagger2 Android中不起作用

[英]Field Injection not working in dagger2 android

I am trying out my hands on dagger.Till now what i have understood is to inject dependency in one class we need to first dagger how that dependency is created through Modules or Constructor injection.Then use that dependency using Components methods. 我正在尝试使用dagger。到目前为止,我所了解的是将依赖项注入一个类中,我们需要首先使如何通过Modules或Constructor注入来创建该依赖项,然后再使用Components方法使用该依赖项。

Here is the sample code i am trying 这是我正在尝试的示例代码
War class depends on House1 and House2 战争等级取决于House1和House2

War Class 战争级

public class War {

    @Inject House1 house1;
    @Inject House2 house2;

    public void startWar(){
        house1.prepareForWar();
        house1.reportForWar();

        house2.prepareForWar();
        house2.reportForWar();

        System.out.println("House1 is "+house1);
    }
}

House1 Class 1号房

public class House1 implements House {

    @Inject
    public House1() {
    }

    @Override
    public void prepareForWar() {
        System.out.println("House1 Prepared for War");
    }

    @Override
    public void reportForWar() {
        System.out.println("House1 reported for War");
    }
}

House2 Class House2类

public class House2 implements House {

    @Inject
    public House2() {
    }

    @Override
    public void prepareForWar() {
        System.out.println("House2 prepared for war");
    }

    @Override
    public void reportForWar() {
        System.out.println("House2 reported for War");
    }
}

WarModule Class WarModule类

@Module
public class WarModule {

    @Provides
    public War provideWarObject(){
        return new War();
    }
}

Component Class 组件类别

@Component(modules = {WarModule.class})
public interface HouseComponent {
    War getWar();
}

Main Class 主班

public class TempMain {

    public War war;

    public void startWar(){
        war = DaggerHouseComponent.builder().warModule(new WarModule()).build().getWar();
        war.startWar();
    }
}

Here Dagger knows how House1 and House2 are Created using Constructor and War Object through WarModule. 在这里,Dagger知道如何通过WarModule使用构造函数和War对象创建House1和House2。

So In war class when @Inject House1 and @Inject House2 are used dagger should inject it as it knows how to create these dependencies. 因此,在使用@Inject House1和@Inject House2的战争类中,匕首应将其注入,因为它知道如何创建这些依赖项。 But I am getting Null Pointer Exception here 但是我在这里得到空指针异常

Error : Exception in thread "main" java.lang.NullPointerException at com.gosemathraj.dagger2.java.models.war.War.startWar(War.java:14) 错误:com.gosemathraj.dagger2.java.models.war.War.startWar(War.java:14)中的线程“ main”中的异常java.lang.NullPointerException

So In war class when @Inject House1 and @Inject House2 are used dagger should inject it as it knows how to create these dependencies. 因此,在使用@Inject House1和@Inject House2的战争类中,匕首应将其注入,因为它知道如何创建这些依赖项。 But I am getting Null Pointer Exception here 但是我在这里得到空指针异常

Dagger won't do anything because you create the object here: Dagger不会执行任何操作,因为在此处创建了对象:

@Provides
public War provideWarObject(){
    return new War();
}

If you use an @Provides annotated method you need to setup the object before returning it. 如果使用带@Provides注释的方法,则需要先设置对象,然后再返回它。 Dagger will only inject objects that it creates or where you call component.inject(object) . Dagger将仅注入它创建的对象或在您调用component.inject(object)

The simples solution is to remove the @Provides annotate method and add a @Inject public War() {} to your class. 简单的解决方案是删除@Provides注释方法,并向您的类添加@Inject public War() {} Then Dagger can create the object and will inject it afterwards, the same thing you're already doing for your House* classes. 然后Dagger可以创建对象,然后将其注入,这与您为House*类所做的相同。

On an additional note, I don't know why you would use field injection over constructor injection, but you can move your dependencies to the constructor as well, allowing the fields to be private. 另外要说明的是,我不知道为什么要在构造函数注入中使用字段注入,但是您也可以将依赖项移至构造函数,从而允许字段为私有。

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

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