Dagger 2注入构造函数的参数 [英] Dagger 2 injecting parameters of constructor
问题描述
我在 Dagger 2网站上看到了以下示例:
I saw the following example on the Dagger 2 website:
class Thermosiphon implements Pump {
private final Heater heater;
@Inject
Thermosiphon(Heater heater) {
this.heater = heater;
}
...
}
和文档:
当请求一个新实例时,Dagger将获得所需的实例 参数值并调用此构造函数.
When a new instance is requested, Dagger will obtain the required parameters values and invoke this constructor.
当我编写一个模块以提供Thermosiphon
之类的
When I write a Module to provide a Thermosiphon
like
@Module
public class ThermosiphonModule {
@Provides
@Singleton
Thermosiphon provideThermosiphon() {
return new Thermosiphon(???);
}
}
Thermosiphon
构造函数仍然需要Heater
作为参数,从而使整个自动注入构造函数依赖项"无效.
the Thermosiphon
constructor still requires a Heater
as an argument, rendering the whole 'automatic injection of constructor dependencies' useless.
我尝试了
return new Thermosiphon(null);
和
return new Thermosiphon();
(空的构造函数),并希望Dagger2接收到我希望注入缺少的Heater
的信息,但是提供的Thermosiphon
的Heater始终为null;
(empty constructor) and hoped for Dagger2 to pick up that I wanted the missing Heater
to be injected, yet the Heater of the provided Thermosiphon
is always null;
我验证了我的HeaterComponent
/HeaterModule
能否正常工作并且能够提供Heater
.
I verified though my HeaterComponent
/ HeaterModule
are working fine and are able to provide a Heater
.
我是否完全误解了"Dagger满足您对构造函数的依赖性"的全部功能,还是我错过了一些东西?
Do I completey misunderstand the whole feature of 'Dagger satisfies constructor dependencies for you' or am I missing something?
推荐答案
如果您使用的是模块,那么如果您有两个提供程序模块绑定到同一组件,则可以让他们看到加热器作为构造函数参数.
If you're using modules, then if you have two provider modules bound to the same component, then you'll be able to allow them to see the heater as a constructor parameter.
@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
构造函数,您还可以提供该给定范围的对象或无作用域的对象.
But with constructor injection, you will also be able to provide objects of that given scope, or unscoped objects, as long as they have a @Inject
constructor.
例如,
@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() {
}
}
这篇关于Dagger 2注入构造函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!