Tuesday, May 1, 2012

Spring Hello World Example

For running this example we have used MyEclipse 8.6.1, to download Latest version of MyEclipse go to below URL

Step by step tutorial to implement Spring hello world example

Step 1. Create a New Java Project
New -> Java Project




Step 2. Add Project name, Let’s say TestSpring
Add project name and click Finish

Our project in workspace will look something like this

Step 3. Now we will add Spring Capabilities to this project.
Right click on the project -> MyEclipse -> Add Spring Capabilities



By Default Spring 3.0 core libraries is selected as shown below, Now Click Finish


It will add Spring Libraries and Spring configuration file i.e. applicationContext.xml in the source folder as shown below. We will modify this applicationContext.xml latter.

Step 4. Now lets create package and classes
Let’s say we have a package com.javaxp. Now we will create below mentioned classes.

The HelloSpring class has a message property and its value is set using the setMessage() method. The displayMessage() method is used to display the message. The value for the property message will be set using Dependency Injection through an external configuration file i.e. applicationContext.xml


In the TestHelloSpring class we will get the object of HelloSpring through ApplicationContext object and call the method displayMessage() on it.


HelloSpring.java

/* HelloSpring.java */

package com.javaxp;

public class HelloSpring {
 private String message;

 public String getMessage() {
  return message;
 }

 public void setMessage(String message) {
  this.message = message;
 }
 
 public void displayMessage() {
  System.out.println(getMessage());
 }
}

Step 5. Now Modify applicationContext.xml


applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans
 xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
 <bean id="helloSpring" class="com.javaxp.HelloSpring">
  <property name="message" value="Hello World!"></property>
 </bean> 

</beans>

TestHelloSpring.java

/* TestHelloSpring.java */

package com.javaxp;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestHelloSpring {

 public static void main(String[] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  
  HelloSpring helloSpingObject = (HelloSpring) context.getBean("helloSpring");
  
  helloSpingObject.displayMessage();
 }

}

Using bean element Dependency Injection is achieved for the property message. Notice the id property of bean it is the same which is used in TestHelloSpring class to get the object of HelloSpring. And the property message, Here we set the message property to "Hello World!”


Output