<?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>SKORKS &#187; Tutorials</title> <atom:link href="http://www.skorks.com/category/software/tutorials/feed/" rel="self" type="application/rss+xml" /><link>http://www.skorks.com</link> <description>For the betterment of the software craft...</description> <lastBuildDate>Wed, 23 Nov 2011 00:18:05 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.1.2</generator> <item><title>Are You Using The Full Power Of Spring When Injecting Your Dependencies?</title><link>http://www.skorks.com/2008/10/are-you-using-the-full-power-of-spring-when-injecting-your-dependencies/</link> <comments>http://www.skorks.com/2008/10/are-you-using-the-full-power-of-spring-when-injecting-your-dependencies/#comments</comments> <pubDate>Tue, 07 Oct 2008 12:46:16 +0000</pubDate> <dc:creator>Alan Skorkin</dc:creator> <category><![CDATA[Developers]]></category> <category><![CDATA[Software]]></category> <category><![CDATA[Tutorials]]></category> <category><![CDATA[code]]></category> <category><![CDATA[dependency injection]]></category> <category><![CDATA[spring]]></category> <category><![CDATA[spring framework]]></category> <category><![CDATA[tutorial]]></category> <guid
isPermaLink="false">http://www.skorks.com/?p=715</guid> <description><![CDATA[Spring has some extremely powerful capabilities that allow you to do a lot more than just call constructors and setters when creating Spring beans and injecting dependencies. I am going to attempt to show you some ways that will allow you to create spring beans and inject dependencies in all sorts of different ways, which should let you get a lot more out of using Spring.
<strong>Related posts:</strong><ol><li><a
href='http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/' rel='bookmark' title='Ruby Access Control &#8211; Are Private And Protected Methods Only A Guideline?'>Ruby Access Control &#8211; Are Private And Protected Methods Only A Guideline?</a></li><li><a
href='http://www.skorks.com/2010/10/write-a-function-to-determine-if-a-number-is-a-power-of-2/' rel='bookmark' title='Write A Function To Determine If A Number Is A Power Of 2'>Write A Function To Determine If A Number Is A Power Of 2</a></li><li><a
href='http://www.skorks.com/2009/09/true-false-and-nil-objects-in-ruby/' rel='bookmark' title='True, False And Nil Objects In Ruby'>True, False And Nil Objects In Ruby</a></li></ol>]]></description> <content:encoded><![CDATA[<p></p><p>If you’re a serious Java (or possibly even .NET) developer you’ve most likely used the Spring Framework before for all you dependency injection needs. You therefore probably know that there are 2 common ways to inject dependencies into your spring beans:</p><ul><li>setter injection</li><li>constructor injection</li></ul><p>But, did you know that there is a lot more to it than that? Spring has some extremely powerful capabilities that allow you to do a lot more than just call constructors and setters when creating Spring beans and injecting dependencies.</p><p>I am going to attempt to show you some ways that will allow you to create spring beans and inject dependencies in all sorts of different ways, which should let you get a lot more out of using Spring. If you’re a Spring guru then you probably won’t get anything new out of this, however everyone else should hopefully learn something new and become a much more versatile Spring developer.</p><p><strong>Different Ways To Instantiate Beans And Inject Dependencies</strong></p><p>&#160;</p><p><u><em>The Standard Way</em></u></p><p>The simplest way is to create a bean with no dependencies using a default constructor:</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;my.crazy.example.SampleBean&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;sampleBean&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div><p>To inject some dependencies we can use setter injection, constructor injection or a mix of both. If our <em>SampleBean</em> is:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleBean
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> intValue<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> floatValue<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> SampleBean<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      intValue <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setIntValue<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      intValue <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setFloatValue<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span> floatValue<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">floatValue</span> <span style="color: #339933;">=</span> floatValue<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>The Spring configuration could be something like:</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;my.crazy.example.SampleBean&quot;</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;sampleBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;25&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;0.57&quot;</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;floatValue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>All pretty standard stuff, on to more interesting things we go.</p><p>&#160;</p><p><u><em>Using A Static Factory Method</em></u></p><p>We can use the &#8216;factory-method&#8217; attribute to tell spring to call a static factory method to instantiate our bean. So, if we have a <em>SampleBean</em>:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleBean
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> SampleBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>and a <em>SampleBeanFactory</em> with a static method:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleBeanFactory
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> SampleBean createEmptySampleBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> SampleBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>we can do the following:</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;sampleBean&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;my.crazy.example.SampleBeanFactory&quot;</span> </span>
<span style="color: #009900;">                            <span style="color: #000066;">factory-method</span>=<span style="color: #ff0000;">&quot;createEmptySampleBean&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div><p>This will only create a simple bean with no dependencies and what if we want to pass parameters to our factory method? Simple, if our <em>SampleBean</em> is now:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleBean
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> intValue<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> floatValue<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> SampleBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setIntValue<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      intValue <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setFloatValue<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span> floatValue<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">floatValue</span> <span style="color: #339933;">=</span> floatValue<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>and our <em>SampleBeanFactory</em> is:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleBeanFactory
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> SampleBean createSampleBeanWithIntValue<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      SampleBean sampleBean <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SampleBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      sampleBean.<span style="color: #006633;">setIntValue</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> sampleBean<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>We can create a bean in the following way:</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;sampleBean&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;my.crazy.example.SampleBeanFactory&quot;</span> </span>
<span style="color: #009900;">                          <span style="color: #000066;">factory-method</span>=<span style="color: #ff0000;">&quot;createSampleBeanWithIntValue&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;25&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;floatValue&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;0.57&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>The constructor arguments will be passed as parameters to our static factory method and the property elements will be set on the <em>SampleBean</em>. Obviously you can&#8217;t use setter injection to inject parameters into the factory method and you can&#8217;t use constructor injection to inject dependencies into the <em>SampleBean</em>.</p><p>You can see how this would come in useful, especially if you&#8217;re working with existing code that you can&#8217;t change that uses static factory methods to create objects. You can now wire in objects created by static factory methods as spring dependencies. But what if the factory methods are not static, but still create objects that you want to wire in?</p><p>&#160;</p><p><u><em>Using An Instance Factory Method</em></u></p><p>We can use the &#8216;factory-bean&#8217; attribute along with the &#8216;factory-method&#8217; attribute to call instance factory methods to create our beans. Let&#8217;s say our SampleBean is still:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleBean
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> intValue<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> floatValue<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> SampleBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setIntValue<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      intValue <span style="color: #339933;">=</span> value<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setFloatValue<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span> floatValue<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">floatValue</span> <span style="color: #339933;">=</span> floatValue<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>Our factory on the other hand is now a <em>SampleBeanInstanceMethodFactory</em>:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleBeanInstanceMethodFactory
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> uselessParameter<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> SampleBean createSampleBean<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> value<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      SampleBean sampleBean <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SampleBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      sampleBean.<span style="color: #006633;">setIntValue</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> sampleBean<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUselessParameter<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> uselessParameter<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">uselessParameter</span> <span style="color: #339933;">=</span> uselessParameter<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>So, we can create our <em>SampleBean</em> by defining our <em>SampleBeanInstanceMethodFactory</em> as a bean and then using it as a value to the &#8216;factory-bean&#8217; attribute:</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;instanceMethodFactory&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;my.crazy.example.SampleBeanInstanceMethodFactory&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;sampleBean&quot;</span> <span style="color: #000066;">factory-bean</span>=<span style="color: #ff0000;">&quot;instanceMethodFactory&quot;</span> </span>
<span style="color: #009900;">                              <span style="color: #000066;">factory-method</span>=<span style="color: #ff0000;">&quot;createSampleBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;25&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>We can also now pass parameters to our factory when we create it and still pass parameters to our factory method and the <em>SampleBean</em> as well:</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;instanceMethodFactory&quot;</span> </span>
<span style="color: #009900;">    <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;my.crazy.example.SampleBeanInstanceMethodFactory&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;uselessParameter&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;123&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;sampleBean&quot;</span> <span style="color: #000066;">factory-bean</span>=<span style="color: #ff0000;">&quot;instanceMethodFactory&quot;</span> </span>
<span style="color: #009900;">                               <span style="color: #000066;">factory-method</span>=<span style="color: #ff0000;">&quot;createSampleBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;25&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;floatValue&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;0.57&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>This is all similar to what we did with the static factory method, but it gives you the extra level of flexibility to be able to work with instance methods that create objects. But what if I don&#8217;t like any of this factory method nonsense and still want to be really advanced?</p><p>&#160;</p><p><em><u>Using A FactoryBean</u></em></p><p>You can instantiate objects by creating a factory class that implements Spring&#8217;s own FactoryBean interface. This means that you can treat your <em>FactoryBean</em> as if it were itself the object that you&#8217;re trying to create, so you would wire in the <em>FactoryBean</em> anywhere you want to use the actual object you&#8217;re trying to create and Spring will automatically call the right method on the FactoryBean and use the return of that method as the object. This is best demonstrated with an example. The <em>FactoryBean</em> interface has the following methods on it:</p><ul><li>Object getObject()</li><li>Class getObjectType()</li><li>boolean isSingleton()</li></ul><p>You have to implement all three. So, our <em>SampleBean</em> is now:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleBean
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> SampleBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>Our <em>FactoryBean</em> to create the <em>SampleBean</em> will look like this:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleBeanFactoryBean <span style="color: #000000; font-weight: bold;">implements</span> FactoryBean
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> getObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> SampleBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">Class</span> getObjectType<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> SampleBean.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isSingleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>When we want to wire in a SampleBean as a dependency of a <em>FunkyBean</em> that looks like this:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FunkyBean
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> SampleBean sampleBean<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> FunkyBean<span style="color: #009900;">&#40;</span>SampleBean sampleBean<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">sampleBean</span> <span style="color: #339933;">=</span> sampleBean<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>we would do the following:</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;sampleBean&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;my.crazy.example.SampleBeanFactoryBean&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;funkyBean&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;my.crazy.example.FunkyBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;sampleBean&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>As you can see we just wire in the <em>SampleBeanFactoryBean</em> in place of a <em>SampleBean</em> and let Spring do its magic by calling <em>getObject</em>() and returning the output of that as the bean to wire into the <em>FunkyBean</em>, pretty sweet.</p><p> Why is this useful, you ask? Well in our contrived example above it doesn&#8217;t really add much value, but let&#8217;s say you needed to create an object in some really complex fashion. For example, let&#8217;s say you obtain your object, by establishing some kind of network connection and getting some xml sent over-the-wire which you then de-serialize into your object. You then want to treat you new object as spring singleton (yeah I know, not contrived at all :)). Well, <em>FactoryBean</em> is a perfect way to do this. It provides you with a factory method type of approach to creating your object and then lets you easily manage your new object as a Spring singleton. So, if our <em>SampleBean</em> still looks like:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleBean
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> SampleBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>Our <em>SampleBeanFactoryBean</em> would look like this:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SampleBeanFactoryBean <span style="color: #000000; font-weight: bold;">implements</span> FactoryBean
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> SampleBean sampleBean<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> getObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">//ignore the fact that all of this is basically pseudocode</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>sampleBean <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
         NetworkConnection connection <span style="color: #339933;">=</span>
            NetworkConnectionFactory.<span style="color: #006633;">getFunkyNetworkConnection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         <span style="color: #003399;">String</span> beanAsXml <span style="color: #339933;">=</span> connection.<span style="color: #006633;">getSampleBeanAsXml</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
         sampleBean <span style="color: #339933;">=</span> DeSerializer.<span style="color: #006633;">deserialize</span><span style="color: #009900;">&#40;</span>beanAsXml<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000000; font-weight: bold;">return</span> sampleBean<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">Class</span> getObjectType<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> SampleBean.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isSingleton<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">true</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>We would be able to wire in our <em>SampleBean</em> in exactly the same way as before. Ultra useful, but you have to implement a Spring interface which couples your code to Spring somewhat, that&#8217;s the price of power. So, consider carefully, if you don&#8217;t mind being tied to Spring, then there is no problem, but if you want your code to remain unaware of the container you&#8217;re using, you may need to consider a different solution.</p><p>&#160;</p><p><em><u>Using Lookup-Method Injection</u></em></p><p>I thought I would include this one because it is pretty cool and a good feature to be aware of. Essentially, Spring can dynamically override methods on container managed beans. This is actually explained pretty well in the Spring documentation, but here is the quick and dirty version. If you have a singleton bean (<em>SingletonBean</em>) that needs to work with an instance of another bean (which is not a singleton &#8211; <em>PrototypeBean</em>) every time a method on the singleton is invoked, you can do one of 2 things. You can either make your SingletonBean <em>BeanFactoryAware</em> so that you can fetch a different instance of your <em>PrototypeBean</em> every time your singleton needs it or you can use lookup-method injection.</p><p>The first solution is fine but is not ideal if you don&#8217;t want to tie your code to Spring, so this is where the second solution comes in. If your <em>SingletonBean</em> looks like this:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> SingletonBean
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> process<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> someState<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      PrototypeBean prototypeBean <span style="color: #339933;">=</span> grabPrototypeBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      prototypeBean.<span style="color: #006633;">setState</span><span style="color: #009900;">&#40;</span>someState<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000000; font-weight: bold;">return</span> prototypeBean.<span style="color: #006633;">doPrototypeBeanStuff</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">abstract</span> PrototypeBean grabPrototypeBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>and your <em>PrototypeBean</em> is as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> PrototypeBean
<span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> state<span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> doPrototypeBeanStuff<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// do whatever here</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setState<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> someState<span style="color: #009900;">&#41;</span>
   <span style="color: #009900;">&#123;</span>
      state <span style="color: #339933;">=</span> someState<span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><p>Spring can actually override the <em>createPrototypeBean</em>() using lookup-method injection to provide a new instance of a <em>PrototypeBean</em> every time that method is called. Spring does this by using bytecode instrumentation with CGLIB to dynamically subclass the <em>SingletonBean</em> and provide it with an implementation for the <em>createPrototypeBean</em>() method (obviously you will need the CGLIB library in your classpath somewhere). The Spring configuration would be as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;prototypeBean&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;my.crazy.example.PrototypeBean&quot;</span> </span>
<span style="color: #009900;">                                        <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">&quot;prototype&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;singletonBean&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;my.crazy.example.SingletonBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lookup-method</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;grabPrototypeBean&quot;</span> <span style="color: #000066;">bean</span>=<span style="color: #ff0000;">&quot;prototypeBean&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>You can see how this one could come in handy in some situations if you want to keep managing your beans through dependency injection but don&#8217;t want to tie your code to Spring. Plus, it sounds really impressive when you tell people how you were &quot;using bytecode instrumentation to facilitate dynamic lookup-method injection&quot; :).</p><p>Well, that’s it, hopefully you got something new out of this and have more of an understanding of the powerful features that you have at your disposal when you use Spring even for basic things like dependency injection.</p><p> If you would like me to expand on anything that I covered here then please let me know by leaving a comment and I will create a new post or extend this one. Also let me know if you feel like this post should have covered some more ground, perhaps more relevant examples, or possibly there is another cool way to instantiate beans in Spring?</p><p><strong>Related posts:</strong><ol><li><a
href='http://www.skorks.com/2010/04/ruby-access-control-are-private-and-protected-methods-only-a-guideline/' rel='bookmark' title='Ruby Access Control &#8211; Are Private And Protected Methods Only A Guideline?'>Ruby Access Control &#8211; Are Private And Protected Methods Only A Guideline?</a></li><li><a
href='http://www.skorks.com/2010/10/write-a-function-to-determine-if-a-number-is-a-power-of-2/' rel='bookmark' title='Write A Function To Determine If A Number Is A Power Of 2'>Write A Function To Determine If A Number Is A Power Of 2</a></li><li><a
href='http://www.skorks.com/2009/09/true-false-and-nil-objects-in-ruby/' rel='bookmark' title='True, False And Nil Objects In Ruby'>True, False And Nil Objects In Ruby</a></li></ol></p>]]></content:encoded> <wfw:commentRss>http://www.skorks.com/2008/10/are-you-using-the-full-power-of-spring-when-injecting-your-dependencies/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> </channel> </rss>
