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

How Guice supports AOP?


Guice conforms to AOP alliance, and so we could directly write interceptors by implementing MethodInterceptor interface (org.aopalliance.intercept). APIs could then be annotated with custom annotations, and these annotations could be used to join method invocation with the interceptors. For joining method interception with the interceptor, we need to use the bindInterceptor(…) API from the AbstractModule. Let's see all of this in action.

Implementing a LoggingInterceptor

Consider we need to design a mechanism (a fictitious one), which could be used to log invocation to a method and arguments passed to it. This could be used across the methods. This makes it a good cross-cutting concern. Let's implement this concern as an implementation of MethodInterceptor.

public class LoggingInterceptor implements MethodInterceptor{

@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
  System.out.println("Invoking"+invocation.getMethod().getName());
  Object...