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

Going the extra mile with Providers


So far, we have worked with directly injecting dependencies. Yet, there are a few cases where injecting direct dependencies may not be an appropriate choice. Let's discuss such scenarios, and figure out how a Factory style Provider class provides a better way to inject dependencies.

Need for a Provider

Guice provides an instance by invoking a suitable constructor. Whenever dependency injection happens, Guice requires all the instances be ready upfront to complete the wiring process. This is, however, undesirable in various situations. Consider FlightSupplier. Every time you require an instance of Client, all the dependencies would be wired to CSVSupplier. But effectively speaking, until a processRequest() invocation is made on FlightEngine's instance, CSVSupplier doesn't come into picture.

Following are certain drawbacks of early instantiation:

  • Late initialization may be required. Based on the client request, it would be unnecessary to parse all the .csv files...