Home » Spring » Spring HelloWorld Example Using Eclipse and Maven

Spring HelloWorld Example Using Eclipse and Maven

This tutorial will help you to write your first Hello World Spring program.We used Maven and eclipse to develop this project.

Tools and Technologies used in this article :

  • Spring
  • Maven
  • JDK
  • Eclipse Indigo

1. Create a java project using maven’s archetype

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

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

mvn archetype:generate -DgroupId=com.javawebtutor -DartifactId=SpringExample -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Layout of the project’s directory

Spring Application in Eclipse

Maven will generate all the Java’s standard folders structure for you (besides resources folder, which you need to create it manually).

2. Convert to Eclipse project

To convert Maven project to support Eclipse IDE, in terminal, navigate to "SpringExample" project, and issue this command :

mvn eclipse:eclipse

This command generates those files required by the Eclipse IDE, in order for the project to be recognized as a valid Apache Maven project.

Now Import the project into Eclipse IDE.Project directory is given below.

Spring Application in Eclipse

3. Create a resources folder

Create a resources folder inside "src/main" folder. We will create Spring’s bean xml configuration file in that folder. Maven will treat all files under this "resources" folder as resources files, and copy it to output classes automatically.

4. Add Spring dependency

Add Spring dependency in Maven’s pom.xml file.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

Issue "mvn eclipse:eclipse" again, Maven will download the Spring dependency libraries automatically and put it into your Maven’s local repository. At the same time, Maven will add the downloaded libraries into Eclipse ".classpath" for dependency purpose.

5. Write Spring bean

Create a normal Java class HelloWorld.java at “src/main/java/com/javawebtutor" package. This class is a normal java bean class.This class must be declared inside Spring bean configuration file.


package com.javawebtutor;

public class HelloWorld {
	private String name;
	public void setName(String name) {
		this.name = name;
	public void printHello() {
		System.out.println("Hello ! " + name);

5. Create Spring bean configuration file

Create an xml file (Spring-Module.xml) at “src/main/resources/Spring-Module.xml“. This is the Spring’s bean configuration file, which declares all the available Spring beans.


<beans xmlns="http://www.springframework.org/schema/beans"
	<bean id="helloBean" class="com.javawebtutor.HelloWorld">
		<property name="name" value="Mukesh Kumar" />

In the Spring-module.xml file we have configured HelloWorld class instance with unique ID.The helloBean bean is injected into the HelloWorld Bean class using the name property.

Final Project Structure

Review it and make sure the folder structure as follows.

Spring Application in Eclipse

Let us create the Test class having the main() method. This class uses the ApplicationContext container provided by Spring.

6. Run It

Create a java class Test inside com.javawebtutor package and add following code into this class.This class will load the Spring bean configuration file (Spring-Module.xml) and retrieve the Spring bean via getBean() method.


package com.javawebtutor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
		HelloWorld obj = (HelloWorld) context.getBean("helloBean");

8. Output

Hello ! Mukesh Kumar

You can download the source code of the example by clicking on the Download link below.


Previous Next Article
comments powered by Disqus