Spring Hello World example with Eclipse


This tutorial guides you how to start a Spring Application with simple Hello World example.

1. Set up Spring Framework Libraries & Apache Common Logging API

Download the latest version of Spring framework binaries from http://repo.spring.io/release/org/springframework/spring to specific folder.

Unzip the downloaded file, you will see directory structure inside:
Download the latest version of Apache Commons Logging API from http://commons.apache.org/logging/

Unzip the downloaded file, you will see directory structure inside:

2. Create Java Project

Open Eclipse IDE. File -> New -> Project: Choose Java Project.

Name our project as HelloSpring and click Finish:

Once the project is created successfully, the Project Explorer is like:

3. Add Libraries

Right click on project name HelloSpring, Build Path -> Configure Build Path to display the Java Build Path window:

Click Add External JARs… under Libraries tab, browse to Spring Libraries Folder libs and Apache Commons Logging folder that we downloaded at the Step 1, add jar files from those folders. Now Java Build Path Window looks like this:

4. Build Source

Right click on src in Package Explorer section: New -> Package, name it as com.testspring and click Finish.

Creating 2 Java Source File HelloWorld and MainApp by right click on package above, click New -> Class, then click Finish.

Content of HelloWorld.java file:

package com.testspring;

public class HelloWorld {
	private String message;

	public void setMessage(String message) {
		this.message = message;

	public void saySomething() {

Content of MainApp.java file:

package com.testspring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

		HelloWorld obj = (HelloWorld) context.getBean("helloWorld");

First step is to create application context where we used framework API ClassPathXmlApplicationContext. This API loads beans configuration file Beans.xml (which we will create at next step) to create and initialize all the objects (for example, beans mentioned in the configuration file).

Second step is to get required bean using method getBean of the created context. This method uses bean ID helloWorld to return a generic object which finally can be casted to actual object. Once you have object, you can use this object to call any class method.

5. Create Bean Configuration file

Under the src directory, create Beans.xml by right click src, choose New -> Other -> XML: XML File.
The file needs to be created as shown below:

The Beans.xml is used to assign unique IDs to different beans and to control the creation of objects with different values without impacting any of the Spring source files. For example, using below file you can pass any value for “message” variable and so you can print different values of message without impacting HelloWorld.java and MainApp.java files.

When Spring application gets loaded into the memory, Framework makes use of the above configuration file to create all the beans defined and assign them a unique ID as defined in tag. You can use tag to pass the values of different variables used at the time of object creation.
In this case, value Hello World! is passed to field message of class com.testspring.HelloWorld for the bean that has ID helloWorld.

Content of Beans.xml file:


6. Run the program

Keep MainApp.Java file tab active and click Run or use Ctrl + F11 to compile and run. If everything is fine with your application, this will print the following message in Eclipse IDE’s console:

Hello World!

7. Source Code

Hello Spring – java source code

By grokonez | August 28, 2016.

Last updated on September 23, 2018.

Related Posts

Got Something To Say:

Your email address will not be published. Required fields are marked *