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.
Download :

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

 

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() {
		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.

Note :
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
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'.

 

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 'App.java' and select from context menu 'Run As' --> 'Java Application'.

 

11. Console Output

Console
Sep 2, 2012 9:01:13 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@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.springframework.beans.factory.support.DefaultListableBeanFactory@f11404: defining beans [helloWorldBean]; root of factory hierarchy
Hello World!

 

Download Source Code

 

References