Aside from dependency injection and configuration facilities via programmable APIs, Guice provides feature of scoping various classes, depending on their role in the business logic.
As we saw, while developing the custom scope in Chapter 3, Diving Deeper in Guice, a scope comes into picture during binding phase. Later, when the scope API is invoked, it brings the provider into picture. Actually it is the provider which is the key to the complete implementation of the scope. Same thing applies for the web scope.
Whenever we annotate any class with either of servlet scopes like @RequestScoped
or @SessionScoped
, call to scope API of these respective APIs are made. This results in eager preparation of the Provider<T>
instances. So to harness these providers, we need not configure any type of binding, as these are implicit bindings. We just need to inject these providers where we need the instances of respective types.
Let us discuss various examples related to these...