Provider

By default, whenever an application requires an instance of an object, Guice instantiates and returns it; but, in some cases, if the object creation process needs customization, then Guice providers do that. A provider interface takes after the conventional factory design in making objects.  For instance, consider our ApplicationModule class binding process:

binder.bind(NotificationService.class).to(new SMSProvider());

By writing above line of code, SMSProvider class gives factory methods that will return objects of type NotificationService. Let's say we want to customize the object-making and maintenance process for the ServiceConnection class, which is shown as follows:

public class ServiceConnection { public void startService(){ ...

Get Java 9 Dependency Injection now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.