Friday, November 9, 2012

Step by step Spring Hello World Tutorial


Here it is.. the simple step by step "Hello World" tutorial using the Spring Framework. Here I used Spring's dependency injection concept to inject the value into an object.

We are going to create a new project in Eclipse Java EE IDE and then write a simple Hello World application. We will finally run the application in the Eclipse IDE.

Just jump to Step 3 directly if you already hava JDK 5 (or higher) and Eclipse IDE.

Tools Used:

1) Jdk 5 or Higher.
2) Eclipse 3
3) Spring 3.2.0.RELEASED

Step 1:
Download Java JDK :The first step of Developing an application using Java Programming Language, you need JDK(Java Development Kit). The Spring 3.X at least requires JDK 5. So, make sure you have JDK 5 or above. Open dos prompt if you are using windows and type "java -version". This will display the version of Java installed on your machine as shown below:

C:\>java -version
java version "1.6.0_17"
Java(TM) SE Runtime Environment (build 1.6.0_17-b04)
Java HotSpot(TM) Client VM (build 14.3-b01, mixed mode, sharing)

Make sure the java version is "1.5.x.x" or Higher
If you don't have Java JDK yet then download it through :
http://www.oracle.com/technetwork/java/javase/downloads/index.html

Step 2:
Download “Eclipse IDE for Java EE Developers” from Eclipse official website at http://www.eclipse.org/downloads/
The Current version of Eclipse IDE is Eclipse Juno (4.2)

Installing Eclipse IDE is easy task, just extract the downloaded file and you will find the eclipse.exe in the extracted folder. To run the IDE, double click on the eclipse.exe file.

Step 3:
Download the latest version of Spring 3 from 
http://www.springsource.org/download

For this tutorial we have downloaded spring-framework-3.1.1.RELEASE-with-docs.zip,which contains the documentation and all the required JAR files. the current Release of Spring Framework is 3.1.3.RELEASE.  After downloading just extract the folder you will get the structure like below. We will use these resource later in this tutorial.


Let's start developing "Hello World" example code:

1) I am going to start with a black workspace. Right click in your project Explorer -> New -> Java Project.

2) a 'New Java Project' window will be appear. In the "Project Name" field type the name of your project. then click "Finish".

3) Expand your project you will get a blank 'src' folder along with some detauls JAR files.


4) Our next task is to add all the JAR files into our project build path. For doing this  - right click on your project folder, select 'Build Path'  then select 'Configure Build Path.."

5) Now under the "Libraries" tab select "Add External JARs".


6) Navigate to all the JAR files under the 'dist' folder which we have been downloaded earlier (in Step 3).


7) Along with these JARs, we need to add an another JAR file named "commons-logging-1.1.1.jar"  (version might be different at the time you will download it). just download this JAR file  from here..
http://commons.apache.org/logging/download_logging.cgi
I have been already download this so i am going to navigate this file to our project's build path.


8) You will get a new label "Referenced Libraries" containing our all the external JARs.



9)  Now our next task is to add a package in our blank 'src' folder. Right click on it -> New -> package.


10) Under the name field type the name of your package. for this tutorial type "org.sumit.com.tutorials" then click Finish.


11) Now right click on package -> New -> Class. Under the name field type "MainApp". check mark on 'public static void main' option then Finish.
Repeat #11 (except don't check on 'public static void main' option) and add an another class named "HelloWorld".


# Code for MainApp.java


package org.sumit.com.tutorials;

/**
* @author SUMIT SAM
*
*/

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

@SuppressWarnings("deprecation")
public class MainApp {

public static void main(String[] args){

BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource("Spring.xml"));
HelloWorld helloWorld = (HelloWorld)beanFactory.getBean("helloWorld");
helloWorld.show();
}
}

#Code for HelloWorld.java
package org.sumit.com.tutorials;
/**
* @author SUMIT SAM
*
*/
public class HelloWorld {
private String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

public void show(){
System.out.println(getMessage() +" By Java Help Centre");
}
}

12) Here we need our Spring Configuration file or Bean file. which inject values to the objects. For this tutorials I used an XML file.  Right click on your project folder -> New -> other -> XML file. click on Next.



13) type the name of your spring configuration file. name is user defined(can be anything).



#Code for spring.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="helloWorld" class="org.sumit.com.tutorials.HelloWorld" >
<property name="message" value="Hello World"/>
</bean>
</beans>

14)  Ultimately the final structure of your project would be looks like this. All done :) Just run your project for doing this right click on your project folder -> Run as -> Java Application.

If everything has done right. You will get result "Hello World By Java Help Centre" as output in console like below.
Image

Thank you for going through this tutorial. and Please let me know if you get any problem in this tutorial.

-
JHC Admin