当前位置: 首页 > news >正文

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{@Overridepublic 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为前缀的属性值会和该类做绑定。

http://www.dtcms.com/a/346928.html

相关文章:

  • 第1篇:走进日志框架的世界 - 从HelloWorld到企业级应用
  • C++中, new对象时有哪几种情况会导致new失败
  • piclist+gitee操作指南
  • DeepSeek V3.1深度解析:一个模型两种思维,迈向Agent时代的第一步!
  • 屏幕类型与信号接口
  • 不用 if-else,Spring Boot 怎么知道 ?status=10 是哪个枚举?
  • 全面解析JVM预热:原理、价值与实践指南
  • Mybatis Plus - 代码生成器简单使用
  • SSE实时通信与前端联调实战
  • 内网穿透教程
  • 亚马逊布局墨西哥低价赛道:Amazon Bazaar的战略逻辑与卖家破局路径
  • STM32CubeIDE V1.9.0下载资源链接
  • 水体反光 + 遮挡难题破解!陌讯多模态融合算法在智慧水务的实测优化
  • RAG学习(六)——检索优化技术进阶
  • Sqlserver存储过程
  • 拼豆设计生成器(支持大写字母、数字,颜色自定义)
  • 力扣 30 天 JavaScript 挑战 第38天 (第九题)学习了 语句表达式的区别 高级函数 promise async await 节流
  • 三、Bpmnjs 核心组件与架构介绍
  • 深入剖析结构体内存对齐
  • 达梦数据库巡检常用SQL(一)
  • Base64 编码优化 Web 图片加载:异步响应式架构(Java 后端 + 前端全流程实现)
  • Linux问答题:分析和存储日志
  • [特殊字符] 在 Windows 新电脑上配置 GitHub SSH 的完整记录(含坑点与解决方案)
  • JUC之AQS
  • csrf漏洞学习笔记
  • C++ 20: Concepts 与Requires
  • 告别SaaS数据绑架,拥抱数据主权:XK+独立部署版跨境商城定制,为海外物流企业深度赋能
  • CentOS创建管理员用户feixue并设置密码全教程
  • 【c++进阶系列】:万字详解多态
  • 快速掌握Java非线性数据结构:树(二叉树、平衡二叉树、多路平衡树)、堆、图【算法必备】