How do you configure Spring MVC in a web application?

Configuring Spring MVC means wiring up the DispatcherServlet, the application context, and the components (controllers, view resolvers) it needs to serve requests. Modern Spring Boot apps auto-configure most of this, but it's worth understanding the underlying pieces, which are still relevant in traditional or legacy setups.

Key Points: • DispatcherServlet must be registered to intercept requests, either declared in web.xml or, in Java config, via a WebApplicationInitializer. • An application context (traditionally applicationContext.xml, or a @Configuration class) declares the beans — controllers, services, repositories — Spring should manage. • @EnableWebMvc (Java config) or <mvc:annotation-driven/> (XML) activates annotation-based mapping so @RequestMapping-style annotations work. • A ViewResolver bean, such as InternalResourceViewResolver, is configured to map logical view names to actual JSP or template files. • Spring Boot replaces most of this manual wiring with auto-configuration triggered by spring-boot-starter-web on the classpath, needing only application.properties tweaks for customization.

Example: In a Spring Boot app, adding spring-boot-starter-web and writing a @RestController is often all that's needed; in a traditional deployment, the same result requires a web.xml entry for DispatcherServlet, an applicationContext.xml with component scanning, and a manually declared ViewResolver bean.

Interview Tip: A concise interview answer is:

"Classic Spring MVC configuration means registering DispatcherServlet, declaring an application context with your beans, enabling annotation-driven mapping, and configuring a ViewResolver for your view technology. Spring Boot automates almost all of this through auto-configuration once spring-boot-starter-web is on the classpath, so in practice I rarely hand-write that wiring anymore."