Google Guice, addiction injector

The purpose of a dependency injector is to have access to objects, without worrying about their instantiation. How and when to instantiate them? How to refer to them? These are problems that Google Guice will be able to answer for you.

Three steps are important for its use:

  • Declare its listener (application entry point) in web.xml which extends GuiceServletContextListener
  • Declare your dependencies in the listener via modules
  • Access your dependencies from anywhere with a simple injection annotation (in your constructors, methods, properties, ...)

In Guice, a dependency can be :

  • A class (a "singleton" service)
  • An instance of a class
  • A constant
  • A Provider (this can be thought of as a context-dependent object getter at the time it is called, like a session object)

One of the advantages of declaring dependencies (services and others) in Java rather than in XML (Spring) is to control what is injected according to the execution context. Typically :

  • For unit tests, easily start the service(s) under test only
  • Also for unit tests, injecting another instance to a dependency, with a mock or a lighter implementation of a service
  • For your different environments (integration / production and customer / supplier), mock up third party applications very easily
  • For your modular projects, no matter what is injected and where it is done, simply access your dependency

Guice also allows you to declare the filters and Web Services of your webapp in Java. There too, the interest is to be able to freely manipulate your declarations.

Finally, another good reason to adopt this framework: Its editor 🙂 It is also known for its light, reliable and efficient developments!

Some examples of use:

1. Declaration of dependencies in a "Module

import com. google.inject .AbstractModule;
import com. google.inject .Scopes;
 
public class MyModule extends AbstractModule {
    @Override
protected void configure() {
    
Declaration of an implementation
bind(MyService.class).to (MyServiceImpl.class).in (Scopes. SINGLETON);
Use : @Inject MyService service;
        
Declaration of an instance
bind (MyClass.class).toInstance (MyClassFactory.get());
Use : @Inject MyClass obj;
        
Declaration of a variable
bind (String. class).annotatedWith (Names. named("MY_CONSTANT_ANNOTATION )).to( "value);
Usage : @Inject @Named("MY_CONSTANT_ANNOTATION") String var;
        
Declaration of a constant
bindConstant ().annotatedWith(Names.named ("MY_CONSTANT_ANNOTATION)).to("value );
Usage : @Inject @Named("MY_CONSTANT_ANNOTATION") String var;
        
Multiple declaration
        Multibinder <MyClass> setObj = Multibinder .newSetBinder(binder(), MyClass.class);
setObj .addBinding().toInstance(new MyClass("param ));
setObj .addBinding().toInstance(new MyClass("otherParam ));
         Utilisation : @Inject Set<MyClass> setObj;
}

2. Declaration of Modules

import com. google.inject .Guice;
import com. google.inject .Injector;
import com. google.inject .servlet.GuiceServletContextListener;
 
public abstract class MyContextListener extends GuiceServletContextListener {
    @Override
protected Injector getInjector () {
return Guice. createInjector(module1 , module2, ...);
}
...
}
 
Note:
In your unit tests, you must do the same work of creating the "Injector" by declaring only the modules necessary for your tests (hence the interest of not declaring everything in a single module).
And it is the "configure" method of your module that you need to override if you want to inject other implementations (Mock).

3. Use of Jersey in Guice (JAX-RS implementation (JSR 311) for RESTful WebServices)

3.1 List of webServices in an "Application" object

import javax. ws .rs .core . Application ;
 
public class MyListsServlets extends Application {
    @Override
    public Set < Class<?>> getClasses () {
return ImmutableSet .of (
MyFirstServlet . class,
OtherWebService . class);
}
}

3.2 Declaration of WebServices in a ServletModule

 
import com. google .inject . AbstractModule ;
import com. google .inject . servlet .ServletModule ;
import com. sun .jersey . guice. spi .container . servlet .GuiceContainer ;
 
AbstractModule moduleWS = new ServletModule () {
    @Override
protected void configureServlets () {
        serve ( « /rest/* » ).with (GuiceContainer . class, new HashMap <String , String>() { { put (« javax.ws.rs.Application » , MyListsServlets. class .getName ()); } };);
}
});
 

Are you interested in this topic?

CONTACT US