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 3.1
  • Maven
  • JDK 1.6
  • 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'.


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

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.

pom.xml
<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">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.javawebtutor</groupId>
	<artifactId>SpringExample</artifactId>
	<packaging>jar</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>SpringExample</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring</artifactId>
			<version>2.5.6</version>
		</dependency>
	</dependencies>
</project>

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.


HelloWorld.java
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.

Spring-Module.xml
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
	<bean id="helloBean" class="com.javawebtutor.HelloWorld">
		<property name="name" value="Mukesh Kumar" />
	</bean>
 
</beans>

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.

Test.java
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(
				"Spring-Module.xml");

		HelloWorld obj = (HelloWorld) context.getBean("helloBean");
		obj.printHello();
	}
}

8. Output

Hello ! Mukesh Kumar
You can download the source code of the example by clicking on the Download link below.

Download this example(src+lib) developed in eclipse




comments powered by Disqus