<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Christian Schenk&#187; dependency injection</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/dependency-injection/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<lastBuildDate>Sun, 04 Dec 2011 23:43:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Comparison between Guice, PicoContainer and Spring</title>
		<link>http://www.christianschenk.org/blog/comparison-between-guice-picocontainer-and-spring/</link>
		<comments>http://www.christianschenk.org/blog/comparison-between-guice-picocontainer-and-spring/#comments</comments>
		<pubDate>Sat, 23 Jun 2007 17:51:10 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[dependency injection]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[guice]]></category>
		<category><![CDATA[inversion of control]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[picocontainer]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/comparison-between-guice-picocontainer-and-spring/</guid>
		<description><![CDATA[This post shows my comparison of Google Guice, PicoContainer and Spring]]></description>
			<content:encoded><![CDATA[<p>For quite some time it was obligatory to use <a title="Springframework.org" href="http://www.springframework.org/">Spring</a> in a Java project; it became a defacto standard. Once you&#8217;re familiar with dependency injection you don&#8217;t want to code <em>without</em> it. One alternative to Spring was and still is <a title="PicoContainer - IoC component container" href="http://www.picocontainer.org/">PicoContainer</a>. But then <a title="google-guice" href="http://code.google.com/p/google-guice/">Guice</a> came along: Googles <em>lightweight dependency injection framework</em>. I investigated it at the time it was released and now I wrote a simple comparison between Guice, PicoContainer and Spring because I&#8217;d like to integrate one framework into a <a title="BibSonomy" href="http://www.bibsonomy.org/">project</a> I&#8217;m working on.</p>
<p>If you&#8217;d like to read and watch something about Guice or Google check out the following links:</p>
<ul>
<li>the authors of Guice, Bob Lee and Kevin Bourrillion, <a title="Java on Guice: Dependency Injection, the Java Way" href="http://video.google.com/videoplay?docid=6068447410873108038">present</a> Guice</li>
<li>Bob Lee presents Guice at the <a title="Google Developer Day Beijing - Guice" href="http://www.youtube.com/watch?v=x_NpraeC3tk">Google Developer Day in Beijing</a></li>
<li>Kevin Bourrillion explains to his wife what <a title="by the way, what does 'extraordinarily typesafe' mean?" href="http://smallwig.blogspot.com/2007/03/by-way-what-does-extraordinarily.html">extraordinarily typesafe</a> means</li>
<li>watch <a title="An Inside Look at Google - Working at Google" href="http://www.youtube.com/watch?v=aOZhbOhEunY">this</a> short video or check out Googles <a title="The Official Google Channel" href="http://youtube.com/google">channel</a> on YouTube to get an impression how they&#8217;re working and what they&#8217;re working on &#8211; quite interesting.</li>
</ul>
<p>During this comparison I&#8217;ll try to focus on DI only. To conclude that, say, Guice or PicoContainer is <em>better</em> than Spring, would be too general to be useful, since Spring is a <a title="Spring Framework Projects" href="http://www.springframework.org/projects">lot more</a> than just a DI framework.</p>
<p><span id="more-28"></span></p>
<h2>Setup</h2>
<p>I hacked together a simple test that was meant to identify the pros and cons of Guice, PicoContainer and Spring. You can download the Eclipse project as <a href="http://data.christianschenk.org/comparison-between-guice-picocontainer-and-spring/GuicePicoSpringComparison.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/comparison-between-guice-picocontainer-and-spring/GuicePicoSpringComparison.zip">zip</a> or browse the code <a title="GuicePicoSpringComparison - Code Reference" href="http://data.christianschenk.org/comparison-between-guice-picocontainer-and-spring/xref/index.html">here</a>; although not needed, you may want to install the Spring <a title="Spring IDE Update Site" href="http://springide.org/updatesite/">plugin</a> for Eclipse. I recommend installing the <a title="AJDT: AspectJ Development Tools - Downloads" href="http://www.eclipse.org/ajdt/downloads/">AspectJ Development Tools</a>, but if you don&#8217;t want to install them, make sure to delete the <code>LoggingAspect</code>, the <code>ajbuilder</code> and <code>ajnature</code> from the <code>.project</code> and the <code>ASPECTJRT</code> entry from the <code>.classpath</code> file.</p>
<p>If you start looking at the code check out the <code>ComparisonTest</code> class first, as everything is bootstrapped from there.</p>
<p>All the test does is creating a hierarchy of objects:</p>
<ul>
<li>the class <code>SuperManager</code> is the <em>top-level</em> class of the hierarchy</li>
<li>the three classes <code>OutputManager1</code>, <code>OutputManager2</code> and <code>ComputationManager</code> are dependencies of <code>SuperManager</code></li>
<li>the utility class <code>OutputHelper</code> needs an implementation of <code>Output</code>, i.e. either <code>SystemOutOutput</code> or <code>LoggerOutput</code>.</li>
<li>one instance of <code>OutputHelper</code> that uses an instance of <code>SystemOutOutput</code> is handed to <code>OutputManager1</code> and another instance of <code>OutputHelper</code> that uses an instance of <code>LoggerOutput</code> is handed to <code>OutputManager2</code></li>
<li>the <code>ComputationManager</code> receives an instance of <code>ComputationHelper</code> and is handed to the instance of <code>SuperManager</code></li>
</ul>
<p>Have a look at the dependency graph:</p>
<p><img src="http://data.christianschenk.org/comparison-between-guice-picocontainer-and-spring/springbeans.png" alt="The dependency graph"/></p>
<h2>Comparison</h2>
<p>Following I&#8217;ll list things that I noticed using Guice, PicoContainer and Spring.</p>
<h3>Guice</h3>
<p>The first thing you&#8217;ll notice is that Guice makes extensive use of Java 5 language features, i.e. <a title="Generics" href="http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html">generics</a> and <a title="Annotations" href="http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html">annotations</a>. While generics lead to the extraordinary type safety <a title="by the way, what does 'extraordinarily typesafe' mean?" href="http://smallwig.blogspot.com/2007/03/by-way-what-does-extraordinarily.html">mentioned</a> by Kevin Bourrillion, annotations couple your code to the Guice framework. If you&#8217;re really picky about loose coupling you might ask why you should use a framework that adds a dependency to itself?! You end up with lots of imports of the <code>Inject</code> annotation scattered all over your code base. And it&#8217;s getting worse if you have to use <code>Singleton</code>, <code>ImplementedBy</code> and so on and so forth. You may want to define that elsewhere because your classes shouldn&#8217;t know anything about their usage (e.g. <code>Singleton</code>) or where dependencies should be injected (<code>Inject</code>) &#8211; that&#8217;s what inversion of control is all about, isn&#8217;t it?</p>
<p>However, for those who aren&#8217;t so picky there are some really cool things about Guice:</p>
<ul>
<li>it is type safe</li>
<li>it reports sane error messages</li>
<li>it is small and very fast</li>
</ul>
<p>Well it is really type safe: if you ask Guice to give you an object of type <code>Bar</code> it&#8217;ll do so &#8211; no cast to <code>Bar</code> required. The authors of Guice don&#8217;t want it to be used as a simple <a title="Service Locator" href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/ServiceLocator.html">service locator</a> for a <a title="Inversion of Control Containers and the Dependency Injection pattern - Martin Fowler" href="http://www.martinfowler.com/articles/injection.html#UsingAServiceLocator">good reason</a>: your code would still be coupled very tightly. That being said you want to call Guice only in some places in your code where it hands you a <em>top-level</em> class (e.g. a class like <code>SuperManager</code>) where all the dependencies are injected. Whether you&#8217;ll have to do a cast on these few occasions shouldn&#8217;t be that bad; this puts the big feature of type safety into another perspective (read: isn&#8217;t that important). But this is definitely a plus for Guice though.</p>
<p>Reasonable error messages are one point for Guice: if something goes wrong you can identify the problem easily. From a user&#8217;s perspective this should be the case anyway but Guice manages this very well &#8211; as far as I noticed it.</p>
<p>Finally I&#8217;d like to credit Guice for being fast. I haven&#8217;t written performance tests myself but Bob Lee <a title="Spring and Guice performance comparison" href="http://google-guice.googlecode.com/svn/trunk/test/com/google/inject/PerformanceComparison.java">wrote a test</a> in March 2007. He came to the conclusion that Guice is more than 50 times faster than Spring. I ran the test lately and got the following results (average of ten test runs):</p>
<table>
<thead>
<tr>
<th> </th>
<th>Spring</th>
<th>Guice</th>
<th>By hand</th>
</tr>
</thead>
<tbody>
<tr>
<td>Creations/s</td>
<td>34.294</td>
<td>336.824</td>
<td>7.893.772</td>
</tr>
</tbody>
</table>
<p>So it boils down to this:</p>
<table>
<thead>
<tr>
<th> </th>
<th>Spring</th>
<th>Guice</th>
<th>By hand</th>
</tr>
</thead>
<tbody>
<tr>
<td>Spring</td>
<td>1x</td>
<td>10x</td>
<td>230x</td>
</tr>
<tr>
<td>Guice</td>
<td>0.1x</td>
<td>1x</td>
<td>23x</td>
</tr>
<tr>
<td>By hand</td>
<td>0.004x</td>
<td>0.04x</td>
<td>1x</td>
</tr>
</tbody>
</table>
<p>After that, Guice (1.0) seems to be <em>only</em> 10 times faster than Spring (2.0.6). Have a look at another performance test <a title="Google GUICE 1000% faster than Spring" href="http://gorif.wordpress.com/2007/07/05/google-guice-1000-faster-than-spring/">here</a>.</p>
<h3>PicoContainer</h3>
<p>PicoContainer is a nice piece of software that facilitates dependency injection without much overhead: no external configuration files (read XML files) and no annotations needed. All you have to do is register your components with the container; for small projects this is an effortless thing.</p>
<p>As with Guice you write everything in Java and can reap the benefits of your IDE&#8217;s refactoring capabilities: if you change a class&#8217;s name all references will be changed too.</p>
<p>PicoContainer supports the concept of a lifecycle; it isn&#8217;t that relevant for my comparison, so if you&#8217;re interested you can read about it <a title="PicoContainer - Lifecycle" href="http://www.picocontainer.org/lifecycle.html">here</a>.</p>
<h3>Spring</h3>
<p>Spring is the ultimate dependency injection framework serving the industry for several years. Some years ago every tool had to have an XML configuration; Spring came along with a fancy one and bingo &#8211; (almost) everyone liked it. First of all, Spring doesn&#8217;t have the aforementioned cool features of Guice or PicoContainer: it is not type safe and doesn&#8217;t leverage existing refactoring tools of your IDE (that is <a title="Eclipse - an open development platform" href="http://www.eclipse.org/">Eclipse</a> in my case) &#8211; <em><a href="http://www.christianschenk.org/blog/comparison-between-guice-picocontainer-and-spring/#comment-72">update</a>:</em> the latter isn&#8217;t true.</p>
<p>Despite these <em>flaws</em> Spring is well established and seems like a good investment into the future. Opposing to Guice, Spring doesn&#8217;t introduce a dependency to itself by default. As long as you don&#8217;t use any special features of Spring, your code base will be completely abundant from any dependency injection framework, i.e. if you&#8217;d like to go back to dependency injection by hand, you can do so without deleting any annotations or special import statements. You just have to get rid of the XML configuration files.</p>
<p>Spring seems to be <em>harder</em> to use than Guice: without autowiring you have to specify the dependencies between your beans in a very verbose manner, i.e. you have to think about the things you&#8217;d like to do and write them down in a configuration file. Ideally you don&#8217;t have to specify anything with Guice, except the <code>Inject</code> annotations and Guice will handle everything for you.</p>
<p>Another difference I noticed is that Spring uses <a title="Singleton pattern" href="http://en.wikipedia.org/wiki/Singleton_pattern">singletons</a> by default while Guice doesn&#8217;t. If you want a class to be a singleton you&#8217;ll have to use the <code>Singleton</code> annotation with Guice. Obviously, both approaches have their strength and weaknesses.</p>
<h2>Conclusion</h2>
<p>It&#8217;s a tough decision to choose one of these frameworks &#8211; obviously they&#8217;re all pretty cool. First I opted for Guice because I like experimenting with new, bleeding-egde software. But as I mentioned earlier, there might be people on your team who think that all these annotations couple the code to Guice &#8211; this takes some evangelizing. Spring, on the other hand, is known for its verbose configuration but also for its robustness and being future-proof.</p>
<p>To draw a conclusion I think choosing between Guice, PicoContainer and Spring, for lack of a hard and fast rule, will be to do what works best for your project. Currently I would recommend using Guice for dependency injection: if you use the annotations wisely they&#8217;ll unobtrusively document your code and since you&#8217;ll do everything <em>&#8220;the Java way&#8221;</em><small><sup>TM</sup></small> refactoring will be really fun.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/comparison-between-guice-picocontainer-and-spring/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

