Spring 3 Hello World Example / Tutorial using Maven Tool and Eclipse IDE
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 :
Spring 3.1
Maven
JDK 1.6
Eclipse 3.7
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=falseGenerated Maven Java Project structure

2. Update pom.xml
Add dependency of Spring core and context.
File : pom.xml
3. Convert to Eclipse compatible Java project
Open the directory 'SpringCoreHelloWorld' in command prompt and run the following maven command.
mvn eclipse:eclipseScreenshot of command prompt

On completion of the above command, Maven Java project will be converted to Eclipse compatible java project.
Eclipse compatible Java Project structure
4. Import project in Eclipse
Open Eclipse IDE and select from the menu File --> Import --> General --> Existing Projects into Workspace

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


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() { System.out.println(getMessage()); } }
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'.

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

Copy the 'springApplicationContext.xml' file in the 'resources' folder.
File: springApplicationContext.xmlWe 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

8. Code to interact with Spring's application context
Copy the following code to 'App' class of package 'com.srccodes.example.spring'.
9. Final project structure
After doing all the changes the overall project structure will look like this

10. Run Your Code
Right click on 'App.java' and select from context menu 'Run As' --> 'Java Application'.
11. Console Output
ConsoleSep 2, 2012 9:01:13 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh INFO: Refreshing org[email protected]145d068: 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 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons INFO: Pre-instantiating singletons in org.s[email protected]f11404: defining beans [helloWorldBean]; root of factory hierarchy Hello World!
Download Source Code