4

I am working to implement a dependency injection of a constructor in a J2EE jersey project. I am using HK2. I create a class

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

Now, my question is when I register this dependency injection in a dependencybinder class by extending AbstractBinder, what is the difference between binding the dependency as a simple "bind" vs a "bindAsContract"?

1 Answers1

6

When you use

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

ServiceImpl is the implementation class, and IService is the contract that you advertise as the injection type. So you would use

@Inject
private IService service;

With

bindAsContract(ServiceImpl.class)

you are saying that ServiceImpl is both the implementation class and the contract to advertise as. So you would need to inject it as such.

@Inject
private ServiceImpl service;
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720