Here is a code snippet showing injection of a dependancy into a constructor:
@Inject
public TimeChecker(IClock clock) { ... }
And here is the Module to tell Guice which concrete implementation to use:
@Inject
public TimeChecker(IClock clock) { ... }
Now to get an instance of TimeChecker you no longer need to build it's dependancies:
TimeChecker timeChecker = Guice.createInjector(new ProductionSettingsModule()).getInstance(TimeChecker.class);
For one dependency this looks like a lot of work, but if your class has four or five dependencies the above line does not get any bigger.
No comments:
Post a Comment