Reader Level:
ARTICLE

Spring Tutorial: Chapter 1

Posted by Suchit Khanna Articles | Java July 28, 2012
In the first chapter we will look at how to write your first Java Spring based class.
  • 0
  • 0
  • 4162

In the first chapter we will look at how to write your first Java Spring based class. The theme of our application we are going to write in this series will be a "Popular Talent Show", so our talent show will cater to a variety of talents, and we need to cater to the performers, so we will first write an interface:

public interface Performer {

      void Perform() throws PerformaceException;

}

And to ensure that something goes wrong during a performance we have:

public class PerformaceException extends Exception {

      /**
       *
       */
      private static final long serialVersionUID = 3648369653354254918L;

}

Where you can give any message or information, for the time being it is empty with just a UID. So without fail we introduce the first performer who can handle at a given instance n number of spinning balls:

public class Juggler implements Performer {

      private int _ballCount = 5;

      public Juggler(){

      }

      public Juggler(int ballCount){
           
_ballCount = ballCount;
      }

      @Override
      public void Perform() throws PerformaceException {
           
// TODO Auto-generated method stub
            System.out.println("JUGGLING " + _ballCount + " BALLS");
      }
}

As you can see we have an overloaded constructor through which I can inject the count of balls the performer can handle at a given point of time.
Ok, now everything seems to be ready, all we need to do is notify our Spring framework about the new performer: what we need is the XML file through which we are going to do that:

<?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>          

</beans>

The above file is a Spring bean definition file where we define our beans or in plain simple words, we have defined that class Juggler should be identified with the name "Mike" so that whenever he is called upon to ask to perform, we can use this name to call him out.

Another interesting thing to note is that we can explicitly pass various counts of balls to Mike without touching the class definition, how Spring allows us to do that is by the use of: "constructor-arg" which is specified for a given bean (or class in vanilla terms).

So, it's time that we introduce to you the first performer for the day, "Mike":

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(performer == null)

                  System.out.println("Mike is missing from the show...");

           
            performer.Perform();
           
           
      }

Right-click on your class which has the main method and select "Run As" -> "Java Application", if everything goes fine, you should see the following in the console terminal:

Spring.jpg

So that's it for the first chapter, you saw how to set a small class using a Spring container and get its instances. So of all these things what was so great about Spring? Why do so many things to invoke such a trivial method. Well the first thing to make a note of is that no where you used a keyword "new" to instantiate your performer, that was done for you by the Spring framework, which handles the object state* on its own; instead you worry about when and where to allocate (using new) an instance and when not to.

Note: By object state we mean, whether it needs to be a singleton object shared by all callers, or one should get a new instance every time the class or bean definition is queried for from the Spring framework.
 

COMMENT USING

Trending up