I am trying to re-use a guice module from a Library which has multiple providers, I want to use few providers from the library and provide few in my own module.
Library Module -
public class LibraryModule extends AbstractModule {
@Override
protected void configure() {
}
@Provides
@Singleton
@Named("dbCredentials")
private AWSCredentialsProvider getCredentialsProvider(@Named("app.keySet") String keySet) {
return new SomeCredentialsProvider(keySet);
}
@Provides
@Singleton
private AmazonDynamoDB getDynamoDBClient(@Named("dbCredentials") AWSCredentialsProvider credentialsProvider,
@Named("aws.region") String region) {
return AmazonDynamoDBClientBuilder.standard()
.withCredentials(credentialsProvider)
.withRegion(region)
.build();
}
@Provides
@Singleton
@Named("dbMapper")
private DynamoDBMapper getDynamoDBMapper(AmazonDynamoDB dynamoDBClient) {
return new DynamoDBMapper(dynamoDBClient);
}
...... more providers using dbMapper
Now where I want to use this module I want to give some different implementation of AmazonDynamoDB
which uses the default credential provider.
public class MyModule extends AbstractModule {
@Override
protected void configure() {
bind(AmazonDynamoDB.class).toProvider(AmazonDynamoDBClientProvider.class).in(Singleton.class);
install(new LibraryModule());
}
AmazonDynamoDBClientProvider class -
public class AmazonDynamoDBClientProvider implements Provider<AmazonDynamoDB> {
private final String region;
@Inject
public AmazonDynamoDBClientProvider(@Named("aws.region") String region) {
this.region = region;
}
@Override
public AmazonDynamoDB get() {
return AmazonDynamoDBClientBuilder.standard()
.withRegion(region)
.build();
}
}
but when I try to do this it fails in the provider of library while trying to create the AmazonDynamoDB by saying A binding to com.amazonaws.services.dynamodbv2.AmazonDynamoDB was already configured
I wanted to know if it is possible to omit providers for classes which have already been bound in the parent module? If yes how do we do that? I was unable to find a solution for this problem.