Guice appeared amidst various dependency injection frameworks in March 2007. Handcrafted in Google, Guice offered altogether new experience of developing applications in Java without having to bother about developing a Factory or calling new keywords to create instances altogether. After six years, three major releases and a number of extensions around the core, Guice is thriving. Today, Guice 3.0 stands as a complete implementation of JSR-330 Injector. Bob Lee, founder of Guice leading the JSR-330 gives a great impression that philosophy of dependency injection is making great strides towards improving the way we program in Java. That itself is a big reason to get ones hands wet with Guice.
My first interaction with Guice happened in fall of 2010, when I was developing a Maven plugin for code scaffolding in Java. As I had a large code base to generate the source code itself, wiring dependencies using factories or injecting them through a constructor was out of question. I investigated various frameworks available for dependency injection. While most of the frameworks did focus on dependency injection yet none of them had such variety of rich APIs to bind dependencies to their implementations in various possible ways. This made Guice my first choice.
This book is written to share my learning and experiences, which I had while using Guice. With this book, the readers have with them detailed hands-on treatment to Guice along with a number of extensions, which surround it. The book is based on latest version of Guice, Guice 3.0. It focuses on core functionality, latest features offered and extensions surrounding the core functionality (to solve specific problems or to provide exclusive features).
Chapter 1, Getting Started with Guice, introduces Guice as an alternative to writing dependency injection code using constructors or factories. The focus is on simplicity and separation of concerns, which Guice brings in with dependency injection. Along with this, Maven is introduced to build and run the sample applications.
Chapter 2, Exploring Google Guice, lays down a sound footing in various areas of the dependency injection. We start our discussion with Injector, which prepares the object graph and injects the dependencies. Discussion continues with various types of bindings, Guice provides to bind a reference to its implementation. At the end of the chapter, we visit a case for injecting static dependencies.
Chapter 3, Diving Deeper in Guice, focuses on advanced concepts in Guice. Beyond explicit bindings we investigate the application of Providers, Scoping, Binding collections, and Configuration Management. Various Guice extensions such as guice-throwingproviders, guice-assistedinject, and guice-multibindings are discussed. A solution to handle installation of modules is discussed in later part.
Chapter 4, Guice in Web Development, helps us to learn about developing a web application in java using Servlets and JSPs using Guice. We discuss the programmatic approach to configure routing to servlets and JSPs and avoiding declarative approach altogether in web.xml. We discuss guice-servlet an important extension, which provides important artifacts like GuiceServletContextListener, ServletModule, and GuiceFilter. We learn how to scope various instances to Request, Session, and Singleton.
Chapter 5, Integrating Guice with Struts 2, demonstrates integration of Guice with Struts 2. We discuss guice-struts the extension, which provides various artifacts such as Struts2GuicePluginModule and Struts2Factory for easy integration. The web application developed is redeveloped with Guice and Struts2.
Chapter 6, Integrating Guice with JPA 2, illustrates integration of Guice with JPA 2 and Hibernate 3 in a web and standalone application. We continue with our Struts 2 based application from the previous chapter and make it driven by a JPA, Hibernate-backed database. In this process we discuss the extension guice-persist and various artifacts it provides such as, JpaPersistModule and PersistFilter. Transaction handling is discussed using JpaTxnInterceptor.
Chapter 7, Developing Plugins and Extensions using Guice, focuses on SPI, Service Provider Interface. Important classes and interfaces, which are useful for developing extensions and plugins around Guice, are discussed. A custom LoggingInterceptor is developed to understand various functionalities of SPI. guice-multibinder, an extension for binding collections is analyzed.
Chapter 8, AOP with Guice, provides a brief overview of what AOP is, and how Guice adds to its own style of AOP with method interceptors by providing an implementation of aopalliance. Various artifacts such as Matchers and MethodInterceptor are discussed. A lot of examples are covered detailing both the artifacts.
Appendix, Prerequisites, provides tips regarding essential tools involved in running and building the samples. Installation of Maven, Eclipse, Tomcat, and MySQL server is illustrated. Importing MySQL data dump for running the samples of chapter 6, 7, and 8 is also detailed.
You need to have JDK 1.5 installed on your machine. For running the samples from the various chapters Maven, the ubiquitous build and project management tool is required. Readers are advised to refer to the Appendix for prerequisites while running the samples.
This book is for anyone hands on with Java (JDK 5 and above) and curious to implement Guice for dependency injection. Being hands on book, programmers struggling with Guice would also find it as a handy reference.
In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.
Code words in text are shown as follows: "We can include other contexts through the use of the include
directive."
A block of code is set as follows:
new ServletModule(){ @Override protected void configureServlets() { install(new MainModule()); serve("/response").with(FlightServlet.class); serve("/").with(IndexServlet.class); } }
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
SearchRQ create( @Assisted("depLoc") String depLoc, @Assisted("arrivLoc") String arrivLoc, Date flightDate);
Any command-line input or output is written as follows:
# mvn exec:java -Dexec.mainClass="org.packt.client.Client"
New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>
, and mention the book title through the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you. The sample code for this book is also available as a Github repository at https://bitbucket.org/hussain-pithawala/begin_guice. Readers are encouraged to fork the repository and tweak it for themselves.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]>
if you are having a problem with any aspect of the book, and we will do our best to address it.