You are previewing Learning Google Guice.
O'Reilly logo
Learning Google Guice

Book Description

Develop your Java applications faster and more efficiently by learning dependency injection using Google Guice. This guide is the perfect introduction, covering both basic and advanced topics with a user-friendly approach.

  • Detailed coverage of the various features of Guice such as the Injector, Bindings, Providers, TypeLiterals, Scoping, Multibinders, and so on

  • Learn how to use the various Guice-throwing providers, Guice-multibinder, Guice-servlet, Guice-struts2, and Guice-persist in various areas of application development

  • Focus on web application development using Struts 2, Hiberate 3, and JPA 2 while managing dependencies using Guice

  • Coverage of advanced topics such as extending Guice to cater for your specific requirements

  • In Detail

    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.

    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.

    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.

    Table of Contents

    1. Learning Google Guice
      1. Table of Contents
      2. Learning Google Guice
      3. Credits
      4. About the Author
      5. Acknowledgments
      6. About the Reviewers
      7. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
        2. Why Subscribe?
        3. Free Access for Packt account holders
      8. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      9. 1. Getting Started with Guice
        1. Wiring dependencies
          1. Resolving dependencies directly
          2. Inverting the dependencies
          3. Understanding Inversion of Control
          4. Inverting the control using the Dependency injection
        2. Comparing two approaches
          1. Initializing dependencies directly
          2. Refactoring to use Guice
        3. Building and Running the examples
          1. Compiling the sample code
          2. Running Unit tests
          3. Running the main application
        4. Summary
      10. 2. Exploring Google Guice
        1. Meet the injector
          1. Various kinds of injections
            1. Field injection
            2. Method injection
            3. Constructor injection
        2. Configuring a module to write bindings
          1. Bindings
            1. Linked bindings
            2. Instance bindings
            3. Untargeted bindings
            4. Constructor bindings
          2. Binding annotations
          3. Binding constants
          4. Binding properties
          5. Just in time bindings
            1. By default constructors
            2. @Inject annotated constructors
            3. Interfaces annotated with @ImplementedBy
        3. Static injection
        4. Summary
      11. 3. Diving Deeper in Guice
        1. Going the extra mile with Providers
          1. Need for a Provider
            1. Working of Provider
          2. Rolling your own Provider
            1. Injecting dependencies in Providers
            2. Advantages of Provider
            3. @Provides
          3. CheckedProviders
            1. @CheckedProvides
        2. AssistedInject
        3. Binding collections
          1. Using TypeLiteral
          2. Using MultiBinder
        4. Scoping
          1. Singletons with Guice
          2. Eager Singletons
          3. Custom Scopes
            1. Motivation to write a custom scope
            2. Modifying our Provider
            3. Defining our own scope
            4. Changes in Module
              1. Direct binding
              2. Binding to an annotation
              3. Developing an annotation for a scope
            5. Observations
        5. Summary
      12. 4. Guice in Web Development
        1. Structure of flightsweb application
        2. Using GuiceFilter
        3. Rolling out our ServletContextListener interface
        4. ServletModule – the entry point for configurations
        5. Binding language
          1. Mapping servlets
          2. Mapping filters
        6. Web scopes
          1. @RequestScoped
          2. @SessionScoped
          3. @RequestParameters
          4. Exercising caution while scoping
        7. Summary
      13. 5. Integrating Guice with Struts 2
        1. Introducing Struts 2
          1. Guice 3 and Struts 2 integration
            1. Struts2GuicePluginModule
            2. Struts2Factory
          2. FlightSearch application with Struts 2
            1. FlightServletContexListener
            2. Index page
            3. FlightSearchAction
            4. Response page
        2. Summary
      14. 6. Integrating Guice with JPA 2
        1. Introduction to JPA 2 and Hibernate 3
          1. Persistence Unit
          2. PersistenceContext
          3. EntityManagerFactory
          4. EntityManager
          5. Session Strategies
        2. Guice 3 with JPA 2
          1. PersistService
          2. UnitOfWork
          3. JpaPersistService
          4. PersistFilter
          5. @Transactional
          6. JpaLocalTxnInterceptor
          7. JpaPersistModule
        3. FlightSearch application with JPA 2 and Hibernate 3
          1. Persistence.xml
          2. Client
          3. SearchResponse
          4. FlightEngineModule
          5. FlightJPASupplier
          6. FlightServletContextListener
        4. Summary
      15. 7. Developing Plugins and Extensions using Guice
        1. Developing a plugin
        2. Guice SPI (Service Provider Interface)
          1. Quick introduction to Visitor Design pattern
          2. Elements SPI
            1. Implementing Elements SPI
            2. Uses of Elements SPI
          3. Extensions SPI
        3. Exploring a custom extension, guice-multibinder
          1. Important classes and interfaces in guice-multibinder
          2. Multibinder in action
        4. Summary
      16. 8. AOP with Guice
        1. What is AOP?
        2. How AOP works?
        3. How Guice supports AOP?
          1. Implementing a LoggingInterceptor
          2. Understanding Matchers
          3. Limitations
        4. Concerns related to performance
        5. Summary
      17. A. Prerequisites
        1. Getting the sample code
          1. Installing Git
          2. Cloning the repository
        2. JDK
        3. Installing Maven
          1. Mac OSX and Linux
          2. Microsoft Windows
        4. Installing Eclipse
        5. Installing the m2eclipse plugin
        6. Installing Apache Tomcat
        7. Installing MySQL
          1. Microsoft Windows
          2. Mac OSX
          3. Linux
        8. Importing MySQL data
      18. Index