I'm writing a webservice using Jersey 2.9 and I'm also using HK2 for DI. I have a class which handles a connection to a database which can be instantiated like that:
public class DBHandler {
private DBConnection<?> dbConnection;
@Inject
public DBHandler(DBConnection<?> dbConnection) {
this.dbConnection = dbConnection;
}
}
As you can see, field dbConnection
has an unbounded generic type. By binding implementation currently looks like follows:
public class MyProductionBinder extends AbstractBinder {
@Override
protected void configure() {
bind(ClientServerDBConnection.class).to(new TypeLiteral<DBConnection<?>>() {});
}
}
Howevery, during runtime the following exception is thrown;
Caused by: org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at Injectee(requiredType=DBConnection<?>,parent=DBHandler,qualifiers={}),position=0,optional=false,self=false,unqualified=null,731556610)
If I turn the variable declaration in DBHandler
into a raw type like this:
public class DBHandler {
private DBConnection dbConnection;
@Inject
public DBHandler(DBConnection dbConnection) {
this.dbConnection = dbConnection;
}
}
it works like expected.
Do I miss something or is it not possible in HK2 to specify such a binding?
Best regards,
Andreas