Friday, January 4, 2008

Spring vs Google Guice

I 've happened to come across this open source Dependency Injection solution from Google and I read Guice user guide and it states that using Guice you get dependency injection without the tedious XML config files.
I agree, the Spring XML config files are more verbose than a configuration need be.
However, I think the Guice comparison leaves out a few, central points in the comparison.
These are:

1) Guice uses annotations to mark injection points.

2) Guice doesn't do third party component injection. You need to implement a custom provider for this.
This is mentioned too quickly in the comparison, as if it didn't matter.
3) Guice doesn't help you with component / application configuration.

4) Component injection configuration is spread throughout the application.

5) Guice should have been compared to Pico Container, which also uses a Java API for configuration.

Guice uses the @Inject annotation to mark where to inject values. Using annotations to mark this is, in my opinion, a less-than-optimal mechanism. It will work for most cases, but it is kinda static. If you have not marked a dependency in a constructor or method for injection with an @Inject,annotation, you can't have it injected. So what happens if you want different configurations of the same component? One instance where you inject 2 values, and one instance where you inject 4? How do you tell to Guice what dependencies to ignore, or add, compared to the ones
marked with the class static annotations? Yes, using named annotations you can determine *what* configuration to inject, but can you also control how many to inject?

The @Inject annotation also results in Guice being unable to inject dependencies into third party components. If the code doesn't have the @Inject annotation, Guice can't inject into it. You can work around this by writing custom providers, Bob Lee writes. But this forces me to use a second mechanism for third party components. Spring doesn't.

Springs XML files can glue all kinds of components together. It can also be used to
inject configuration parameters into components. Configuration parameters like database driver, url, user and password, or like directories on hard drives, server addresses etc.
Is this easy to do with Guice? I'd have to register some String instances with named annotations,and specify where to inject them using @Inject annotations, right? And what about injecting these configuration values into a third party component? Springs XML files does all that exactly the same way you inject one of your classes into another.

Another aspect I think is missing is that when using Guice the injection configuration is spread throughout the application. Some is marked on components with the @Inject annotation.
Some is marked in Guice modules. Some is implemented in custom providers. And yet some is specified on the interfaces using an @ImplementedBy annotation. In Spring you know where to look. In the XML files. This is a speculated "problem". It may turn out to be no problem at all in practice

And while looking for more deeper insights into it, I came across this beautiful article William Slouth's Code where William has given a very good example which shows how Guice increases the performance of an application.
For now I would like to analyze this more,before saying it is better than Spring.
Suggested Video Tutorial



I would like someone else to shed more light on this........

Print this post

0 comments: