- Generic class
DynamoDbUtil.
public class DynamoDbUtil<T> {
private final DynamoDbEnhancedClient ddbClient;
private final DynamoDbTable<T> mappedTable;
@Inject
public DynamoDbUtil(
@Named("someNamedthing") final DynamoDbEnhancedClient ddbClient,
@Assisted final String tableName,
@Assisted final Class<T> beanClass
) {
this.ddbClient = ddbClient;
this.tableName = dynamoDbConfigMap.get(tableName);
this.mappedTable = ddbClient.table(this.tableName, TableSchema.fromBean(beanClass));
}
}
- Factory for assisted injection:
public interface DynamoDbUtilFactory {
<T> DynamoDbUtil<T> create(String tableName, Class<T> beanClass);
}
- In the below class I am trying to inject
DynamoDbUtil.
@Singleton
public class ChildClass extends ParentClass {
private final DynamoDbUtil<DynamoDbStore> dynamoDbUtil;
@Inject
public ChildClass(final DynamoDbUtilFactory dynamoDbUtilFactory) {
this.dynamoDbUtil =
dynamoDbUtilFactory.create("my-table", DynamoDbStore.class);
}
- Module Code:
install(new FactoryModuleBuilder()
.implement(new TypeLiteral<DynamoDbUtil<?>>() {}, DynamoDbUtil.class)
.build(DynamoDbUtilFactory.class));
But I getting this error:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
Caused by: com.google.inject.CreationException: Unable to create injector, see the following errors:
1) [Guice/KeyNotFullySpecified]: com.demo.factories.DynamoDbUtilFactory cannot be used as a key; It is not fully specified.
1 error
at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Unknown Source)
at com.google.inject.internal.InternalInjectorCreator.initializeStatically(Unknown Source)
at com.google.inject.internal.InternalInjectorCreator.build(Unknown Source)
at com.google.inject.Guice.createInjector(Unknown Source)
at com.google.inject.Guice.createInjector(Unknown Source)
at com.google.inject.Guice.createInjector(Unknown Source)
at com.demo.Driver.main(FashionHubService.java:48)
... 5 more