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

Configuring a module to write bindings


There are two ways by which we can configure the bindings:

  • Firstly by extending the abstract class com.google.inject.AbstractModule

    @Override
    protected void configure() {
    }
  • Secondly by implementing the interface com.google.inject.Module

    @Override
    public void configure(Binder binder) {
    }

Tip

It is suggested that modules must be extended using AbstractModule. It provides a more readable configuration and also avoids repletion of invoking methods on binder.

We have simply followed one module per class strategy. Also, for the sake of illustration, we have developed all the modules as the method local classes in the main() method of Client.class. In the next chapter we will discuss more about structuring modules in a better way.

Developing modules as method local classes is not a suggested approach. It makes an application difficult to maintain. We have followed this approach for illustration purposes only. In the next chapter modules are developed as independent...