Please watch it if DI (a.k.a Dependency Injection) interests you.
Where is Guice being used in production :-
The video mentions Google uses it for Google Wave, Google Adwords, GMail, Orkut, Google Docs, Youtube...pretty much every new project .
This is how I talked with myself, in case it is gonna help you let us learn together.
Should I care to learn Guice, I already know/use Spring :-
Spring framework is much more than a DI framework.It made DI popular (although there were older DI framework available before it). Spring competes with JEE stack.
So, yeah we can say Spring is a super set of Google Guice. Guice wholly embraces annotations and generics, thereby enabling you to wire together and test objects with measurably less effort. Guice proves you can use annotations instead of error-prone, refactoring-adverse string identifiers.And of course it is much light weight.
Spring uses annotations too :- That is true. The newer versions (probably 2+) focuses more to use annotations. We may discuss the benefits of XML declarations vs annotations in a separate post, as it is a topic in itself .
There are so many annotations for different DI libraries, shouldn't it be standardized :- You are in luck. Check it out JSR330- http://code.google.com/p/atinject/
Well...so show me some code :-
I won't boar you with the definition(s) of DI, IOC (search it on google rather in case you are new to this). Goal of this post is to make you run a simple Guice Program.
Download the guice library, and add it in your classpath.
The interfaces and it's implementations are self explanatory :-
public interface IPrinter {
public void print();
}
public class MyDummyPrinter implements IPrinter {
public void print() {
System.out.println("Article Printed !");
}
}
public interface IMailSender {
public void sendMail();
}
public class MyDummyMailSender implements IMailSender {
public void sendMail() {
System.out.println("Mail Sent !");
}
}
public interface ArticleService {
public void publishArticle();
}
import com.google.inject.Inject;
public class Article implements ArticleService{
private IMailSender mailSender;
private IPrinter printer;
@Inject
Article (IMailSender mailSender, IPrinter printer) {
this.mailSender = mailSender;
this.printer = printer;
}
public void publishArticle() {
mailSender.sendMail();
printer.print();
}
}
As shown in the above program, we want the mailSender and printer to be injected.
So, note the @inject annotation over the Article Constructor.
Lets inject the dependencies. Well, if you have worked with Spring's XML declarations for defining and injecting the dependencies, then think the next java code to be it's counter part in Guice.
import com.google.inject.AbstractModule;
public class MyArticleModule extends AbstractModule {
protected void configure() {
bind(IPrinter.class).to(MyDummyPrinter.class);
bind(IMailSender.class).to(MyDummyMailSender.class);
}
}
What I like here is the readability (It is like I am coding in Ruby :)). The code clearly shows that IPrinter.class is bound to MyDummyPrinter.class. Similarly, with the other line.
Cool..Now let us launch/bootstrap the Guice. For the Spring folks, think it similar to the code you write to load spring config beans (remember : ApplicationContext ctx = new ClasspathApplicationContext("example.xml")).
import com.google.inject.Injector;
import com.google.inject.Guice;
public class MyBootStrapInvoker {
public static void main(String[] args) {
Injector injector = Guice.createInjector(new MyArticleModule());
Article article = injector.getInstance(Article.class);
article.publishArticle();
}
}
And that's it. You are running Guice !!!
Hopefully, we will dive more in depth in the second post on the same topic.
Please let me know your comments, so the next time I could offer more.
1 comment:
Good introduction about Guice Anshu.
I've also written a tutorial about Guice a while ago
http://dev.eek.be/2009/09/dependency-injection-with-google-guice/
Post a Comment