Spring 4 Framework Hello World Example

In this tutorial you will learn how to develop a Spring 4 Framework Hello world example. We hope this tutorial will give you a quick start with Spring MVC development using the latest Spring 4 Release.

Here first I downloaded the new release of Spring 4 Framework from here. I have created a simple hello world application using spring 4 mvc. In this particular blog I will explain you guys how to create a simple helloworld application in spring using spring 4 libraries.

Technologies Required:

  • Spring 4.0.1.RELEASE
  • JDK 1.6
  • Tomcat 7.0
  • Eclipse or STS

Project Structure
Before we start adding some controller code lets first take a look at overall project structure in eclipse. Here one thing to note down is that we never needs to add all jar files in spring distribution. I have added only necessary jars here.

Spring 4 Framework Hello World Example

Every web project in java starts with an web.xml file, this is an entry point for application. As we starts the application or deploy the application on server the container searches for an web.xml file and work according to configurations.

There is nothing new in spring 4.0 regarding web.xml file, we need to register the DispatcherServlet here to tell the container that all other upcoming requests are going to be handled by spring itself. From now whenever a request will come to container it will delegate the control to spring configuration file to be processed accordingly.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

We have declared an welcome file in web.xml, this is the first file that the user will see at running project. We have added a simple link here this will call the controller mappings accordingly.

 <title>dineshonjava.com | Hello Spring 4 World</title>
 <h2>dineshonjava.com | Hello Spring 4 World</h2>
 <h4><a href="hello.html">Click Here</a></h4>

This is core of all spring applications, all configurations related to spring goes here in a single file. Here one thing to note down is that the container will detect a file as spring configuration file if the file is ending with ‘-servlet’ suffix. We have defined a base package over here that helps the application to search appropriate ‘url mapping’. Every requested url will be searched in the controller classes defined under ‘base-package’ directory and annotated with ‘@Controller’.

Spring supports a number of view types including, jsp,html,xslt,xml,freemarker etc. We have added a simple view resolved to point our jsp file to be shown as output.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"

 <context:component-scan base-package="com.doj.spring4.controller" />

 <bean id="viewResolver"  class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
    <property name="suffix">


package com.doj.spring4.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

 * @author Dinesh Rajput
public class HelloSpring4Controller {
  public ModelAndView sayHello() {  
   String message = "Welcome to Spring 4.0 !!! dineshonjava.com";  
   return new ModelAndView("hello", "message", message);  

This is a simple controller class for our application, to make a class work as controller we need to add a ‘@Controller’ annotation. To match the appropriate url with controller method add a ‘@RequestMapping’ annotation there enclosed within double cotes.


 <title>dineshonjava.com | Hello Spring 4 World</title>
  <h2>dineshonjava.com | Hello Spring 4 World</h2>

We can generate a War file and deploy that to a web server to test the application. In eclipse , right click the project and Click Run As web application at tomcat server. This will build the project and create a war file in the target folder. In the case of this example the file will be Spring4HelloWorld.war

Deploy this WAR file to a web server , say Tomcat and issue.


Spring 4 Hello World Example


Hello World Example in Spring 4

Download Source Code with Libs

<<Previous <<   || Index ||   >>Next >>

One Response

  1. Anonymous February 21, 2014