android - Dagger 2注入(inject)构造函数的参数

标签 android dagger-2

我在 Dagger 2 website 上看到了以下示例:

class Thermosiphon implements Pump {
  private final Heater heater;

  @Inject
  Thermosiphon(Heater heater) {
    this.heater = heater;
  }

  ...
}

和文档:

When a new instance is requested, Dagger will obtain the required parameters values and invoke this constructor.

当我编写一个模块来提供 Thermosiphon 之类的

@Module
public class ThermosiphonModule {

    @Provides
    @Singleton
    Thermosiphon provideThermosiphon() {
        return new Thermosiphon(???);
    }

}

Thermosiphon 构造函数仍然需要一个 Heater 作为参数,从而使整个“构造函数依赖项的自动注入(inject)”变得毫无用处。

我试过了

return new Thermosiphon(null); 

return new Thermosiphon(); 

(空构造函数)并希望 Dagger2 能够发现我希望注入(inject)丢失的 Heater,但提供的 Thermosiphon 的 Heater 始终为空;

我确认我的 HeaterComponent/HeaterModule 工作正常并且能够提供 Heater

我是否完全误解了“Dagger 为您满足构造函数依赖项”的整个功能,还是我遗漏了什么?

最佳答案

如果您正在使用模块,那么如果您有两个提供程序模块绑定(bind)到同一个组件,那么您将能够允许它们将加热器视为构造函数参数。

@Module
public class HeaterModule {
    @Provides
    @Singleton
    Heater heater() {
        return new Heater(); // if not using @Inject constructor
    }
}

@Module
public class ThermosiphonModule {
    @Provides
    @Singleton
    Thermosiphon thermosiphon(Heater heater) {
        return new Thermosiphon(heater); // if not using @Inject constructor
    }
}

@Singleton
@Component(modules={ThermosiphonModule.class, HeaterModule.class})
public interface SingletonComponent {
    Thermosiphon thermosiphon();
    Heater heater();

    void inject(Something something);
}

public class CustomApplication extends Application {
    private SingletonComponent singletonComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        this.singletonComponent = DaggerSingletonComponent.builder().build(); //.create();
    }

    public SingletonComponent getSingletonComponent() {
        return singletonComponent;
    }
}

但是通过构造函数注入(inject),您还可以提供给定范围的对象或无范围的对象,只要它们具有 @Inject 构造函数。

例如,

@Singleton
@Component // no modules
public interface SingletonComponent {
    Thermosiphon thermosiphon();
    Heater heater();

    void inject(Something something);
}

@Singleton
public class Heater {
    @Inject
    public Heater() {
    }
}

@Singleton
public class Thermosiphon {
    private Heater heater;

    @Inject
    public Thermosiphon(Heater heater) {
        this.heater = heater;
    }
}

或者

@Singleton
public class Thermosiphon {
    @Inject
    Heater heater;

    @Inject
    public Thermosiphon() {
    }
}

关于android - Dagger 2注入(inject)构造函数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32076244/

相关文章:

AndroidKeystore NoSuchAlgorithm 异常

android - 如何从 Sherlock 操作栏菜单项中删除蓝光?

android - 使用 Dagger 的应用程序组件构建 Android Instant App

android - Dagger2 依赖组件

java - 如果我知道图像的 URL,如何获取图像大小?安卓(java)

java - Webview加载url但在默认浏览器中打开内容?

java - 从具有值的 fragment B 返回到 fragment A

android - Dagger 2 - 在构造函数中注入(inject)默认值

android - 子组件(无作用域)可能不引用作用域绑定(bind) : @Singleton @Provides @org. jetbrains.annotations.NotNull

java - Dagger 在 Android 应用程序中生成重复的 `XXXDialogFragment_MembersInjector` 类(程序类型已存在)