Spring Hello World Example

Every time, while learning any new language/ framework, we start with conventional “Hello World” program 🙂 . In this article, we are going to create a conventional “Hello World” example in spring framework.

We are using Eclipse IDE with Spring Tool Suite plugin for this example. Java 1.8 and Spring framework version – 5.1.2-RELEASE.

Following are the involved steps to create our application :

  • Create a Spring project and add required spring dependency Jars.
  • Add the required Java classes to the project.
  • Add the spring bean configuration XML file.

Create a Spring project and add required spring dependency Jars

First step is to create a Spring project from Eclipse IDE (Make sure that Spring Tool suite eclipse plugin is installed). Select File > New > Spring Legacy Project, fill the project name and select Template as Simple Java and click on Finish button as shown below.

Add the required dependency jars to the class path of created spring project. Following is the screenshot of project with added dependency jar files. (Download the jar files from here)

Add the required Java classes to the project

Create an Interface called MessageService under package com.asb.service and define an abstract method getMessage() as shown below.

package com.asb.service;
public interface MessageService {
public String getMessage();

Add an implementation class for created interface as shown below. This class provides implementation to getMessage() method, which prints “Hello World!!” message to the console. @Service annotation is used to register the defined class as service bean class by spring framework.

package com.asb.service;
import org.springframework.stereotype.Service;
public class MessageServiceImpl implements MessageService
public String getMessage() {
return "Hello World…!!!";

Finally create MainClass.java and add the following code. This class is used to test the application. In this class, we are creating spring application context object to read the configuration file, which contains our bean declaration and configuration details. Spring framework will initialize and load the beans using this configuration file. The getBean() method of application context is used to get registered service bean object.

package com.asb.main;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.asb.service.MessageService;
public class MainClass {
  public static void main(String args[]) {
  ApplicationContext appContext = new ClassPathXmlApplicationContext("beans.xml");
  MessageService service = appContext.getBean("messageServiceImpl", MessageService.class);
  System.out.println("Message ::"+service.getMessage());

Add the spring bean configuration XML file

Final step is to add the spring bean configuration file to the application. This file is used by the spring framework to register the defined classes as beans and these beans are injected by the spring wherever required.

Create a beans.xml file using File > New > New Spring configuration File, enter the name as beans and click on Next button. Select the checkbox context on next window and click on Finish button.

Finally, and add the following highlighted content as shown below. Here, spring <context:component-scan> element scans the provided package and registers annotated classes as spring bean.

< ?xml version="1.0" encoding="UTF-8"?>
< beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<context:component-scan base-package="com.asb.service"/>

Finally, our project structure looks as shown below.

To test our application, right click on MainClass.java file, and choose run as java application. Following is the screenshot of “Hello World” message printed on console.

You may also be interested in