Spring Tutorial : Chapter 2

Introduction 

 
Read the first chapter here
 
In this, the second chapter we will cover more details about constructor injection in Spring; until now we saw:
  1. How you are spared from creating objects for your classes and delegate that work to Spring
  2. How to inject primitive values to the classes using constructor injection
In this chapter, we will see how to pass reference arguments to the classes rather than passing primitive types. In our "Popular Talent Show" we saw a juggler so far. Next, we have another performer who can juggle balls as well as sing, both at the same time, which is a very unique talent, so to showcase that let us write an interface first:
  1. public interface Song   
  2. {  
  3.        void Sing();  
  4. }  
Next, we will have a concrete implementation of this type:
  1. public class Singer implements Song   
  2. {  
  3.  private static String[] Lines =   
  4.  {  
  5.   "I'm never lettin' go",  
  6.   "No, no, no, won't let go",  
  7.   "When you want it, when you need it",  
  8.   "You'll always have the best of me",  
  9.   "I can't help it, believe it",  
  10.   "You'll always get the best of me",  
  11.   "I may not always know what's right",  
  12.   "But I know I want you here tonight",  
  13.   "Gonna make this moment last for all your life",  
  14.   "Oh ya this is love and it really means so much",  
  15.   "I can tell from every touch"  
  16.  };  
  17.  @Override  
  18.  public void Sing()   
  19.  {  
  20.   for (int i = 0; i < Lines.length; i++)   
  21.   {  
  22.    System.out.println(Lines[i]);  
  23.   }  
  24.  }  
  25. }  
That's cool. So far, we have a favourite song who our singer would be singing, this popular song of Bryan Adams (his favorite singer) on the show along with Juggling, so let's create a class for the SingingJuggler:
  1. public class SingingJuggler extends Juggler   
  2. {  
  3.  private Song _song;  
  4.  public SingingJuggler(Song song)   
  5.  {  
  6.   super();  
  7.   _song = song;  
  8.  }  
  9.  public SingingJuggler(int beanCount, Song song)   
  10.  {  
  11.   super(beanCount);  
  12.   _song = song;  
  13.  }  
  14.  public void Perform() throws PerformaceException   
  15.  {  
  16.   super.Perform();  
  17.   System.out.println("While singing...");  
  18.   _song.Sing();  
  19.  }  
  20. }   
Great. So we have everything ready, now we need to declare them in the Spring bean definition file:
  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.       <bean id="bestOfMe" class="spring.decoded.big.awards.Singer" />     
  9.       <bean id="john" class="spring.decoded.big.awards.SingingJuggler">  
  10.             <constructor-arg value="11" />  
  11.             <constructor-arg ref="bestOfMe"/>  
  12.       </bean>                  
  13. </beans>  
As we did previously, we declared the Singer class as a bean with id "bestOfMe" to identify it and interestingly our Singing Juggler needs this bean, so we are injecting the same again through a constructor-arg but as a reference, using the "ref" attribute.
  
So, without further delay, let's invite our new performer to showcase his talent: 
  1. public static void main(String[] args) throws PerformaceException   
  2. {  
  3.  System.out.println("Starting the show....");  
  4.  Stage stage = Stage.getIstance();  
  5.  stage.SetUp();  
  6.  ApplicationContext context = new ClassPathXmlApplicationContext("spring/decoded/big/awards/big-awards.xml");  
  7.  Performer performer = (Performer) context.getBean("mike");  
  8.  if (performer == null)  
  9.   System.out.println("Mike is missing from the show...");  
  10.  performer.Perform();  
  11.  System.out.println("Moving onto next performance : ");  
  12.  performer = (Performer) context.getBean("john");  
  13.  if (performer == null)  
  14.   System.out.println("John is missing from the show...");  
  15.  performer.Perform();  
  16. }    
Let's run the application to see the output in the console:
  
Spring.jpg
  
Great, so easy and simple, cheers and enjoy until the next chapter.