This tutorial will help you to write your first Hello World Spring program. We will use Maven tool to generate the project and Eclipse IDE to implement a method which will print 'Hello World!' in the console.

Tools and Technologies used in this article

  1. Spring 3.1
  2. Maven
  3. JDK 1.6
  4. Eclipse 3.7

Note : Spring 3 requires at least JDK 5. So, make sure you have JDK 5 or above.

1. Create a Java Project using Maven Tool

In the command prompt execute the following command to generate Maven compatible Java project named as 'SpringCoreHelloWorld'.

mvn archetype:generate -DgroupId=com.srccodes.example.spring -DartifactId=SpringCoreHelloWorld -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Generated Maven Java Project structure
Maven Java Project structure

2. Update pom.xml

Add dependency of Spring core and context.
File: pom.xml

<project xmlns="" xmlns:xsi=""

3. Convert to Eclipse compatible Java project

Open the directory 'SpringCoreHelloWorld' in command prompt and run the following maven command.

mvn eclipse:eclipse

Screenshot of command prompt
Maven Java Project

On completion of the above command, Maven Java project will be converted to Eclipse compatible java project.

Eclipse compatible Java Project structure
Eclipse Java Project

4. Import project in Eclipse

Open Eclipse IDE and select from the menu File --> Import --> General --> Existing Projects into Workspace
Existing Projects into Workspace

Browse to the directory of the newly converted Eclipse compatible Java Project and click 'Finish' button.
Browse project

Screenshot of Eclipse project structure
Eclipse project structure

5. Write Spring bean

Create a class 'SpringCoreHelloWorld' under the package 'com.srccodes.example.spring' and copy the following content.

package com.srccodes.example.spring;
 * @author Abhijit Ghosh
 * @version 1.0
public class SpringCoreHelloWorld {
    private String message = null;
    public String getMessage() {
        return message;
    public void setMessage(String message) {
        this.message = message;
    public void sayHello() {

We will use Spring framework to get the instance of this class from Spring runtime and call the 'sayHello()' method to print 'Hello World!' message in the console.

Though Interface based implementation is advisable in Spring Framework, we have not written any interface for our bean class 'SpringCoreHelloWorld'to keep it simple for our first Spring Hello World program.

6. Add Spring Application context Configuration file

Right click on 'main' and select from context menu 'New' --> 'Folder'.
new folder

Enter 'resources' in the 'Folder name' field and click the 'Finish' button.
resources folder

Copy the 'springApplicationContext.xml' file in the 'resources' folder.
File: springApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    <bean id="helloWorldBean" class="com.srccodes.example.spring.SpringCoreHelloWorld">
        <property name="message" value="Hello World!"/>

Note: We have used Spring's setter injection to pass our message 'Hello World!' to 'setMessage' method of 'SpringCoreHelloWorld'. This message is printed by the 'sayHello()' method.

7. Configure Java Build Path

Right click on 'SpringCoreHelloWorld' project and select from context menu 'Properties' --> 'Java Build Path'.
Add 'resources' folder as shown in the screenshot below
Java Build Path

8. Code to interact with Spring's application context

Copy the following code to 'App' class of package 'com.srccodes.example.spring'.

package com.srccodes.example.spring;
import org.springframework.context.ApplicationContext;
 * Hello world!
public class App {
    public static void main(String[] args) {
        // Get Spring application context
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("springApplicationContext.xml");
        // Get a bean instance
        SpringCoreHelloWorld helloWorldBeanInst = applicationContext.getBean("helloWorldBean", SpringCoreHelloWorld.class);
        // Execute SpringCoreHelloWorld bean's method

9. Final project structure

After doing all the changes the overall project structure will look like this
overall project structure

10. Run Your Code

Right click on '' and select from context menu 'Run As' --> 'Java Application'.

11. Console Output


Sep 2, 2012 9:01:13 PM prepareRefresh
INFO: Refreshing startup date [Sun Sep 02 21:01:13 IST 2012]; root of context hierarchy
Sep 2, 2012 9:01:13 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [springApplicationContext.xml]
Sep 2, 2012 9:01:14 PM preInstantiateSingletons
INFO: Pre-instantiating singletons in defining beans [helloWorldBean]; root of factory hierarchy
Hello World!

Download SrcCodes

All code samples shown in this post are available in the following link