April 2018
Intermediate to advanced
246 pages
6h 11m
English
This kind of binding binds a type to a constructor. This specific case arises when the @Inject annotation can't be implemented to the target constructor. Possible reasons for this could be:
To address such a problem, we have the toConstructor() binding in our module. Here, if the constructor cannot be found, module reflectively select our target constructor and handle the exception:
public class SampleModule extends AbstractModule { @Override protected void configure() { try { bind(NotificationService.class).toConstructor( SMSService.class.getConstructor(SMSDatabaseConnection.class)); } catch (NoSuchMethodException e) { ...