Introduction to Google Guice framework in Java

Introduction to Google Guice framework in Java

This post is an introduction to Google Guice framework in Java. Guice (pronounced ‘juice’) is a lightweight dependency injection framework for Java 6 and above, developed by Google. You may ask why we need Guice if we already have Spring for dependency injection. Well, if you only need dependency injection in your application, you don’t need a spring container for it. Spring is more than just a dependency injection provider. Also most of the applications in spring uses XML configuration for dependency injection. But Guice is a relatively-lightweight framework with fewer integrations, Java instance configuration, and runtime bindings. With the use of Java bindings, you get compile-time type checking and IDE autocomplete integration.

Guice is also very fast. By default Guice injects a new and separate instance of an object for each dependency (called “prototype” scope by Spring), whereas Spring provides singletons by default. The Guice framework takes dependency injection to the next level, leveraging the full power of Java typing, especially annotations and generics, to make powerful DI applications more modular, easier to write, and less error prone to maintain.

Google Guice download

Google Guice dependencies are available on maven central, so for maven projects you can add below dependency for it.

You can also download the package from Github-Guice.

Below are the most important jar files needed to run a simple Java application using Guice.

  • guice-3.0.jar
  • javax.inject.jar
  • aopalliance.jar

 

Let us create a simple Java application which uses Guice to inject dependencies.

Service Classes

ArithmeticService.java

AdditionService.java

SubtractionService.java

MultiplicationService.java

DivisionService.java

Consumer Class

Guice supports both constructor based and setter based injection. In the above class, I have used setter based injection and commented the constructor based injection.

Binding Service implementation

Google guice needs to know which service implementation to use, so we have to configure it by extending AbstractModule class and provide implementation for configure() method. Below is an example injector configuration.

Note that in the above configuration, I have bound the service to AdditionService implementation. We can bind any of the implementation to service class. If, in future, you want to use a different service implementation like SubtractionService or MultiplicationService, you just need to change the binding in the above file. No need to change the client application.

Client Application

Let us see a sample client program.

Here we have created an Injector object using Guice class createInjector() method where we pass our custom injector class implementation object. Then we created an instance of the consumer class (MyApplication) using the injector’s getInstance() method. While creating MyApplication’s object, guice automatically injects the dependent service class implementation (AdditionService in this case). Below is the output of running the above code.

You can also use Google Guice to create instance of a service class without configuring the injector (i.e.binding service class implementation). Below is a sample client program without binding a specific service class implementation.

As shown in the above example, we did not use any custom injector like GuiceInjector. If we run above class, it will produce following output.

That’s all about Google guice framework in Java. You can also look into Dagger which is a compile-time dependency injection framework for both Java and android.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.