<?xml version="1.0"?>
<rss version="2.0">
<channel>
  <title>Weiqi Gao&#039;s Observations - dependency-injection tag</title>
  <link>http://www.weiqigao.com/blog/tags/dependency-injection/</link>
  <description>Sharing My Experience...</description>
  <language>en</language>
  <copyright>Weiqi Gao</copyright>
  <lastBuildDate>Fri, 11 May 2012 12:48:36 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  
  <image>
    <url>http://pebble.sourceforge.net/common/images/powered-by-pebble.gif</url>
    <title>Weiqi Gao&#039;s Observations</title>
    <link>http://www.weiqigao.com/blog/</link>
  </image>
  
  
  <item>
    <title>Dependency Injection Annotations To Come To A JVM Near You.</title>
    <link>http://www.weiqigao.com/blog/2009/05/05/dependency_injection_annotations_to_come_to_a_jvm_near_you.html</link>
    
      
        <description>
          &lt;p&gt;&lt;em&gt;(Via &lt;a href= &#034;http://twitter.com/crazybob/status/1707957663&#034; &gt;Twitter&lt;/a&gt;, where else?)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Remember &lt;a href= &#034;http://www.weiqigao.com/blog/2007/03/18/google_guice_1_0_release_sparks_discussions.html&#034; &gt;this (my blog reaction to Google Guice 1.0)&lt;/a&gt;?  At the time I wondered:&lt;/p&gt;

&lt;blockquote&gt;
&lt;a href= &#034;&#034; &gt;Me&lt;/a&gt;: &lt;h4&gt;Guice, Or Something Like It, Should Be In The JDK&lt;/h4&gt; 
 
&lt;p&gt;About the only thing that makes feel a little bit uncomfortable is the use of Guice specific imports in my application code.&lt;/p&gt; 
 
&lt;p&gt;Wouldn&#039;t it be nice if I don&#039;t have to do a
 
&lt;pre style=&#034;margin-left:3em&#034;&gt;import com.google.inject.*;&lt;/pre&gt; 
 
but do a
 
&lt;pre style=&#034;margin-left:3em&#034;&gt;import java.lang.inject.*;&lt;/pre&gt; 
 
instead?&lt;/p&gt; 
&lt;/blockquote&gt;

&lt;p&gt;It&#039;s been 779 days since then, but &lt;a href= &#034;http://crazybob.org/&#034; &gt;Bob Lee&lt;/a&gt; and &lt;a href= &#034;http://blog.springsource.com/author/rodj/&#034; &gt;Rod Johnson&lt;/a&gt; have just &lt;a href= &#034;http://crazybob.org/2009/05/announcing-javaxinjectinject.html&#034; &gt;made public&lt;/a&gt; a (work-in-progress) JSR &lt;a href= &#034;http://docs.google.com/Doc?id=dd2fhx4z_13cw24s7dj&#034; &gt;JSR ?: Annotations for Dependency Injection&lt;/a&gt; &#034;to standardize a proven, non-controversial set of annotations that make injectable classes portable across frameworks.&#034;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href= &#034;http://google-code-updates.blogspot.com/2009/05/javaxinjectinject.html&#034; &gt;Bob Lee&lt;/a&gt;: Five years ago, &lt;a title=&#034;Spring 1.0&#034; href= &#034;http://www.springsource.org/&#034; &gt;Spring 1.0&lt;/a&gt; brought Java &lt;a title=&#034;dependency injection&#034; href= &#034;http://en.wikipedia.org/wiki/Dependency_injection&#034; &gt;dependency injection&lt;/a&gt; into the mainstream. Three years later, &lt;a title=&#034;Google Guice 1.0&#034; href= &#034;http://code.google.com/p/google-guice/&#034; &gt;Google Guice 1.0&lt;/a&gt; introduced annotation-based dependency injection and made Java programming a little easier. Since then, developers have had to choose between a) writing external configuration or b) importing vendor-specific annotations.&lt;/p&gt;

&lt;p&gt;Today, we hope to give developers the best of both worlds. &lt;b&gt;Google Guice and SpringSource have partnered to standardize a proven, non-controversial set of annotations that make injectable classes portable across frameworks.&lt;/b&gt; At the moment, the set of specified annotations consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;span style=&#034;font-family: &#039;Courier New&#039;;&#034;&gt;@Inject&lt;/span&gt; - Identifies injectable constructors, methods, and fields&lt;/li&gt;
&lt;li&gt;&lt;span style=&#034;font-family: &#039;Courier New&#039;;&#034;&gt;@Qualifier&lt;/span&gt; - Identifies qualifier annotations&lt;/li&gt;
&lt;li&gt;&lt;span style=&#034;font-family: &#039;Courier New&#039;;&#034;&gt;@Scope&lt;/span&gt; - Identifies scope annotations&lt;/li&gt;
&lt;li&gt;&lt;span style=&#034;font-family: &#039;Courier New&#039;;&#034;&gt;@Named&lt;/span&gt; - String-based qualifier&lt;/li&gt;
&lt;li&gt;&lt;span style=&#034;font-family: &#039;Courier New&#039;;&#034;&gt;@Singleton&lt;/span&gt; - Identifies a type that the injector only instantiates once&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One additional interface is specified for use in conjunction with these annotations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;span style=&#034;font-family: &#039;Courier New&#039;;&#034;&gt;Provider&amp;lt;T&gt;&lt;/span&gt; - Provides instances of a type T. For any type T that can be injected, you can also inject &lt;span style=&#034;font-family: &#039;Courier New&#039;;&#034;&gt;Provider&amp;lt;T&gt;&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;
        </description>
      
      
    
    
    
    <comments>http://www.weiqigao.com/blog/2009/05/05/dependency_injection_annotations_to_come_to_a_jvm_near_you.html#comments</comments>
    <guid isPermaLink="true">http://www.weiqigao.com/blog/2009/05/05/dependency_injection_annotations_to_come_to_a_jvm_near_you.html</guid>
    <pubDate>Tue, 05 May 2009 18:23:22 GMT</pubDate>
  </item>
  
  <item>
    <title>Scott Bale: What&#039;s So Great About Guice</title>
    <link>http://www.weiqigao.com/blog/2008/05/08/scott_bale_whats_so_great_about_guice.html</link>
    
      
        <description>
          &lt;p&gt;&lt;a href= &#034;http://puredanger.com/kablooie/&#034; &gt;Scott Bale&lt;/a&gt;&#039;s presentation about &lt;a href= &#034;http://code.google.com/p/google-guice/&#034; &gt;Guice&lt;/a&gt; at the &lt;a href= &#034;http://www.ociweb.com/javasig/&#034; &gt;St. Louis JUG&lt;/a&gt; tonight is well attended and lively.&lt;/p&gt;

&lt;p&gt;You can tell Scott &lt;b&gt;liked&lt;/b&gt; Guice.  He enumerated all the things about Guice that he liked at the beginning of the talk: small, simple powerful, type safe, no xml, easy to use, etc.  It&#039;s been my experience that people either really like Guice, or they don&#039;t care for it at all.&lt;/p&gt;

&lt;p&gt;Scott did the brave thing&amp;mdash;he did the presentation mostly in Eclipse, with only a few slides.  So once again this is one of those &#034;you have to be there to appreciate it&#034; deals.&lt;/p&gt;

&lt;p&gt;The presentation is based around a set of simple examples that covers the basic features of Guice.  Content-wise, this presentation is similar to what you can find in Google Video.  The interesting bits are the conversations with the audience members.  Jeff Grigg, Kyle Cordes, Michael Easter, Rob Smith, Dean Farwell, Jeff Cunningham, et. al. were on hand to make the evening a thought provoking and entertaining one.&lt;/p&gt;

&lt;p&gt;As for me, I was too tired from moving my daughter back home from school this afternoon and couldn&#039;t focus too well on the code on the screen.  And I caught the mentioning of Modules, Injectors, Annotations, Scopes, Providers, SINGLETONs, and the binding DSL.  Oh, and we did not bash Spring Framework.  Honest.&lt;/p&gt;

&lt;p&gt;But no worries, the slides and code samples will be &lt;a href= &#034;http://www.ociweb.com/javasig/knowledgebase/2008-05/index.html&#034; &gt;posted on the website&lt;/a&gt; in a moment.&lt;/p&gt;
        </description>
      
      
    
    
    
    <comments>http://www.weiqigao.com/blog/2008/05/08/scott_bale_whats_so_great_about_guice.html#comments</comments>
    <guid isPermaLink="true">http://www.weiqigao.com/blog/2008/05/08/scott_bale_whats_so_great_about_guice.html</guid>
    <pubDate>Fri, 09 May 2008 03:49:35 GMT</pubDate>
  </item>
  
  <item>
    <title>Google Guice 1.0 Release Sparks Discussions</title>
    <link>http://www.weiqigao.com/blog/2007/03/18/google_guice_1_0_release_sparks_discussions.html</link>
    
      
        <description>
          &lt;p&gt;(&lt;a href= &#034;http://gafter.blogspot.com/index.html&#034; &gt;Neal Gafter&lt;/a&gt; started the trend of referring to him as &lt;em&gt;&#034;crazy&#034; Bob Lee&lt;/em&gt;.)&lt;/p&gt;

&lt;p&gt;After hearing about &lt;a href= &#034;http://www.weiqigao.com/blog/2007/01/20/java_generics_let_the_other_shoe_drop.html#comment1169334943370&#034; &gt;his DI framework&lt;/a&gt; 57 days ago, in the context of my &#034;&lt;a href= &#034;http://www.weiqigao.com/blog/2007/01/20/java_generics_let_the_other_shoe_drop.html&#034; &gt;erase erasure&lt;/a&gt;&#034; discussion, I&#039;m very glad to see &#034;crazy&#034; Bob Lee release Google Guice 1.0 as open source:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href= &#034;http://crazybob.org/2007/03/guice-10.html&#034; &gt;&#034;crazy&#034; Bob Lee&lt;/a&gt;: We&#039;re pleased to announce the open source release of Google&#039;s internal Java dependency injection framework Guice. Guice wholly embraces annotations and generics,&lt;img style=&#034;margin: 10px 5px 10px 0px; float: left;&#034; src=&#034;http://crazybob.org/uploaded_images/juice-770770.gif&#034; alt=&#034;&#034; border=&#034;0&#034;&gt; thereby enabling you to wire together and test objects with less effort than ever before. Annotations finally free you from error-prone, refactoring-adverse string identifiers.&lt;/p&gt;

&lt;p&gt;Guice injects constructors, fields and methods (any methods with any number of arguments, not just setters). Guice includes advanced features such as custom scopes, circular dependencies, static member injection, Spring integration, and AOP Alliance method interception, most of which you can ignore until you need it.&lt;/p&gt;

&lt;p&gt;An earlier version of Guice already powers Struts 2&#039;s plugin architecture. Google has been running Guice in mission critical applications for months, and now you can, too. We hope you enjoy Guice as much as we do.&lt;/p&gt;

&lt;p&gt;&lt;a title=&#034;the Guice home page&#034; href=&#034;http://code.google.com/p/google-guice/&#034;&gt;Guice lives&lt;/a&gt; at Google Code. From there, you&#039;ll find the &lt;a title=&#034;user&#039;s guide&#034; href=&#034;http://docs.google.com/Doc?id=dd2fhx4z_5df5hw8&#034;&gt;user&#039;s guide&lt;/a&gt;, &lt;a title=&#034;Javadocs&#034; href=&#034;http://google-guice.googlecode.com/svn/trunk/javadoc/index.html&#034;&gt;Javadocs&lt;/a&gt;, and &lt;a title=&#034;download&#034; href=&#034;http://code.google.com/p/google-guice/downloads/list&#034;&gt;download&lt;/a&gt;. Please direct any questions to the &lt;a title=&#034;mailing list&#034; href=&#034;http://groups.google.com/group/google-guice&#034;&gt;mailing list&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The release sparked immediate discussions in the Java blogging community:

&lt;ul&gt;
&lt;li&gt;&lt;a href= &#034;http://www.infoq.com/news/2007/03/guice&#034; &gt;InfoQ&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href= &#034;http://www.theserverside.com/news/thread.tss?thread_id=44593&#034; &gt;TheServerSide&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href= &#034;http://stuffthathappens.com/blog/2007/03/09/guicy-good/&#034; &gt;Stuff That Happens&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href= &#034;http://crazybob.org/2007/03/guice-10.html&#034; &gt;crazybob.org&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href= &#034;http://www.oreillynet.com/onjava/blog/2007/03/the_next_spring_google_guice.html&#034; &gt;OnJava.com&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href= &#034;http://forum.springframework.org/showthread.php?p=107546&#034; &gt;SpringFramework Support Forum&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href= &#034;http://jroller.com/page/scolebourne?entry=configuration_in_java_it_sure&#034; &gt;Stephen Colebourne&#039;s Weblog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href= &#034;http://jroller.com/page/fate/20070314&#034; &gt;The BileBlog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href= &#034;http://google-code-updates.blogspot.com/2007/03/guice-googles-internal-java-dependency.html&#034; &gt;Google Code Blog&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It also sparked several rounds of discussions among colleagues in the office here.  I would imagine similar discussions went on in hundreds of other places where Java developers try to solve real world problems.&lt;/p&gt;

&lt;p&gt;Here are some aspects of the discussions that I found interesting:&lt;/p&gt;

&lt;h4&gt;The Nature of Dependency Injection&lt;/h4&gt;

&lt;p&gt;I&#039;ve heard all of the following opinions:

&lt;ul&gt;
&lt;li&gt;DI is just the Strategy pattern.&lt;/li&gt;
&lt;li&gt;DI is just the Factory pattern.&lt;/li&gt;
&lt;li&gt;DI is a pattern on its own, useful with several other patterns.&lt;/li&gt;
&lt;li&gt;I prefer the practice of following the DI pattern without using a third party framework.&lt;/li&gt;
&lt;li&gt;I use Spring&#039;s DI framework and like the result very much.&lt;/li&gt;
&lt;/ul&gt;

&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Wiring&lt;/em&gt; is the term that people use liberally in DI related discussions without too much discrimination.  Everybody assumes that everybody else knows what it means, and that its meaning is clear-cut and uniformly agreed.&lt;/p&gt;

&lt;p&gt;However, depending on the Who, When, Why, What, How of the wiring, it could be made to mean different things:

&lt;ul&gt;
&lt;li&gt;internal component assembly (a Swing programmer wiring a custom Model to a GUI component)&lt;/li&gt;
&lt;li&gt;component configuration (setting logging appenders and levels for components)&lt;/li&gt;
&lt;li&gt;application configuration (selecting to use MySQL vs. Oracle database drivers)&lt;/li&gt;
&lt;li&gt;performance tuning (setting the size of the connection pool)&lt;/li&gt;
&lt;li&gt;end-user preferences (disabling JavaScript in the browser)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While one can do all of the above using Guice (or some other framework like Spring), it is not always productive to mix all these different kinds of &lt;em&gt;wiring&lt;/em&gt; together and treat them as the same thing.&lt;/p&gt;

&lt;h4&gt;Embracing Java 5 Features&lt;/h4&gt;

&lt;p&gt;Two and a half years after the release of Java 5, I think it is time for everybody but the most conservative (&#034;my system is done and making me tons of money, I&#039;m not touching it&#034;) to move on to the new platform.&lt;/p&gt;

&lt;p&gt;I also think it is time for new products and new versions of existing products to exhibit their commitment to the new platform by taking advantage of new language features like annotations and generics.&lt;/p&gt;

&lt;p&gt;Those organizations and developers who still operate in the 1.4 platform need to feel all the pressure that associating with a legacy platform engenders.&lt;/p&gt;

&lt;p&gt;That&#039;s why I&#039;m really glad that Guice is unabashedly embracing annotations and generics and all the rest of the Java 5 language features.&lt;/p&gt;

&lt;p&gt;Already, the source code of Guice is the bed for innovative idioms of Java generics, as Neal Gafter pointed out &lt;a href= &#034;http://gafter.blogspot.com/2006/12/super-type-tokens.html&#034; &gt;here&lt;/a&gt; and &lt;a href= &#034;http://gafter.blogspot.com/2007/03/compact-object-comparator.html&#034; &gt;here&lt;/a&gt;.

&lt;p&gt;Other surprises and a-ha&#039;s as I was browsing through Guice source include an implementation of an annotation, the use of &lt;tt&gt;String.format()&lt;/tt&gt;, and the &lt;tt&gt;&amp;lt;T&gt; T notNull(T t)&lt;/tt&gt; template.&lt;/p&gt;

&lt;h4&gt;Guice Is Extremely Easy To Use&lt;/h4&gt;

&lt;p&gt;Guice does its job with a straightforward user interface.  The &lt;a href= &#034;http://docs.google.com/Doc?id=dd2fhx4z_5df5hw8&#034; &gt;User&#039;s Guide&lt;/a&gt; prints out to about 30 pages of easy to follow recipes.  My experience with API design tells me that this is not an accident.&lt;/p&gt;

&lt;p&gt;I&#039;m not surprised that &lt;a href= &#034;http://stuffthathappens.com/blog/2007/03/09/guicy-good/&#034; &gt;Eric&lt;/a&gt; was able to swap Guice in to replace Spring IOC in a day.&lt;/p&gt;

&lt;p&gt;At this point I must confess that I haven&#039;t worked on a so called Spring/Hibernate project that was so prevalent in the last couple of years.  And my previous experience with an DI/IOC framework consists of me working out the Pico container tutorial (&lt;tt&gt;boy.kiss(girl);&lt;/tt&gt;, cute) and promptly forgot about the details; and multiple attempts at learning the Spring IOC framework, and feeling like hitting a brick wall when it comes to actually write the @#$%ed xml configuration file.&lt;/p&gt;

&lt;p&gt;With Guice there is no XML configuration files, and that&#039;s a big win in my book.&lt;/p&gt;

&lt;h4&gt;Guice, Or Something Like It, Should Be In The JDK&lt;/h4&gt;

&lt;p&gt;About the only thing that makes feel a little bit uncomfortable is the use of Guice specific imports in my application code.&lt;/p&gt;

&lt;p&gt;Wouldn&#039;t it be nice if I don&#039;t have to do a

&lt;pre style=&#034;margin-left:3em&#034;&gt;import com.google.inject.*;&lt;/pre&gt;

but do a

&lt;pre style=&#034;margin-left:3em&#034;&gt;import java.lang.inject.*;&lt;/pre&gt;

instead?&lt;/p&gt;

&lt;p&gt;That, and the fear of the dueling frameworks scenario, where I want to use both frameworks A and B while framework A requires Guice 10.7.19 and framework B requires an incompatible Guice 10.8.1, prompt me to suggest that maybe &lt;a href= &#034;http://www.infoq.com/news/2007/03/guice#view_5326&#034; &gt;Guice, or something like it, ought to be in the JDK&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href= &#034;http://www.infoq.com/news/2007/03/guice#view_5331&#034; &gt;&#034;Keep an eye on JSR 299.&#034;&lt;/a&gt; was the answer.&lt;/p&gt;

&lt;p&gt;So here&#039;s my eye on &lt;a href= &#034;http://jcp.org/en/jsr/detail?id=299&#034; &gt;JSR 299&amp;mdash;Web Beans&lt;/a&gt;.  It&#039;s scope is Java EE rather than Java SE.  But features of Java EE have known to migrate to Java SE.&lt;/p&gt;

&lt;h4&gt;Relation Between DI and Other Patterns&lt;/h4&gt;

&lt;p&gt;Although Dependency Injection is officially a design pattern now (&#034;says who?&#034; you ask. &#034;&lt;a href= &#034;http://en.wikipedia.org/wiki/Dependency_injection&#034; &gt;Wikipedia&lt;/a&gt;.  If it&#039;s on Wikipedia, it must be true, right?&#034;) in many ways it doesn&#039;t feel like a design pattern, although I can&#039;t pin down exactly in what way this pattern is not like the others.&lt;/p&gt;

&lt;p&gt;Viewed in the narrow sense, DI is a mechanism that links a client that makes use of an interface with a concrete implementation of that interface.  Many of the GoF patterns uses the abstract interface/concrete implementation paradigm.  Yet I don&#039;t think it make sense to say that these patterns depend on (or include) a dependency injection subpattern.&lt;/p&gt;

&lt;p&gt;Another clue that dependency injection is different is that it is rarely called dependency injection when done without the assistance of an DI/IOC container&amp;mdash;it&#039;s just initialization code.  It&#039;s almost like the situation with garbage collection&amp;mdash;when done by hand, it&#039;s called memory management.&lt;/p&gt;

&lt;p&gt;If you stop to think about it, there is a duality between a dependency injector and a garbage collector: one is where objects come from to live, the other is where objects go to die.&lt;/p&gt;

&lt;h4&gt;How About That Logo?&lt;/h4&gt;

&lt;p&gt;If you are familiar with my &lt;a href= &#034;http://www.weiqigao.com/blog/2003/10/29/cute_logo_theory.html&#034; &gt;cute logo theory&lt;/a&gt;, you should recognize the genius of announcing Guice with the kid with a glass image.  Although not an official logo of the project, the picture is attractive enough that it may very well serve the purpose of a logo.&lt;/p&gt;

&lt;p&gt;&#034;The first thing I noticed was the image.  It&#039;s so cute, I have to try the Guice.  I took one sip and am hooked!&#034;&lt;/p&gt;
        </description>
      
      
    
    
    
    <comments>http://www.weiqigao.com/blog/2007/03/18/google_guice_1_0_release_sparks_discussions.html#comments</comments>
    <guid isPermaLink="true">http://www.weiqigao.com/blog/2007/03/18/google_guice_1_0_release_sparks_discussions.html</guid>
    <pubDate>Mon, 19 Mar 2007 04:18:38 GMT</pubDate>
  </item>
  
  </channel>
</rss>

