What Spring MVC is, its core components, the request lifecycle, the DispatcherServlet's role, WebApplicationContext, configuring MVC with web.xml or Java config, running without web.xml, and servlets vs listeners.
Published September 25, 2026
Every Spring MVC question leads back to one diagram: request → DispatcherServlet → HandlerMapping → HandlerAdapter → Controller → (view or message converter) → response. Learn it once, and you can answer all nine questions below. In Spring Boot, most of the configuration is automatic, but interviewers still ask how it works underneath.
Short answer: Spring MVC is Spring's servlet-based web framework, built around the Model–View–Controller pattern and a central front controller, the DispatcherServlet. Controllers handle requests, the model carries data, and views render HTML. For REST APIs, message converters write JSON instead of rendering a view.
Key points to cover:
spring-boot-starter-web.Learn it in depth → REST Controllers
Short answer:
DispatcherServlet: the front controller.HandlerMapping: finds which handler matches the request.HandlerAdapter: invokes the handler.@Controller or @RestController.Model: the data for the view.ViewResolver: turns a view name into a View.View: renders the output.HttpMessageConverters: convert JSON and XML.HandlerExceptionResolvers: handle errors.HandlerInterceptors: pre- and post-processing.Short answer:
DispatcherServlet.DispatcherServlet asks each HandlerMapping for a handler. RequestMappingHandlerMapping matches @RequestMapping methods, and the handler comes back wrapped in an execution chain with any interceptors.preHandle methods run.HandlerAdapter invokes the controller method. Argument resolvers bind path variables, parameters and the request body (using message converters), and run validation.ViewResolver finds the View, which renders HTML;@ResponseBody/@RestController): an HttpMessageConverter writes JSON directly.postHandle and afterCompletion methods run. If an exception occurred, the exception resolvers (@ExceptionHandler/@ControllerAdvice) produce the error response.Client → Filters → DispatcherServlet → HandlerMapping → Interceptors.preHandle
→ HandlerAdapter → Controller → (ViewResolver → View | MessageConverter → JSON)
→ Interceptors.postHandle/afterCompletion → Response
DispatcherServlet play? How are controllers and view resolvers wired together during a request?Short answer: The DispatcherServlet is the front controller. Every request goes through it, and it orchestrates the whole flow: it delegates handler lookup to HandlerMappings, invocation to HandlerAdapters, view resolution to ViewResolvers, and errors to HandlerExceptionResolvers. It doesn't do any of that work itself. That's what makes each step pluggable, following the Strategy pattern.
Key points to cover:
WebApplicationContext. It falls back to defaults (DispatcherServlet.properties) when none are defined. Spring Boot auto-configuration registers the standard ones.WebApplicationContext?Short answer: It's an ApplicationContext that is aware of the web environment. It knows its ServletContext, and it supports the request, session and application scopes. The DispatcherServlet loads its MVC beans (controllers, view resolvers, handler mappings) from it.
Key points to cover:
ContextLoaderListener, with services, repositories and data sources;DispatcherServlet, with web beans.Short answer: In Spring Boot: add spring-boot-starter-web, and you're done. The DispatcherServlet, Jackson converters, static-resource handling and error pages are all auto-configured. Customise them by implementing WebMvcConfigurer.
In classic Spring:
DispatcherServlet, in web.xml or a WebApplicationInitializer.@EnableWebMvc (or <mvc:annotation-driven/>).ViewResolver (for JSP or Thymeleaf).@Configuration
class WebConfig implements WebMvcConfigurer { // Boot: customise without @EnableWebMvc
@Override public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new RequestTimingInterceptor()).addPathPatterns("/api/**");
}
@Override public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**").allowedOrigins("https://shop.example.com");
}
}
Common trap: adding @EnableWebMvc in a Boot application to "turn on MVC". It disables Boot's MVC auto-configuration, so you lose the Jackson settings, static resources and more.
web.xml or Java config play in setting up Spring MVC?Short answer: It's where the servlet container is told about Spring. It declares the DispatcherServlet and its URL mapping (usually /), points it at its configuration, and optionally registers ContextLoaderListener and filters. Java config does the same thing in code, through WebApplicationInitializer.
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/app-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
web.xml?Short answer: Servlet 3.0+ containers discover a WebApplicationInitializer automatically. The easiest route is to extend AbstractAnnotationConfigDispatcherServletInitializer:
public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] { ServiceConfig.class }; }
@Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[] { WebConfig.class }; } // @EnableWebMvc here
@Override protected String[] getServletMappings() { return new String[] { "/" }; }
}
Key points to cover:
DispatcherServlet as a bean. For WAR deployment you extend SpringBootServletInitializer.Short answer:
DispatcherServlet is the servlet in a Spring MVC app.ContextLoaderListener creates the root application context when the web app starts, and closes it on shutdown. RequestContextListener exposes the current request to non-MVC code.DelegatingFilterProxy, CharacterEncodingFilter and CORS filters are the common examples.Q: What's the difference between a filter and an interceptor?
A: Filters are part of the Servlet API, and run before the DispatcherServlet, on every request, including static resources. They're used for security, encoding, CORS and logging. Interceptors are Spring MVC components that run around handler execution, with access to the chosen handler. They're used for per-controller concerns such as auditing and timing.
Q: Can you have more than one DispatcherServlet?
A: Yes, each with its own context and URL mapping (for example /api/* and /admin/*). It's rarely needed today.
Q: How does Spring MVC choose the JSON library?
A: Through registered HttpMessageConverters. With Jackson on the classpath, MappingJackson2HttpMessageConverter handles application/json, based on the request's Content-Type and Accept headers.
Q: Is Spring MVC thread-safe? A: Each request runs on its own container thread, and controllers are singletons. So controllers must be stateless, just like any singleton service.