1
我想在使用guice實例化子類時將依賴項注入父類。在下面的例子中,我試圖創建一個TrainingCommandData
的實例,而我希望能夠在運行時使用Guice注入TelemetryServiceClient
。我怎樣才能做到這一點?使用Guice,在子類中注入依賴項
public class TrainingCommandData extends CommandData {
private Intent intent;
public TrainingCommandData(UserCommandResource userCommandResource, Intent intent) {
super(userCommandResource);
this.intent = intent;
}
}
public class CommandData {
private TelemetryServiceClient telemetryServiceClient;
private UserCommandResource userCommandResource;
@Inject
public void setTelemetryServiceClient(TelemetryServiceClient telemetryServiceClient) {
this.telemetryServiceClient = telemetryServiceClient;
}
public CommandData(UserCommandResource userCommandResource) {
this.userCommandResource = userCommandResource;
}
}