Google Guice - sample code on multiple bindings

Google Guice is a dependency injection framework in Java.Its lightweight because it is not a container like other AOP(Aspect Oriented Programming) containers such as PicoContainer or Spring.They are based on Inversion of Control principle.I don't have much experience in Spring or PicoContainer.Guice is used in Apache Shindig.Guice is a dependency injector using the features of generics and annotations.I wanted to know more about aspect oriented programming,IoC etc.So I tried with a simple one .. Guice.If you want to know more about IoC and Dependency Injection go through Martin Fowler's article . I wrote some an example.This example shows how multiple dependency bindings can be done in Guice.


IM.java

package com.im.sample;

public interface IM {
void sendMessage(String message);
}

An interface IM is created to be implemented by different classes.We can bind this interface
to a class by @ImplementedBy annotation, while it supports for one.We need multiple
implementations.

GTalkIMImpl.java

package com.im.sample;

public class GTalkIMImpl implements IM{
@Override
public void sendMessage(String message){
System.out.println("Gtalk:"+message);
}
}

YahooIMImpl.java

package com.im.sample;

public class YahooIMImpl implements IM {

@Override
public void sendMessage(String message){
System.out.println("Yahoo:"+message);
}
}


We use Modules to bind classes to interface.It acts as the configuration mechanism.

IMModule.java

package com.im.sample;

import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.name.Names;

public class IMModule implements Module{

@Override
public void configure(Binder binder) {

binder.bind(IM.class).annotatedWith(Names.named("GTalk")).to(GTalkIMImpl.class);
binder.bind(IM.class).annotatedWith(Names.named("Yahoo")).to(YahooIMImpl.class);
}

}


Here we have to create two annotations to bind the implementations.Names calss will
automatically bind the annotation to the specific classes.Names.named will take the annotation name.

GTalk.java

package com.im.sample;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.google.inject.BindingAnnotation;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@BindingAnnotation
public @interface GTalk{}


Yahoo.java

package com.im.sample;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import com.google.inject.BindingAnnotation;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@BindingAnnotation
public @interface Yahoo{}


These annotations provide the named binding.@Retention defines the class to be available in runtime.@Target define where we the elements are applicable.During injection the target define the elements.@BindingAnnotation annotates annotations which are used for binding.More here here

Messenger.java

package com.im.sample;

import com.google.inject.Inject;
import com.google.inject.name.Named;

public class Messenger {

@Inject @Named("GTalk") private IM gtalk;
@Inject @Named("Yahoo") private IM yahoo;

public Messenger(){

}

public IM getGtalk() {
return gtalk;
}

public IM getYahoo() {
return yahoo;
}

}

Test.java

package com.im.sample;

import com.google.inject.Guice;
import com.google.inject.Injector;

public class Test {

public static void main(String[] args) {

Injector injector = Guice.createInjector(new IMModule());
Messenger msg = new Messenger();
injector.injectMembers(msg);
msg.getGtalk().sendMessage("Hello Gtalk");
msg.getYahoo().sendMessage("Hello Yahoo");

}

}


Guice automatically finds the IM class and injects it into Messenger class. Guice seems simple and cool.Using guice we can mostly avoid configuration files (I donno whether this will be advantage in all situations!) , we can provide better maintainability.The coding is smooth.Using annotations it becomes more flexible.

Go through users guide here.

If there is anything to be changed or to be improved .. try out anyway.

No comments:

Post a Comment