Spring Hello world example in eclipse

This is 4 of 16 parts of tutorial series

Tutorial Content: Spring tutorial for beginners

    Introduction to spring framework Spring interview questions Dependency injection(ioc) in spring Spring hello world example in eclipse Spring java based configuaration Dependency injection via setter method in spring Dependency injection via constructor in spring Spring Bean scopes with examples Initializing collections in spring Beans Autowiring in spring Inheritance in Spring Spring ApplicationContext Spring lifetime callbacks BeanPostProcessors in Spring Annotation based Configuration in spring Spring AOP tutorial

After having some basic understanding of spring framework now we are ready to create Hello world example.
Firstly download required jars and docs from Spring Downloads | SpringSource.org
Now,In eclipse IDE,click on File->new

Click on other and then select java project

Click on next and  Write project name.I have writtern here "HelloWorldInSpring3"

Click on finish and now our project is created

Create new folder "jars" under src folder so for that right click on project->new->folder

Write folder name as "jars"

click on finish and empy jar folder will be created in src folder.

Now we will add the Spring 3. libraries to the project. Extract the "spring-framework-3.1.1.RELEASE-with-docs.zip" file if you have not extracted. Now go to the "dist" directory of the and then copy all the jar files (Ctrl+C) and paste on the jars directory (of our project) in the Eclipse IDE.

Then find the commons-logging.jar from extracted folder and also copy this file into Eclipse IDE. You will find this library into spring-framework-3.0.0.RELEASE-with-docs\spring-framework-3.1.1.RELEASE\projects\spring-build\lib\ivy folder.

Now add all the jars to "Java Build Path". Right click on the "Spring3HelloWorld" in project explorer and then select properties. Then select "Java Build Path" --> Libraries and then click on the "Add JARs" button. And add all the libraries to Java Build Path.

Create a new package org.arpit.javapostsforlearning to hold the java files. Right click on the "src" folder and then select New --> Package. Then provide the package name as org.arpit.javapostsforlearning and click on the "Finish" button. 

Create a new Java file Spring3HelloWorld.java under the package org.arpit.javapostsforlearning and add the following code:

package org.arpit.javapostsforlearning;

public class Spring3HelloWorld {

    String name;
    public void printHello()
        System.out.println("Hello World from "+name);

    public String getName() {
        return name;

    public void setName(String name) {
        this.name = name;

now create a Xml file called Spring3HelloWorld in src folder.Add following content to it

<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

<bean id="Spring3HelloWorldBean"
<property name="name" value="arpit"/>


Here <beans> tag is for defining multiples beans.All beans of our program are defined in <beans> tag
<bean> tag is for defining a single bean.
"id" is for providing unique identification to bean.
"class" is fully qualified name of class
"property" is used for defining attribute of bean class.

The above xml file declares the spring bean "Spring3HelloWorldBean" of the class org.arpit.javapostsforlearningSpring3HelloWorld.

Create another java file named "Spring3HelloWorldMain.java" under package org.arpit.javapostsforlearning.Copy following content in "Spring3HelloWorldMain.java" file

package org.arpit.javapostsforlearning;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class Spring3HelloWorldMain {

       public static void main(String[] args) {

              ApplicationContext beanFactory = new ClassPathXmlApplicationContext("spring3HelloWorld.xml");

              Spring3HelloWorld myBean = (Spring3HelloWorld) beanFactory.getBean("Spring3HelloWorldBean");


In the above code we have created the instance of ApplicationContext and the retrieved the "Spring3HelloWorldBean". Then we called the printHello() method on the bean.

Run program:

right click on project->Run as->Java Application

Output is :

Hello World from arpit

Source code:

In next post ,we will see dependency injection via setter method.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012