Spring Tutorial: Chapter 5

Introduction 

 
So far we have covered the setting of the expectations of the classes using constructor injection or the setter injections, and we saw how we can set a single bean expectation. What if you want to inject a dependency which is a collection? Well, Java Spring lets you do that with collection elements of Spring:
  1. <list> : Helps you setup injection of list of values, allowing duplication of values
  2. <set> : Helps you setup injection of set of values, ensuring distinct (i.e. no duplication) values
  3. <map> : Helps you setup injection of name-value pairs, name and value can be of any type.
  4. <props> : Helps you setup injection of name-value pairs, name and value both should be of type String only.
In our "Popular Talent Show" we have a performer who can play more than one instrument at the same time so let us see how we can implement this:
  1. public class BandMan implements Performer   
  2. {  
  3.  private Collection < Instrument > _instrumentList;  
  4.  public void Initialize()   
  5.  {  
  6.   System.out.println("One Man Band: Registering in the show.");  
  7.  }  
  8.  public void Destroy()   
  9.  {  
  10.   System.out.println("One Man Band: Signing off from the show.");  
  11.  }  
  12.  @Override  
  13.  public void Perform() throws PerformaceException   
  14.  {  
  15.   for (Instrument instrument: _instrumentList)   
  16.   {  
  17.    instrument.Play();  
  18.   }  
  19.  }  
  20.  public void setInstruments(Collection < Instrument > instrumentList)   
  21.  {  
  22.   _instrumentList = instrumentList;  
  23.  }  
  24. }   
We have created a new class which would represent our one-man-band army. It has nothing special, just a collection set using the setter method, and in the interface implementation, we iterate over the available instruments and invoke the play method on it.
  
Now our performer would need multiple instruments to play at the same time, so let us go ahead and create a "Flute" instrument for our performer using the "Drum" instrument that is already available:
  1. public class Flute implements Instrument   
  2. {  
  3.  @Override  
  4.  public void Play() {  
  5.   System.out.println("Playing Flute : ....");  
  6.  }  
  7. }  
No magic so far. Next what we need is to declare our performer in the bean configuration file:
  1. <bean id="flute"class="spring.decoded.instruments.Flute"></bean>  
  2. <bean id="edward"class="spring.decoded.talent.show.BandMan"init-method="Initialize"destroy-method="Destroy">  
  3. <property name="instruments">  
  4. <list>  
  5.    <ref bean="drum"/>  
  6.    <ref bean="guitar"/>  
  7. </list>  
  8. </property>  
  9. </bean>  
Notice the bean declaration, that's the important place to focus on and make use of the collection elements of the Spring framework. We have used the list element and given it the references to the two beans, one drum and the other flute.
  
That's all we need to inject the list into the band man class (or bean), and we need to invoke this into the main method just as we did for other beans. Run the application and you should see something like this:
  
Spring.jpg
 
Well, this was as simple as it could be. You can play around with the other collection elements which I mentioned in the beginning of the article, you just need to change the list element to any of the other three elements and set the values appropriately. In the case of name-value pairs:
  1. <bean id="edward" class="spring.decoded.talent.show.BandMan" init-method="Initialize" destroy-method="Destroy">  
  2.          <property name="instruments">  
  3.                   <map>  
  4.                         <entry key="Fute" value-ref="flute" />  
  5.                         <entry key="Drums" value-ref="drum" />  
  6.                   </map>  
  7.        </property>  
  8. </bean>  
And the private member to hold the collection in the BandMan class should be: 
  1. private Map<String, Instrument> _instrumentMap;  
It's so simple and easy, without any hassles. That is it for this article, we will see some more action in future chapters until then enjoy.  


Similar Articles