hello world example in spring

Hello world example in spring framework

We will create a basic hello world example in spring framework. In this tutorial, I will show you step by step process on how you can setup environment using Maven as dependency manager.

What are the things you will need

  1. Install JDK 1.7 as first step. You can download it from Oracle official website http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-javase7-521261.html.
  2. Download Spring tool suite, which is good when you are developing spring projects from their official website https://spring.io/tools/sts/legacy
  3. Install and configure Maven on your system, found this link helpful https://maven.apache.org/install.html
  4. Download and install Apache Tomcat 7.0 as your server from the website https://tomcat.apache.org/download-70.cgi

When you are done with the above installation and configuration, you can start spring tool suite in order to start and create a new project.

For this and every tutorial we will create Maven project only, because we don’t need to worry about our dependencies. Maven will automatically take care of them as part of pom.xml.  

Step 1 : Create a new project

First step will surely be create new project. File -> New -> Maven Project  


Step 2 : Choose default location

Let it as it is and click on the next button. 


Step 3 : Select archtype ( type of the project )

After that select the type of the project you want to create, we will select maven-archtype-webapp for now.


Step 4 : provide Group Id, Artifact Id and package information

Provide below details and click on finish button to create a new maven webapp project.


After clicking finish, you will see the project structure as below.


So now, we are done with the configuration and new project creation part. Next step is to provide spring dependency configuration in pom.xml.

We are using maven in our project, so we don’t need to manually add dependencies to our project library. We need to provide dependency information in pom.xml and rest maven will take care.

Below is the pom.xml configuration file which we are using in this tutorial.

<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">
 <name>TestSpring Maven Webapp</name>



 <!-- Spring 3 dependencies -->

 <!-- Spring MVC -->

 <!-- Spring + aspects -->

 <!-- jstl for jsp page -->

 <!-- logging, slf4j -->



As we know about spring IoC container, Spring IoC container reads metadata information from XML files. We will create a separate spring-beans.xml file which will contain beans definition.  
<beans xmlns="http://www.springframework.org/schema/beans"

 <bean id="testBean" class="com.testspring.beans.TestBean" />


After creating metadata file, we will create actual bean class inside the package com.testspring.beans which is TestBean.

package com.testspring.beans;

public class TestBean {
 public void displayMessage() {
 System.out.println("Welcome to spring test.");

When everything is ready, we will create Main class in order to test our example. For this, we will use ApplicationContext interface.

ApplicationContext is responsible for managing life cycle of the beans. ClassPathXmlApplicationContext will read metadata information from class path resource folder and provide it to Spring IoC container.

package com.testspring.main;

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

import com.testspring.beans.TestBean;

public class MainClass {

 public static void main(String[] args) {
 ApplicationContext context = new ClassPathXmlApplicationContext("spring-beans.xml");

 TestBean testBean = (TestBean) context.getBean("testBean");

  Everything is good 🙂

Related Post

2 thoughts on “hello world example in spring”

  1. Hello!
    Everything is no good 🙁
    I get this error:
    The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path

    1. Reg4b,

      You need to add your runtime library to your build path.

      Right Click on Project -> Properties -> Project Facets -> Go to Runtimes Tab and select Apache Tomcat run time library.

      If this does not work then please add maven dependency in pom.xml.


Leave a Reply

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