Spring 'Hello World' Example using Eclipse

In this tutorial I am going to show how to create and execute a basic Spring program using Eclipse IDE. This example is explained here.

This example is using following technologies:

1) Spring-framework-3.0.2.RELEASE 
2) Eclipse Java EE IDE Helios Release (Eclipse version 3.6)
3) Java SE 6 

Note: The Spring 3.0 requires at least Java 1.5

Step1: Download Spring Framework

Download Spring 3.0.2 release from here. After it unzip the downloaded zip file to an appropriate folder. You will see the following contents under unzipped folder.

The dist folder contains various jar files which you will need for your Spring based application.

Step2: Setting Up Spring Project in Eclipse

Open Eclipse and go to File > New > Java Project a shown below: 

Upon clicking on Java Project you will see following figure.

In the field Project Name, enter the name for project as SpringHelloWorld and click next. Following figure will come up.

Enter value for field Default output Folder as SpringHelloWorld/build and click finish. You will see the following figure.


You can see the newly created SpringHelloWorld project in above figure.

Now your project has been created. Let's add some source code to it.

Step3: Adding Source Code

Our project has three files: 

1) HelloWorld.java

2) MainApp.java (File containing main() function to execute the project)

3) ApplicationContext.xml ( File containing bean configurations)

Let's add these files in eclipse one by one.

Right click on src folder under SpringHelloWorld project and go to New > Class to create a new java class. Enter the name of class as HelloWorld and name of package as examples.spring.helloworld as shown in following figure.

 Similarly create a another class named MainApp.java in the same package.

 Enter following code to both files:

package examples.spring.helloworld;

public class HelloWorld{
public void sayHello(){
System.out.println("Hello World!");

package examples.spring.helloworld;

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

public class MainApp{
public static void main(String args[]){
ApplicationContext ac=new
HelloWorld helloBean=(HelloWorld)ac.getBean("hello");

Under src folder create a new folder named resources.Under resources folder create a new file and name it as ApplicationContext.xml.

Your project structure will look like this:

Add following code to applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

<bean id="hello" class="examples.spring.helloworld.HelloWorld/>


Step4: Adding Jars

In order to run your project you need to add required jars to it. Following jars will be required:

1) org.springframework.beans-3.0.2.RELEASE.jar
2) org.springframework.core-3.0.2.RELEASE.jar
3) org.springframework.context-3.0.2.RELEASE.jar
4) org.springframework.asm-3.0.2.RELEASE.jar
5) org.springframework.expression-3.0.2.RELEASE.jar
6) commons-logging-1.1.1.jar 

You can download commons-logging-1.1.1.jar from here. Other Spring framework related jars can be found in dist folder mentioned in Step 1.

To add above jars right click on your project then go to Build Path > Configure Build Path.You will see following figure.

Click on Add External JARs... . Go to the location where you have stored your jars. Add them.

You can see jars added to SpringHelloWorld Project in the Package Explorer tab as shown in following figure.

Step5: Executing the Program 

Right Click on the project and go to Run As > Java Application.

You will see the output "Hello World!" in the Console Pane at bottom.

Explanation >>

I would like to know your comments and if you liked the article then please share it on social networking buttons.

1 comment:

  1. Hi Rajinder, and thanks so much for creating and posting this example. After many (many!) hours of frustration, your article offered some much-needed guidance.