HK2 中 bindAsContract 和 bind 的区别

Difference between bindAsContract and bind in HK2

我正在努力在 J2EE jersey 项目中实现构造函数的依赖注入。我正在使用 HK2。我创建了一个 class

class MyServiceImpl implements MyService{
  @Inject
  public MyServiceImpl(String test){
   // do something
  }
}

现在,我的问题是,当我通过扩展 AbstractBinder 在 dependencybinder class 中注册此依赖注入时,将依赖绑定为简单 "bind" 与 "bindAsContract"?

当你使用

bind(ServiceImpl.class).to(IService.class)

ServiceImpl 是实现 class,IService 是您宣传为注入类型的 contract。所以你会使用

@Inject
private IService service;

bindAsContract(ServiceImpl.class)

你是说 ServiceImpl 既是执行 class 又是 合同来宣传。所以你需要这样注入它。

@Inject
private ServiceImpl service;