Spring Framework Tutorial
Software Setup and Configuration (STS/Eclipse/IntelliJ)
Core Spring
Spring Annotations
Spring Data
Spring JDBC
Spring Security
The DispatcherServlet
is at the heart of any Spring web application. It's the front controller that manages the flow of the application. Configuring the DispatcherServlet
correctly is essential for a Spring web application to function.
Here are three ways to configure DispatcherServlet
:
Using web.xml
Configuration (Traditional Servlet Configuration)
Before Spring 3.1 and the introduction of the Servlet 3.0 spec, this was the most common way of configuring DispatcherServlet
.
web.xml
:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
Here, the DispatcherServlet
will load the configuration from spring-dispatcher-servlet.xml
.
Using Java Configuration (Servlet 3.0+)
With Servlet 3.0+ and Spring 3.1+, you can set up the DispatcherServlet
without any XML by implementing WebApplicationInitializer
.
public class MyWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext container) { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setConfigLocation("com.example.config"); ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(context)); registration.setLoadOnStartup(1); registration.addMapping("/"); } }
Here, the configuration classes for the DispatcherServlet
will be searched for in the com.example.config
package.
Using Spring Boot
Spring Boot offers an opinionated way to configure Spring, which greatly simplifies the setup process. With Spring Boot, you typically won't manually configure the DispatcherServlet
as it's auto-configured for you.
Maven dependency for Spring Boot web starter:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
SpringBootApplication
class:
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
In the background, Spring Boot automatically sets up the DispatcherServlet
for you and makes reasonable default decisions based on the libraries present in the classpath.
For most new projects, Spring Boot is the recommended approach because of its simplicity and convention-over-configuration philosophy. However, understanding the traditional configurations provides a deeper understanding of the underlying workings of a Spring MVC application.
Setting up DispatcherServlet using XML in Spring framework:
<!-- web.xml --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
JavaConfig approach to configure DispatcherServlet in Spring:
// AppConfig.java 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.WebMvcConfigurerAdapter; @Configuration @EnableWebMvc @ComponentScan("com.example") public class AppConfig extends WebMvcConfigurerAdapter { // Configuration details... }
Using annotations to configure DispatcherServlet in Spring:
// WebAppInitializer.java import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[]{RootConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{WebConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } }