Default scope

By default, Guice injects a new and separate instance of an object for each dependency (similar to the prototype scope in Spring), whereas Spring provides singletons by default. 

Let us consider an example of a house that has a family with three people, all with their own personal car. Every time they call the injector.getInstance() method, a new instance of a car object is available for each family member:

home.give("Krunal", injector.getInstance(Car.class));home.give("Jigna", injector.getInstance(Car.class));home.give("Dirgh", injector.getInstance(Car.class));

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.