Spring Tutorial : Chapter 3

Introduction 

 
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: 
  1. public interface Instrument   
  2. {  
  3.       void Play();  
  4. }  
Next, we need a class to represent our performer: 
  1. public class Percussionist implements Performer   
  2. {  
  3.       private Instrument _instrument;        
  4.       @Override  
  5.       public void Perform() throws PerformaceException   
  6.       {  
  7.             // TODO Auto-generated method stub              
  8.             _instrument.Play();              
  9.       }  
  10.       public void setInstrument(Instrument instrument)  
  11.       {  
  12.             _instrument = instrument;  
  13.       }  
  14. }  
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: 
  1. public class Drums implements Instrument   
  2. {   
  3.       @Override  
  4.       public void Play()   
  5.       {  
  6.             // TODO Auto-generated method stub  
  7.             System.out.println("Playing Drums : ....");              
  8.       }   
  9. }  
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: 
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">   
  5.       <bean id="mike" class="spring.decoded.big.awards.Juggler">  
  6.             <constructor-arg value="10" />  
  7.       </bean>  
  8.         
  9.       <bean id="bestOfMe" class="spring.decoded.big.awards.Singer" />        
  10.       <bean id="john" class="spring.decoded.big.awards.SingingJuggler">  
  11.             <constructor-arg value="11" />  
  12.             <constructor-arg ref="bestOfMe"/>  
  13.       </bean>        
  14.       <bean id="drum" class="spring.decoded.big.awards.Drums" />  
  15.       <bean id="shaun" class="spring.decoded.big.awards.Percussionist">  
  16.             <property name="instrument" ref="drum" />  
  17.       </bean>        
  18.       <bean id="bigAwardsStage"  
  19.             class="spring.decoded.big.awards.Stage"  
  20.             factory-method="getIstance" />  
  21. </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 an 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 performed in this case to set the show on fire: In our PopularTalentShow.java class get these lines:
  1. public class PopularTalentShow   
  2. {   
  3.       /** 
  4.        * @param args 
  5.        * @throws PerformaceException  
  6.        */  
  7.       public static void main(String[] args) throws PerformaceException   
  8.       {  
  9.             // TODO Auto-generated method stub              
  10.             System.out.println("Starting the show....");  
  11.              
  12.             ApplicationContext context = new ClassPathXmlApplicationContext("spring/decoded/big/awards/big-awards.xml");  
  13.             Performer performer = (Performer)context.getBean("mike");  
  14.             if(!PrePerformaceChecks(performer, "Mike"))  
  15.                   return;                    
  16.             performer.Perform();              
  17.             System.out.println("Moving onto next performance : ");             
  18.             performer = (Performer)context.getBean("john");              
  19.             if(!PrePerformaceChecks(performer, "John"))  
  20.                   return;              
  21.             performer.Perform();              
  22.             System.out.println("Moving onto next performance : ");  
  23.             performer = (Performer)context.getBean("shaun");  
  24.               
  25.             if(!PrePerformaceChecks(performer, "Shaun"))  
  26.                   return;              
  27.             performer.Perform();              
  28.       }  
  29.       private static Boolean PrePerformaceChecks(Performer performer, String performerName)  
  30.       {  
  31.             if(performer == null)  
  32.             {  
  33.                   System.out.println(performerName + " is missing from the show...");  
  34.                   return false;  
  35.             }              
  36.             return true;  
  37.       }   
  38. }   
Run the class and you should see it:
 
spring.gif
 


Similar Articles