This quick guide example uses Maven to generate a simple Java project structure, and demonstrates how to retrieve Spring bean and prints a “hello world” string.

Technologies used in this article :

  1. Spring 2.5.6
  2. Maven 3.0.3
  3. Eclipse 3.6
  4. JDK
Spring 3 example
For Spring 3, refer to this Maven + Spring 3 hello world example.

1. Generate project structure with Maven

In command prompt, issue following Maven command :

mvn archetype:generate -DgroupId=com.mkyong.common -DartifactId=SpringExamples 
	-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

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

Type “mvn eclipse:eclipse” to convert the newly generated Maven style project to Eclipse’s style project.

mvn eclipse:eclipse

Later, import the converted project into Eclipse IDE.

Create a resources folder
Create a resources “/src/main/resources” folder, the Spring’s bean xml configuration file will put here later. Maven will treat all files under this “resources” folder as resources files, and copy it to output classes automatically.

3. Add Spring dependency

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

File : pom.xml

<project xmlns="" 
		<!-- Spring framework -->

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.

4. Spring bean (Java class)

Create a normal Java class ( at “src/main/java/com/mkyong/common/”. Spring’s bean is just a normal Java class, and declare in Spring bean configuration file later.

package com.mkyong.common;
 * Spring bean
public class HelloWorld {
	private String name;
	public void setName(String name) { = name;
	public void printHello() {
		System.out.println("Hello ! " + name);

5. 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.

File : Spring-Module.xml

<beans xmlns=""
	<bean id="helloBean" class="com.mkyong.common.HelloWorld">
		<property name="name" value="Mkyong" />

6. Review project structure

Review it and make sure the folder structure as follows

spring hello world example

7. Run It

Run, it will load the Spring bean configuration file (Spring-Module.xml) and retrieve the Spring bean via getBean() method.

File :

package com.mkyong.common;
import org.springframework.context.ApplicationContext;
public class App {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext(
		HelloWorld obj = (HelloWorld) context.getBean("helloBean");

8. Output

Hello ! Mkyong

Download Source Code

Download it – (7KB)
Tags :
Founder of and, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

Related Posts

Popular Posts