Spring jump start with 'Hello World'

- JavaTouch    - Spring Framework

Introduction to Spring Framework with "Hello World"

In the grand tradition of programming books, we’ll start by showing you how Spring works with the proverbial “Hello World” example. Unlike the original Hello World program, however, our example will be modified a bit to demonstrate the basics of Spring.
Spring-enabled applications are like any Java application. They are made up of several classes, each performing a specific purpose within the application. What makes Spring-enabled applications different, however, is how these classes are configured and introduced to each other. Typically, a Spring application has an XML file that describes how to configure the classes, known as the Spring configuration file.
The first class that our Springified Hello World example needs is a service class whose purpose is to print
the infamous greeting. Listing 1.1 shows GreetingService.java, an interface that defines the contract for our
service class.

Listing 1.1 An interface separates the GreetingService’s implementation from its contract.

 package com.javaTouch.hello;

    publicinterface GreetingService {
        public void sayGreeting();
   }

GreetingServiceImpl.java (listing 1.2) implements the GreetingService interface. Although it’s not necessary to hide the implementation behind an interface, it’s highly recommended as a way to separate the implementation from its contract.

Listing 1.2 GreetingServiceImpl prints a friendly greeting.

 package com.javaTouch.hello;

     public class GreetingServiceImpl implements GreetingService {
         private String greeting;

         public GreetingServiceImpl() {}

         public GreetingServiceImpl(String greeting) {
             this.greeting = greeting;
         }

         public void setGreeting(String greeting) {
             this.greeting = greeting;
         }

         publicvoid sayGreeting() {
             System.out.println(greeting);
         }
     }

 

The GreetingServiceImpl class has a single property: the greeting property. This property is simply a String that holds the text that is the message that will be printed when the sayGreeting() method is called. You may have noticed that the greeting can be set in two different ways: by the constructor or by the property’s setter method.

OK, so far nothing new, its all standerd way of coding in Java right :-)

What’s not apparent just yet is who will make the call to either the constructor or the setGreeting() method to set the property. As it turns out, we’re going to let the Spring container set the greeting property. The Spring configuration file (hello.xml) in listing 1.3 tells the container how to configure the greeting service.

Listing 1.3 Configuring Hello World in Spring (hello.xml).

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
       "
http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
    <bean id="greetingService"
          class="com.javaTouch.hello.GreetingServiceImpl">
          <property name="greeting">
              <value>Hello JavaTouch!</value>
          </property>
    </bean>
</beans>

The XML file in listing 1.3 declares an instance of a GreetingServiceImpl in the Spring container and configures its greeting property with a value of “Hello JavaTouch!” Let’s dig into the details of this XML
file a bit to understand how it works.

At the root of this simple XML file is the <beans> element, which is the root element of any Spring
configuration file. The <bean> element is used to tell the Spring container about a class and how it should be configured. Here, the id attribute is used to name the bean greetingService and the class attribute specifies the bean’s fully qualified class name.

Within the <bean> element, the <property> element is used to set a property, in this case the greeting property. By using <property>, we’re telling the Spring container to call setGreeting() when setting the property.
The value of the greeting is defined within the <value> element. Here we’ve given the example a
Spanish flair by choosing “Hello JavaTouch”.
The following snippet of code illustrates roughly what the container does when instantiating the greeting service based on the XML definition in listing 1.3:

GreetingServiceImpl greetingService = new GreetingServiceImpl();
 greetingService.setGreeting("Buenos Dias!");

Similarly, we may choose to have Spring set the greeting property through GreetingServiceImpl’s single argument constructor. For example:

 <bean id="greetingService"
        class="com.javaTouch.hello.GreetingServiceImpl">
       <constructor-arg>
           <value>Hello JavaTouch!</value>
       </constructor-arg>
 </bean>

The following code illustrates how the container will instantiate the greeting service when using the
<constructor-arg> element:

 GreetingServiceImpl greetingService =
      new GreetingServiceImpl(“Buenos Dias”);

The last piece of the puzzle is the class that loads the Spring container and uses it to retrieve the greeting service. Listing 1.4 shows this class.

Listing 1.4 The Hello World main class.

package com.javaTouch.hello;
  import org.springframework.core.io.FileSystemResource;
  import org.springframework.beans.factory.BeanFactory;
  import org.springframework.beans.factory.xml.XmlBeanFactory;

  publicclass HelloApp {
     public static void main(String[] args) throws Exception {
      BeanFactory factory = new XmlBeanFactory(new FileSystemResource("hello.xml"));
      GreetingService greetingService =  (GreetingService) factory.getBean("greetingService");
        greetingService.sayGreeting();
     }
}

The BeanFactory class used here is the Spring container. After loading the hello.xml file into the
container, the main() method calls the getBean() method on the BeanFactory to retrieve a reference to
the greeting service. With this reference in hand, it finally calls the sayGreeting() method. When we run
the Hello application, it prints (not surprisingly)

Hello JavaTouch!

This is about as simple a Spring-enabled application as we can come up with. But it does illustrate the
basics of configuring and using a class in Spring. Unfortunately, it is perhaps too simple because it only
illustrates how to configure a bean by injecting a String value into a property. The real power of Spring lies in how beans can be injected into other beans using IoC.

 

Guides to develop and run this using your favourite IDE

Download and unzip the latest Spring 2.xx release with dependencies from here..
1. Create a new Java Project.
2. Add following jar files to your projects build path;
     springframework2.xx_HOME/dist/spring.jar
     springframework2.xx_HOME/lib/jakarta-commons/commons-logging.jar
3. Create GreetingService, GreetingServiceImpl and HelloApp classes as above listed.
4. Create a new xml file named hello.xml in the project's root folder and add the content as listing 1.3 above.
5. Now you can run the HelloApp class as a java application. You may modify the value for the property of 'greeting' in hello.xml file and run the application.

The above steps can be applicable for command line run as wel. Just make sure that spring.jar and commons-logging.jar files are available in the class path.