SpringMVC相关自动配置

一.概述

文本探讨了SpringBoot的自动装配原理对于有关web的自动配置,其主要有四个配置类。

  • ServletWebServerFactoryAutoConfiguration

自动配置和初始化一个内嵌的 Servlet Web 服务器

  • DispatcherServletAutoConfiguration

用于自动配置 Spring MVC 核心组件DispatcherServlet

  • WebMvcAutoConfiguration

用于自动配置 Spring MVC 基础设施的类,如视图解析器、静态资源处理、类型转换、数据绑定、消息转换器(JSON)、格式化器以及众多默认设置

  • ErrorMvcAutoConfiguration

用于自动配置错误处理机制的类

测试代码如下:

复制代码
package com.example.springdemo.demos.a06;

import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.context.annotation.Import;
import org.springframework.core.type.AnnotationMetadata;

/**
 * @author zhou
 * @version 1.0
 * @description TODO
 * @date 2025/8/23 22:22
 */
public class TestMvcAuto {
    public static void main(String[] args) {
        AnnotationConfigServletWebServerApplicationContext context = new AnnotationConfigServletWebServerApplicationContext();
        context.registerBean(Config.class);
        context.refresh();
        for (String name : context.getBeanDefinitionNames()){
            String source = context.getBeanDefinition(name).getDescription();
            if(source != null){
                System.out.println(name+"来源: "+source);
            }
        }
        context.close();
    }
    @Configuration
    @Import(MyImportSelector.class)
    static class Config{

    }
    static class MyImportSelector implements DeferredImportSelector{
        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            return new String[]{
                    ServletWebServerFactoryAutoConfiguration.class.getName(),
                    DispatcherServletAutoConfiguration.class.getName(),
                    WebMvcAutoConfiguration.class.getName(),
                    ErrorMvcAutoConfiguration.class.getName()
            };
        }
    }
}

测试结果:

复制代码
tomcatServletWebServerFactory来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryConfiguration$EmbeddedTomcat.class]
servletWebServerFactoryCustomizer来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfiguration.class]
tomcatServletWebServerFactoryCustomizer来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/ServletWebServerFactoryAutoConfiguration.class]
dispatcherServlet来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfiguration$DispatcherServletConfiguration.class]
dispatcherServletRegistration来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/DispatcherServletAutoConfiguration$DispatcherServletRegistrationConfiguration.class]
requestMappingHandlerAdapter来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
requestMappingHandlerMapping来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
welcomePageHandlerMapping来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
localeResolver来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
themeResolver来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
flashMapManager来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
mvcConversionService来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
mvcValidator来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
mvcContentNegotiationManager来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
mvcPatternParser来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
mvcUrlPathHelper来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
mvcPathMatcher来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
viewControllerHandlerMapping来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
beanNameHandlerMapping来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
routerFunctionMapping来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
resourceHandlerMapping来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
mvcResourceUrlProvider来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
defaultServletHandlerMapping来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
handlerFunctionAdapter来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
mvcUriComponentsContributor来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
httpRequestHandlerAdapter来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
simpleControllerHandlerAdapter来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
handlerExceptionResolver来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
mvcViewResolver来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
mvcHandlerMappingIntrospector来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
viewNameTranslator来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]
defaultViewResolver来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]
viewResolver来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]
requestContextFilter来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]
formContentFilter来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration.class]
error来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]
beanNameViewResolver来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]
conventionErrorViewResolver来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration$DefaultErrorViewResolverConfiguration.class]
errorAttributes来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.class]
basicErrorController来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.class]
errorPageCustomizer来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.class]
preserveErrorControllerTargetClassPostProcessor来源: class path resource [org/springframework/boot/autoconfigure/web/servlet/error/ErrorMvcAutoConfiguration.class]

可以看到,配置类里面导入了许多有关处理web请求的类,例如:适配器,影射器等等。

二.配置类分析

1.EmbeddedTomcat类的导入

从上面可以看到SpringBoot是怎么导入内嵌Tomcat类的,只有当项目中存在Servlet, Tomcat, UpgradeProtocol类的时候才会导入。

2.DispatcherServletConfiguration类

用于导入DispatcherServlet相关的配置,该类存在的前提是ServletRegistration类存在

并且自动导入WebMvcProperties配置

该类是有关web的相关配置类,项目的主配置文件中以spring.mvc为前缀的属性值会和该类做绑定。

相关推荐
杨杨杨大侠2 分钟前
第1篇:走进日志框架的世界 - 从HelloWorld到企业级应用
java·开源·apache log4j
David爱编程1 小时前
Java 内存模型(JMM)全景图:并发世界的底层基石
java·后端
努力努力再努力wz2 小时前
【c++进阶系列】:万字详解多态
java·linux·运维·开发语言·c++
秦亿凡2 小时前
多线程下为什么用ConcurrentHashMap而不是HashMap
java·开发语言
知其然亦知其所以然2 小时前
SpringAI + Groq 实战:3 分钟教你搭建超快聊天机器人!
java·后端·openai
M1A12 小时前
诺贝尔奖得主的终极学习法:西蒙学习法全解读
后端
阿波罗尼亚2 小时前
ExcelUtils实现 设置内容 插入行 复制行列格式
java·开发语言
Monkey-旭2 小时前
Android 定位技术全解析:从基础实现到精准优化
android·java·kotlin·地图·定位
带刺的坐椅2 小时前
Solon StateMachine 实现状态机使用示例详解
java·solon·状态机
Yyyy4822 小时前
MyCAT高可用
java·运维