如何在HK2注入一个未绑定的泛型? [英] How to inject an unbound generic type in HK2?

查看:159
本文介绍了如何在HK2注入一个未绑定的泛型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jersey 2.9编写Web服务,我也使用HK2作为DI。我有一个类处理与数据库的连接,可以像这样实例化:

  public class DBHandler {
私人DBConnection<?>的DbConnection;

@Inject
public DBHandler(DBConnection<?> dbConnection){
this.dbConnection = dbConnection;


code


$ b

正如你所看到的,字段 dbConnection 具有无限的泛型类型。绑定实现目前看起来如下所示:

  public class MyProductionBinder extends AbstractBinder {

@Override
protected void configure(){
bind(ClientServerDBConnection.class).to(new TypeLiteral< DBConnection<>>(){});






$ b

在运行时,会发生以下异常:

 原因:org.glassfish.hk2.api.UnsatisfiedDependencyException:Injectee没有可用于注入的对象(r​​equiredType = DBConnection< ;?>,parent = DBHandler,qualifiers = {}),position = 0,optional = false,self = false,unqualified = null,731556610)

如果我将 DBHandler 中的变量声明转换为这样的原始类型:

  public class DBHandler {
private DBConnection dbConnection;

@Inject
public DBHandler(DBConnection dbConnection){
this.dbConnection = dbConnection;
}
}

它可以像预期的那样工作。

我是否错过了一些东西,或者在HK2无法指定这种绑定?



最好的问候,

Andreas

解决方案

HK2使用与CDI类型匹配相同的规则,这意味着在使用DBConnection的情况下,注入DBConnection的用例不起作用。如果这是一个足够重要的用例,您可以进入Jira,我们可以考虑让HK2偏离CDI规则。我也认为DBConnection也可以。


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

解决方案

HK2 uses the same rules for type matching that CDI does, which does mean that your use case of injecting DBConnection will NOT work while just using DBConnection will work. If this is an important enough use case for you enter a Jira and we can think about having HK2 deviate from the CDI rules for this case. I also think that DBConnection would also work.

这篇关于如何在HK2注入一个未绑定的泛型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆