Configuring Spring Boot and Spring MVC

In modern Java web development, understanding the relationship between Spring Boot and Spring MVC along with their configuration mechanisms is crucial for building efficient and maintainable applications. Many developers tend to view these two as completely independent components; however, they actually share a close inheritance and dependency relationship: Spring Boot serves as a rapid application development scaffolding based on the Spring Framework, while Spring MVC acts as the core module responsible for handling web requests. Mastering their configuration logic enables developers to quickly scaffold projects and flexibly adjust runtime environments.

The Underlying Principle of Spring Boot Auto-Configuration

The reason Spring Boot can implement the philosophy that "convention overrides configuration" lies in its powerful auto-configuration mechanism (Auto-Configuration). When launching a Maven or Gradle project with the spring-boot-starter-web dependency, the framework automatically scans for specific annotations and metadata files under the classpath. If it detects components related to Spring MVC within the project, Spring Boot will by default introduce and configure the Spring MVC module, including registering core beans such as DispatcherServlet, MVCConfigurerAdapter (deprecated in newer versions), and corresponding view resolvers.

This mechanism greatly simplifies the initial setup process; developers do not need to manually write large amounts of XML or Java Config configuration files to obtain a runnable web application. However, the flexibility of auto-configuration means it is not immutable. When project requirements change, we must use explicit configurations to override default behaviors or add additional functional modules.

The Dependency Management Role of Spring Boot Starter

Before diving into specific configurations, it is essential to clarify the role of the spring-boot-starter-web package. This Starter is not merely a simple dependency; rather, it encapsulates all core libraries required to launch a web application along with their version constraints. By introducing this Starter, Spring Boot automatically handles configuration for embedded containers such as Tomcat, Jetty, or Undertow, while ensuring that classes related to Spring MVC are correctly loaded into the Classpath.

If developers wish to customize container types (for example, using Jetty instead of the default Tomcat), or need to integrate non-standard web framework components, the Spring Boot Starter provides a clear dependency tree structure for us to analyze and manage. This standardized dependency management avoids version conflict issues, allowing Spring MVC configurations across different modules to coexist harmoniously.

Practical Methods for Manually Overriding Auto-Configuration

Although Spring Boot advocates for zero-config startup, in actual production environments, it is often necessary to fine-tune or extend default behaviors. In such cases, we need to intervene in the Spring container initialization process by writing custom configuration classes. The most common and recommended approach involves using the @EnableConfigurationProperties annotation paired with specific property classes, or directly defining a Bean with specific configurations to achieve override effects.

For instance, if one wishes to change the default Tomcat port from the standard 8080 to another value, this can be achieved by creating a new Configuration Class to rewrite relevant properties:

@Configuration
public class CustomMvcConfig {
    @Bean
    public DispatcherServlet dispatcherServlet() {
        return new DispatcherServlet(); // Custom initialization logic
    }
}

Additionally, utilizing the @Conditional annotation allows for enabling or disabling certain auto-configuration modules based on conditions. This is particularly common in microservices architectures; for example, loading corresponding MVC data source configurations only when a specific database driver is detected. This fine-grained control capability ensures that Spring Boot maintains its out-of-the-box convenience while granting developers sufficient flexibility to meet the demands of complex business scenarios.

Customization of View Resolvers and Static Resource Handling

When rendering pages, Spring MVC relies on view resolvers (ViewResolver), whereas Spring Boot provides default auto-configuration options based on FreeMarker, JSP, or Thymeleaf. If a project requires support for multiple template engines, or wishes to map frontend static resources directly to the server root directory rather than processing them through controller forwarding, manual intervention in this configuration is necessary.

This can be achieved by creating a custom InternalResourceViewResolver Bean to specify prefix and suffix formats:

@Configuration
public class ViewConfig {
    @Bean(name = "internalViewResolver")
    public InternalResourceViewResolver internalViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

Simultaneously, regarding the handling of static resources (such as CSS and JS files), Spring Boot 3.x versions introduced a new StaticResources API, allowing developers to configure resource filters and mapping rules more precisely. This is critical for single-page application development under frontend-backend separation architectures, significantly enhancing page load speeds and user experience.

Summary and Best Practice Recommendations

In summary, Spring Boot provides a powerful and flexible runtime environment for Spring MVC; the two are seamlessly integrated through the auto-configuration mechanism. In actual projects, developers should fully utilize default configurations provided by Starters to quickly launch projects while mastering manual override and customization capabilities to address complex business requirements. Whether adjusting container parameters, switching template engines, or optimizing static resource loading strategies, reasonable configuration practices form the foundation for building high-performance Java web applications. It is recommended to develop the habit of reading the spring-boot-starter-web dependency tree during daily development to better understand the internal workings of the framework and perform targeted optimizations.