Reader Level:
ARTICLE

Spring Tutorial: Chapter 5

Posted by Suchit Khanna Articles | Java August 16, 2012
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. If you want to inject a dependency which is a collection then Java Spring lets you do that with collection elements.
  • 0
  • 0
  • 2842

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:

public class BandMan implements Performer {

     

      private Collection<Instrument> _instrumentList;

     

      public void Initialize(){

            System.out.println("One Man Band: Registering in the show.");

      }

     

      public void Destroy(){

            System.out.println("One Man Band: Signing off from the show.");  

      }

 

      @Override

      public void Perform() throws PerformaceException {

            // TODO Auto-generated method stub

            for(Instrument instrument : _instrumentList){

                  instrument.Play();

            }

      }

     

      public void setInstruments(Collection<Instrument> instrumentList){

            _instrumentList = instrumentList;

      }

 

}

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:

public class Flute implements Instrument {

 

      @Override

      public void Play() {

            // TODO Auto-generated method stub

           

            System.out.println("Playing Flute : ....");

           

      }

 

}

No magic so far. Next what we need is to declare our performer in the bean configuration file:

<bean id="flute" class="spring.decoded.instruments.Flute">

</bean>

 

<bean id="edward" class="spring.decoded.talent.show.BandMan" init-method="Initialize" destroy-method="Destroy">

            <property name="instruments">

                  <list>

                        <ref bean="drum"/>

                        <ref bean="guitar"/>

                  </list>

            </property>
      </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:

<bean id="edward" class="spring.decoded.talent.show.BandMan" init-method="Initialize" destroy-method="Destroy">

            <property name="instruments">

                  <map>

                        <entry key="Fute" value-ref="flute" />

                        <entry key="Drums" value-ref="drum" />

                  </map>

            </property>

      </bean>

And the private member to hold the collection in the BandMan class should be:

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.
 

COMMENT USING

Trending up