Ответ 1
Если вы используете модули, то если у вас есть два модуля поставщика, привязанные к одному и тому же компоненту, то вы сможете разрешить им видеть нагреватель в качестве параметра конструктора.
@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
.
Например,
@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() {
}
}