Every time, while learning any new language/ framework, we start with the conventional “Hello World” program 🙂 In this article, we are going to create a conventional “Hello World” example in the 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.
Table of Contents
- Create a Spring “hello world” example project and add spring dependency Jars
- Add the required Java classes to the project
- Add the spring bean configuration XML file
- Conclusion
Create a Spring “hello world” example project and add spring dependency Jars
The first step is to create a Spring project from Eclipse IDE (Make sure to add the Spring Tool suite eclipse plugin). Select File > New > Spring Legacy Project, fill in the project name and select Template as Simple Java, and click on the Finish button as shown below.

Add the required dependency jars to the classpath of created spring project. Following is the screenshot of the 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 the getMessage() method, which prints the “Hello World!!” message to the console.
we can use the @Service annotation to register the defined class as service bean class by spring framework.
package com.asb.service; import org.springframework.stereotype.Service; @Service public class MessageServiceImpl implements MessageService { @Override public String getMessage() { return "Hello World…!!!"; } }
Create a MainClass.java and add the following code. We can use this class to test the application.
In this class, we are creating a 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.
We can use the getBean() method of application context to get the 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
The final step is to add the spring bean configuration file to the application.
The spring framework uses this configuration file to register the defined classes as beans and injects them wherever required.
Create a beans.xml file using File > New > New Spring configuration File, enter the name as beans, and click on the Next button. Select the checkbox context on the next window and click on the Finish button.

Finally, and add the following highlighted content as shown below.
Here, the 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 the MainClass.java file, and choose run as java application.
Following is the screenshot of the “Hello World” message printed on the console.

Conclusion
In this article, we learned how to create a spring “hello world” example web application.