<?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>It's Just a Bunch of Stuff That Happens &#187; ClassBus</title>
	<atom:link href="http://stuffthathappens.com/blog/category/classbus/feed/" rel="self" type="application/rss+xml" />
	<link>http://stuffthathappens.com/blog</link>
	<description>Technology and Geek Stuff by Eric Burke</description>
	<lastBuildDate>Fri, 04 Jun 2010 14:44:50 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Guice Questions (with Answers)</title>
		<link>http://stuffthathappens.com/blog/2007/11/14/guice-questions-with-answers/</link>
		<comments>http://stuffthathappens.com/blog/2007/11/14/guice-questions-with-answers/#comments</comments>
		<pubDate>Thu, 15 Nov 2007 00:18:12 +0000</pubDate>
		<dc:creator>Eric Burke</dc:creator>
				<category><![CDATA[ClassBus]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Guice]]></category>

		<guid isPermaLink="false">http://stuffthathappens.com/blog/2007/11/14/guice-questions-with-answers/</guid>
		<description><![CDATA[I mentioned that ClassBus uses Guice, and received some questions:
Speaking of Guice, don’t you have to reference Guice APIs to bootstrap the dependency injection? In general how should &#8220;third party&#8221; libraries handle Guice? As an app developer how should I handle libraries that use Guice? Do I care?
First, let me explain how ClassBus uses Guice. [...]]]></description>
			<content:encoded><![CDATA[<p>I mentioned that <a href="http://code.google.com/p/classbus/">ClassBus</a> uses <a href="http://code.google.com/p/google-guice/">Guice</a>, and received <a href="http://stuffthathappens.com/blog/2007/11/13/static-imports-rock/#comment-1406">some questions</a>:</p>
<blockquote><p>Speaking of Guice, don’t you have to reference Guice APIs to bootstrap the dependency injection? In general how should &#8220;third party&#8221; libraries handle Guice? As an app developer how should I handle libraries that use Guice? Do I care?</p></blockquote>
<p>First, let me explain how ClassBus uses Guice. ClassBus is a tiny project with a handful of interfaces and classes. For example, <code>BasicEventService</code> depends on a <code>DeliveryStrategy</code> interface:</p>
<pre class="prettyprint">@Inject
public BasicEventService(DeliveryStrategy deliveryStrategy) {
  this.deliveryStrategy = deliveryStrategy;
}</pre>
<p>See that &#8220;@Inject&#8221;? That&#8217;s Guice. The <code>DeliveryStrategy</code> interface declares a default implementation:</p>
<pre class="prettyprint">@ImplementedBy(EdtDeliveryStrategy.class)
public interface DeliveryStrategy {
 ...
}</pre>
<p>That&#8217;s because I anticipate most people will use this framework in Swing GUIs and will want event dispatch thread delivery semantics.</p>
<p>I also use the <code>@Singleton</code> tag in one place:</p>
<pre class="prettyprint">@Singleton
public class BasicEventService implements EventService {
  ...
}</pre>
<p>Again, that&#8217;s the most common (anticipated) usage pattern.</p>
<h2>Ramifications</h2>
<p>Using these Guice annotations means I need Guice in my classpath <strong>when I compile ClassBus</strong>.</p>
<p>Let me be perfectly clear about this: <strong>You do not need Guice to use ClassBus.</strong> Projects using ClassBus don&#8217;t need a Guice JAR file anywhere, either at build or run time.</p>
<p>Of course, if you wish to use dependency injection with Guice, you&#8217;ll then need to include Guice in your classpath.</p>
<h2>Bootstrap Example</h2>
<p>Two of the demonstration programs use Guice. It&#8217;s pretty basic stuff:</p>
<pre class="prettyprint">private static void swingSafeMain() {
  EventService es = Guice.createInjector().getInstance(EventService.class);
  new ProgressDemo(es).setVisible(true);
}</pre>
<div class="left"><img src='http://stuffthathappens.com/blog/wp-content/uploads/2007/11/why-not-guice.png' alt='Why Not?' /></div>
<p>That&#8217;s it. One line of code to create the injector and get an implementation of the <code>EventService</code> interface. Since the <code>EventService</code> interface specifies an <code>@ImplementedBy(BasicEventService.class)</code> annotation, you get the default. And since <code>BasicEventService</code> specifies the <code>@Singleton</code> annotation, it is a singleton. And finally, since the <code>BasicEventService</code> constructor uses <code>@Inject</code>, you get the <code>EdtDeliveryStrategy</code>. All by magic.</p>
<h2>Non Guice</h2>
<p>If you don&#8217;t want to use Guice, you just need to either use some other DI framework, or manually construct the classes:</p>
<pre class="prettyprint">DeliveryStrategy deliveryStrategy = new EdtDeliveryStrategy();
EventService es = new BasicEventService(deliveryStrategy);</pre>
<h2>Third Party</h2>
<p>And finally, this question:</p>
<blockquote><p>In general how should &#8220;third party&#8221; libraries handle Guice?</p></blockquote>
<p>I don&#8217;t see any downside to me including Guice annotations in ClassBus. You can ignore them if you don&#8217;t use Guice. If you use Guice in your application, then ClassBus will blend right in.</p>
]]></content:encoded>
			<wfw:commentRss>http://stuffthathappens.com/blog/2007/11/14/guice-questions-with-answers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.286 seconds -->
