Book Image

Learning Google Guice

By : Hussain Pithawala
Book Image

Learning Google Guice

By: Hussain Pithawala

Overview of this book

<p>Google Guice is an open source software framework for the Java platform released by Google under the Apache License. It provides support for dependency injection using annotations to configure Java objects.</p> <p>Learning Google Guice is a concise, hands-on book that covers the various areas of dependency injection using the features provided by the latest version of Google Guice. It focuses on core functionalities as well as the various extensions surrounding Guice that make it useful in other areas like web development, integration with frameworks for web development, and persistence.</p> <p>Learning Google Guice covers Guice extensions which avoid complex API usage. You will start by developing a trivial application and managing dependencies using Guice. As the book gradually progresses, you will continue adding complexity to the application while simultaneously learning how to use Guice features such as the Injector, Provider, Bindings, Scopes, and so on. Finally, you will retrofit the application for the Web, using Guice not only to manage dependencies, but also to solve configuration related problems.</p>
Table of Contents (17 chapters)
Learning Google Guice
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

AssistedInject


For Guice solutions like Providers, CheckedProviders seem to be a nice replacement of the Factory classes. Yet, there are certain cases where in Factories seem to be irreplaceable. Consider a case where in constructor for a class has arguments, and there is no default constructor available. The problem over here is very common with the value or domain objects. For example, if SearchRequest were modified a little, and default constructor is changed to the following:

public SearchRequest(String departureLocation, 
String arrivalLocation, Date flightDate){
this.departureLocation = departureLocation;
this.arrivalLocation = arrivalLocation;
this.flightDate = flightDate;
}

Now, in such a case, injecting the Provider and obtaining the value object instance with provider.get() would not be feasible. One solution could be implementing a factory and via it exposing the APIs to constructor. Then, we would inject the factory to prepare value objects.

Guice provides a simple solution for...