Binding in Guice
In the previous topic, we became acquainted with the binding process and its importance in Guice DI. Each binding call to the bind()
method is type-checked, so the compiler can report mistakes in case you utilize off-base types.
Guice provides different types of binding techniques which can be used in modules. The types of binding available are: linked bindings, instance bindings; untargeted bindings. Constructor bindings, built-in bindings, Just-in-time bindings. and provider bindings.
Linked bindings
Linked binding helps to map a type to its implementation. Examples of linked bindings include an interface to its implementation class, and a superclass to a subclass.
Here, NotificationService
is bound to the SMSService
instance in the ApplicationModule
class. This binding affirmation binds an interface to its implementation:
bind(NotificationService.class).to(SMSService.class);
When we call injector.getInstance(ApplicationModule.class)
,it will utilize SMSService
. If binding...