Skip to main content


Showing posts from September, 2017

More Guice Please!!!: Re-Learning Google's Agile Lightweight Dependency Injection Library (Part 1.4)

Let’s see if we can go through some simple examples starting with a typical XService/XServiceImpl example.  In this case, after creating the appropriate XService trait and XServiceImpl extending it, you could extend AppNameModule in the following way:
Listing 1.4: Example of Module binding XService and XServiceImpl package com.uequations.demo.module
import com.uequations.demo.service.{XService, XServiceImpl}
object XServiceModule extends AppNameModule {
override def configure(): Unit = {   bind(classOf[XService]).to(classOf[XServiceImpl]) } }

More Guice Please!!!: Re-Learning Google's Agile Lightweight Dependency Injection Library (Part 1.3)

... This is typically done with the bind and install methods inherited from AbstractModule.  The install method is fairly straightforward, since it can only accept a parameter of type Module.  You’ll notice the bind method will take parameters of type Class, Key, or TypeLiteral.  The Key and TypeLiteral types are part of the Guice library.  
You can probably guess using the bind method takes more thought than just using the install method with a neatly packaged module.  To ease things, though, the bind method comes with a fairly intuitive Embedded Domain-Specific Language(EDSL).  We will give some examples, which will be helpful since developers typically use bind more often than install.  Both methods, as aforementioned, act on a Binder instance.
Before we get into some examples, let me just show you a simple design approach some developers have found useful.  That is to extend Guice’s AbstractModule with an Application-Specific abstract class that overrides the configure method with an…

More Guice Please!!!: Re-Learning Google's Agile Lightweight Dependency Injection Library (Part 1.2)

In the subtitle of this tutorial, we say we are “re-learning” Google Guice because, with the growing adoption of the Scala programming language, it is necessary to re-learn libraries like Guice so that we are able to use them to build not only Java-based but also Scala-based services.  Continuing our discussion, one thing we should point out is that, when we extend the AbstractModule abstract class, we do not override the configure method of Listing 1.1 but a configure method without parameters.  In fact, if we try to override the method from Listing 1.1, you’ll get a complaint from either your compiler or IDE.  No, this is not magic.  It may be worthwhile to take a look at the AbstractModule source to see why this is the case. 
Listing 1.2: AbstractModule abstract class …
public abstract class AbstractModule implements Module {
Binder binder;
public final synchronized void configure(Binder builder) { … }
protected abstract void configure();
protected Binder binder() { .. }
... As we see from Li…

More Guice Please!!!: Re-Learning Google's Agile Lightweight Dependency Injection Library (Part 1.1)

Google Guice is used as a lightweight dependency injection framework that further assists developers in modularizing their applications.  Google shared this very useful library with the development community in 2010 in between the Java SE 6 and Java SE 7 releases.  This library is used in some of Java’s (and now Scala’s) most prominent libraries and platforms such as the Simian Army platform shared by Netflix.
We will begin our discussion of Google Guice with its Module interface.  In the Guice developers’ own words, ‘A Guice-based application is ultimately composed of little more than a set of modules and some bootstrapping code.’  We will not be using this interface directly, but it gives us a very good context from which to start.  Instead, we will be extending the abstract class that implements it -- intuitively named AbstractModule.  
If you ever get a chance to look at the Module interface JavaDoc or source code, you’ll see a configure method taking a parameter of type Binder.