In Spring MVC, configuration is necessary to handle incoming web requests, resolve views and manage controllers. You can configure Spring MVC using either:
- XML-based configuration (traditional approach).
- Java-based configuration (modern, annotation-driven approach).
Both methods serve the same purpose, but the choice depends on the flexibility and maintainability requirements of the project.
XML-Based Configuration
XML-based configuration is the traditional way of configuring Spring applications. In this approach, beans such as controllers, view resolvers and other necessary components are declared in XML files. Though verbose, XML provides a clear and declarative approach, making it ideal for legacy systems.
How It Works:
1. DispatcherServlet is defined in web.xml, mapping requests to a specific URL pattern (e.g., /).
2. A separate XML file (often named dispatcher-servlet.xml) contains the bean definitions for:
- Controllers (@Controller classes).
- View Resolvers (such as InternalResourceViewResolver for JSPs).
- Any other Spring beans required by the application.
3. The web.xml file initializes the DispatcherServlet, which reads from the XML file to know how to handle requests.
Example: Creating XML-Based Spring MVC Configuration
Step 1: Create a New Maven Project
- Open IntelliJ IDEA / Eclipse
- Select New Project → Maven
- Project details: Group Id: com.example, Artifact Id: spring-mvc-xml
- Packaging: war
- Finish project creation
Step 2: Add Required Dependencies (pom.xml)
Add required Spring MVC, Servlet API, and JSP support libraries. Add the following dependencies to pom.xml:
<dependencies>
<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.30</version>
</dependency>
<!-- Spring Web MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.30</version>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- JSP Support -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
Explanation:
- spring-webmvc -> Core Spring MVC framework
- javax.servlet-api -> Required to run on Tomcat
- jstl -> Enables JSTL tags in JSP pages
Step 3: Configure DispatcherServlet (web.xml)
Create web.xml inside WEB-INF:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
version="3.1">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Explanation:
- DispatcherServlet receives all incoming requests
- / mapping means every request goes through Spring MVC
- It automatically loads spring-servlet.xml
Step 4: Create Spring MVC XML Configuration (spring-servlet.xml)
Create spring-servlet.xml inside WEB-INF:
<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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- Enable Spring MVC annotations -->
<mvc:annotation-driven />
<!-- Scan controller classes -->
<context:component-scan base-package="com.example.controller" />
<!-- View Resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
Explanation:
- <mvc:annotation-driven /> -> Enables @Controller, @GetMapping
- <component-scan> -> Finds controller classes automatically
- InternalResourceViewResolver -> Maps view names to JSP files
Step 5: Create Controller Class
Create HomeController.java:
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@GetMapping("/")
public ModelAndView home() {
ModelAndView mv = new ModelAndView("home");
mv.addObject("message", "Hello from XML Configured Spring MVC");
return mv;
}
}
Explanation:
- @Controller -> Marks this as MVC controller
- @GetMapping("/") -> Handles root URL
- "home"-> Resolved to /WEB-INF/views/home.jsp
- addObject() -> Sends data to JSP
Step 6: Create JSP View
Create home.jsp inside /WEB-INF/views/:
<%@ page language="java" contentType="text/html; charset=UTF-8" %>
<html>
<body>
<h2>${message}</h2>
</body>
</html>
Explanation:
- ${message} retrieves model data
- JSP renders final HTML response
Step 7: Configure Tomcat Server
- Download Apache Tomcat 9
- In IDE:
- Add Tomcat Server
- Deploy spring-mvc-xml project
- Start the server
Step 8: Run & Test Application
Open browser and hit:
http://localhost:8080/spring-mvc-xml/
Output:

Java-Based Configuration
Java-based configuration is a modern approach introduced in Spring 3.0, which eliminates the need for XML files by using annotations. With @Configuration, developers can configure Spring in plain Java classes, making the setup more concise and readable.
How It Works:
@EnableWebMvcenables Spring MVC features, such as controller and request mapping.@ComponentScanis used to scan the base packages for annotated components like@Controller.@Configurationclasses are used to configure components like view resolvers, data sources and more.DispatcherServletcan be registered programmatically using WebApplicationInitializer or AbstractAnnotationConfigDispatcherServletInitializer.
Example: Creating Java-Based Spring MVC Configuration
Step 1: Create a New Maven Project
- Open IntelliJ IDEA / Eclipse
- Select New Project -> Maven
- Enter project details:
- Group Id: com.example
- Artifact Id: spring-mvc-java-config
- Set Packaging: war
- Click Finish to create the project
Step 2: Add Dependencies (pom.xml)
Add Spring MVC, Servlet API, and JSTL.
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-mvc-java-config</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<dependencies>
<!-- Spring MVC -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.24</version>
</dependency>
<!-- Servlet API -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- JSTL -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
</project>
Explanation:
- spring-webmvc -> Core Spring MVC
- javax.servlet-api -> Provided by Tomcat
- jstl -> Used in JSP
Step 3: Create web.xml
Create web.xml inside WEB-INF.
<web-app version="3.1">
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
Explanation:
- DispatcherServlet is the front controller
- / mapping sends all requests to Spring MVC
Step 4: Create Java Configuration Class
Create WebConfig.java.
package com.example.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example.controller")
public class WebConfig implements WebMvcConfigurer {
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver resolver =
new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
registry.viewResolver(resolver);
}
}
Explanation:
- @Configuration -> Java config class
- @EnableWebMvc ->Enables Spring MVC features
- @ComponentScan -> Finds controllers
Step 5: Register Java Config with DispatcherServlet
Create a class extending AbstractAnnotationConfigDispatcherServletInitializer.
package com.example.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebInitializer
extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
Explanation:
- Replaces XML-based servlet config
- Connects DispatcherServlet with WebConfig
Step 6: Create Controller Class
Create HomeController.java.
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HomeController {
@GetMapping("/")
public ModelAndView home() {
ModelAndView mav = new ModelAndView("home");
mav.addObject("message",
"Welcome to Spring MVC Java Config");
return mav;
}
}
Explanation:
- @Controller -> Marks MVC controller
- @GetMapping("/") -> Handles root URL
- "home" -> JSP name
- addObject() -> Sends data to JSP
Step 7: Create JSP View
Create home.jsp inside /WEB-INF/views.
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>${message}</h1>
</body>
</html>
Explanation:
- ${message} -> Reads model data
- JSP renders HTML response
Step 8: Deploy on Tomcat
- Deploy WAR file on Apache Tomcat.
- Spring MVC applications require a servlet container.
Step 9: Test the Application
Open browser:
http://localhost:8080/spring-mvc-java-config/
Output:

XML Config vs Java-based Config
XML Configuration | Java-based Configuration |
|---|---|
Syntax style is declarative. | Syntax is programmatic. |
Configuration files are applicationContext.xml or other XML files. | Configuration files are annotated with @Configuration. |
No compile-time checking. | Full compile-time type safety. |
Complex config can be verbose. | Concise and readable. |
Easy to separate config from code. | Tightly coupled with Java code. |
Beginneers friendly. | Requires understanding of Java and annotations. |