我正在尝试使用Dagger 2管理依赖项。我需要为每个收到的请求检索并传递AWS凭证,但是我找不到一种干净的方法。
根据我一直在阅读的内容,该想法是将“新产品”与“可注射产品”结合在一起。
如果可能(简化代码),我想实现以下目标:
public static void main(String[] args) {
Credentials credentialsOne = new Credentials("username1", "password1");
DaggerAWSComponent.builder().withCredentials(credentialsOne).build();
Credentials credentialsTwo = new Credentials("username2", "password2");
DaggerAWSComponent.builder().withCredentials(credentialsTwo).build();
}
@Component(modules = Module.class)
interface Component {
UseCase useCase();
}
@Module
class Module {
@Provides
static Organizations provideOrganizations(Credentials credentials) {
return Builder.standard().withCredentials(credentials).build();
}
}
public class UseCase {
private final AWSOrganizations awsOrganizations;
@Inject
public UseCase(final AWSOrganizations awsOrganizations) {
this.awsOrganizations = awsOrganizations;
}
}