Xebia

Blog Xebia France

J2EE, Agility and SOA

April 15th, 2009

Google Guice 2 The bases of the injection of dependency

Guice (pronounce Juice) is the framework of injection of dependency of GOOGLE. The shape of dependency is made by code, with the aid of annotations, and requires therefore the use of Popular dance 5.
GOOGLE works currently on V2 of his framework, who hesitates to go out. However, a regular update of the Wiki of plan and publication snapshots already allow to make a good idea of this alternative in the injection of dependency 'to Spring'.

We start a series of articles today which is aimed at making you touch of the finger the big freedom which gives the injection of dependency by code. We will go progressively of basic concepts of Guice 2.0, towards an advanced use of the framework.

Declare a dependency with the annotation @Inject.

Let us take the postulate of following departure: the class MYSERVICEIMPL (which implements interface MYSERVICE) must accept the injection of the class MYBASICDAOIMPL (which implements interface MYBASICDAO).
In the source code of the class to be injected, we are therefore going to point out that Guice must take care of this injection, by using the annotation com.google.inject. Inject. In this first example, we will position this annotation at the level of the constructor.

Besides, we declare Appendable who will allow us to accomplish traces. This Appendable will be directly injected into variable.

public class MYSERVICEIMPL IMPLEMENTS MYSERVICE {

  @Inject
  private Appendable to re-string;

    private final MYBASICDAO BASICDAO;
  
  @Inject
  PUBLIC MYSERVICEIMPL (MYBASICDAO BASICDAO) {
    great ();
    this.basicDao = BASICDAO;
  }
}

You will point out that thanks to injection by constructor, we are capable of declaring BASICDAO final variable.

Injection can be accomplished at the level of the constructor, of a method even of very variable. Some advantages / disadvantages of these different methods:

  • Constructor
    • + Variables can be declared final
    • - Am not possible be used if the object is instanci out of Guice
  • Method
    • + An instanci object out of Guice allows to inject
    • + Classes girls do not need to know dependency of classes mothers.
  • Variable
    • + The most compact syntax
    • - Reduced the testabilit and the encapsulation

Method recommended by GOOGLE is injection by constructor.

Link an interface to its real realisation, in a Module

It is necessary us to link interface MYBASICDAO now to the realisation which we want to use at first, to know MYBASICDAOIMPL.
This is made in a module of spreading com.google.inject shape. Module with the aid of relatively explicit DSL.

public class MYMODULE IMPLEMENTS com.google.inject. Module {
  
  @Override
  public void configure (com.google.inject. Binder Binder) {

    //Bind to class
    binder.bind (MyBasicDao.class).to (MyBasicDaoImpl.class);
    binder.bind (MyService.class).to (MyServiceImpl.class);

    //Bind to authority
    binder.bind (Appendable.class).toInstance (System.out);
  }
}

Module allows to link interfaces to classes which Guice will undertake to instancier (_bind to _) but also in classes already instanci es (_bind toInstance _) as it is the case for our Appendable, that will be linked to the console System.

Recover an authority to Injector

Last stage, it is necessary us to get an authority injected by our service.
It is to the class com.google.inject. Injector that we will recover her. This injector is created in a static way, from one or several Module.
In this example, we will accomplish this operation from one hand.

And Spring in any ca?

The question which settles as soon as they approach that the injection of dependency is really sure 'Yes, but in comparison with Spring?'.
Let us try to avoid the answer of Norman, even if Spring and Guice do not play in the same court: Guice concentrates exclusively on the injection of dependency, while Spring aims at giving a complete stack.
The team of Guice answers besides itself question on its wiki.
However, to give a more pragmatic point of view:

  • the footprint of the Jar du Guice is well less than that (those) of Spring Core.
  • the chaining of Beans is quicker with Guice, even if this argument weighs not much, the majority of the dependency being loaded in the initialization of application.
  • Guice is more interfering (appearance of annotations in the source code of services job)
  • orientated programming aspect of Guice is at the embryonic stadium.

Following this series of articles, we will try to show that the difference of approach between Guice and Spring gives more freedom to the developers (but driven perhaps also to more chaos).

Continuation?

It is already the purpose of this tutoriel first, and you can inject a class now simply. However, in plan such as writes today, the class MYBASICDAOIMPL will be injected into all classes requiring MYBASICDAO. In a next article, we will see how raising this border.

Download Eclipse plan on the Google Code de Xebia

Resources

Official Site
A comparative of performances Spring / Guice, based on the previous versions

Key words :, , , , , Popular dance,

You can follow answers accepted by this article thanks to the thread of comments.

3 answers to Google Guice 2 The bases of the injection of dependency

  • April 16th, 2009 in 8:55 (), said :

    Exactly in the same type, they have of course tapestry-ioc (http://tapestry.apache.org/tapestry5/tapestry-ioc/) who besides draws inspiration from it apparently.

  • On April 16th, 2009 in 9:37 (), S ven Le Mesle said :

    Yes, we plan besides to make an article of presentation of tapestry-ioc method in next days.
    For recall it is about a fork of .

  • On April 16th, 2009 in 13:38 (), Fanf said :

    To note that one of T5-IoC's big contributions in comparison with Guice is the possibility of configurer his services in a decentralised way, that is that different modules, in different ganders, can "participate" in the shape of a service defined in a module. For more information on this small wonder of modularit (without intending that coupled with the automatic load of modules T5-IoC located in the classpath, create a system of plugins for your application becomes free):

    As for Hivemind, T5-IoC not is not a fork, in sense where there is no base of code common between both plans. But since in both cases, the creator is Howard Lewis Ship, there are big resemblances willy-nilly - one can as Tapestry 5 a fork of TAPESTRY 4 is not, but they have similarities apparently.

Leave a comment


Defence Colis e - 10/12, avenue of The Ark
92419 Courbevoie Cedex

T l : +33 (0) 1 46 91 76 16
Fax : +33 (0) 1 46 91 88 00
E-mail : info@xebia.fr