Spring Tutorial : Chapter 3

In the previous chapters we saw how to perform constructor injections. Next we will cover how to perform setter injections using the Spring framework. This is another way of resolving dependencies in your class; let's see it using a use case of how one can leverage this.

In our Popular Talent Show we have a new performer, Shaun, who can play any sort of instrument, so to represent that, let's write a generic instrument interface:

public interface Instrument
{
     
void Play();
}

Next we need a class to represent our performer:

public class Percussionist implements Performer
{
     
private Instrument _instrument;     
     
@Override
      public void Perform() throws PerformaceException
      {
           
// TODO Auto-generated method stub           
           
_instrument.Play();           
      }
     
public void setInstrument(Instrument instrument)
      {
           
_instrument = instrument;
      }
}

The class represents a percussionist who plays the instrument injected to it from the setter method; well all looks good but we need the instrument which Shaun would be playing:

public class Drums implements Instrument
{
 
      @Override
      public void Play()
      {
           
// TODO Auto-generated method stub
            System.out.println("Playing Drums : ....");           
      }
 
}

As you can see we have drums which implement the instrument interface and we need this set up in the bean configuration file, so the Spring framework can identify it:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
      <bean id="mike" class="spring.decoded.big.awards.Juggler">
            <constructor-arg value="10" />
      </bean>
     
     
<bean id="bestOfMe" class="spring.decoded.big.awards.Singer" />     
     
<bean id="john" class="spring.decoded.big.awards.SingingJuggler">
            <constructor-arg value="11" />
            <constructor-arg ref="bestOfMe"/>
      </bean>     
     
<bean id="drum" class="spring.decoded.big.awards.Drums" />
      <bean id="shaun" class="spring.decoded.big.awards.Percussionist">
            <property name="instrument" ref="drum" />
      </bean>     
     
<bean id="bigAwardsStage"
            class="spring.decoded.big.awards.Stage"
            factory-method="getIstance" />
</beans>

Notice that we have declared the percussionist too in the bean file, and instead of a constructor-arg attribute we have a property attribute to inject the dependency, what that bean declaration says is that:

"For the property whose name is instrument in the class Percussionist, set it with a reference drum declared in the bean configuration file above". Well, all we need now is to get the instance of this class and invoke the right method which is perform in this case to set the show on fire: In our PopularTalentShow.java class get these lines:

public class PopularTalentShow
{
 
      /**
       *
@param args
       *
@throws PerformaceException
       */
      public static void main(String[] args) throws PerformaceException
      {
           
// TODO Auto-generated method stub           
            System.
out.println("Starting the show....");
          
            ApplicationContext context =
new ClassPathXmlApplicationContext("spring/decoded/big/awards/big-awards.xml");
            Performer performer = (Performer)context.getBean("mike");
           
if(!PrePerformaceChecks(performer, "Mike"))
                 
return;                 
            performer.Perform();           
            System.
out.println("Moving onto next performance : ");           

            performer = (Performer)context.getBean("john");           
           
if(!PrePerformaceChecks(performer, "John"))
                 
return;           
            performer.Perform();           
            System.
out.println("Moving onto next performance : ");
            performer = (Performer)context.getBean(
"shaun");
           
           
if(!PrePerformaceChecks(performer, "Shaun"))
                 
return;           
            performer.Perform();           
     
}
      private static Boolean PrePerformaceChecks(Performer performer, String performerName)
      {
           
if(performer == null)
            {
                  System.
out.println(performerName + " is missing from the show...");
                 
return false;
            }           
           
return true;
      }
 
}

Run the class and you should see it:

spring.gif

Up Next
    Ebook Download
    View all
    Learn
    View all