SpringMVC 自动配置

SpringMVC 自动配置


  • SpringMVC 有关的自动配置,SpringMVC 自动配置会创建很多对象,重点的有:
    • ContentNegotiatingViewResolver 和 BeanNameViewResolver bean
    • 支持提供静态资源,包括对 WebJars 的支持
    • 自动注册 Converter、GenericConveter 和 Formatter bean
    • 对 HttpMessageConverters 的支持
    • 自动注册 MessageCodesResolver
    • 静态 index.html 支持
    • 自动使用 ConfigurableWebBindingInitializer bean

一、WebMvcAutoConfiguration(SpringMVC自动配置)

  • WebMvcAutoConfiguration 是 SpringMVC 自动配置类。

    java 复制代码
    @AutoConfiguration(after = { DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class })
    @ConditionalOnWebApplication(type = Type.SERVLET)
    @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class })
    @ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
    @AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE + 10)
    @ImportRuntimeHints(WebResourcesRuntimeHints.class)
    public class WebMvcAutoConfiguration {
    	//...
    }
    • DispatcherServletAutoConfiguration.class 自动配置 DispatcherServlet。
    • WebMvcConfigurationSupport.calss 配置 SpringMVC 组件。
    • ValidationAutoConfiguration.class 配置 JSR-303 验证器。
    • @ConditionalOnWebApplication(type = Type.SERVLET):应用是基于 Servlet 的 web 应用时有效。
    • @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class }):当项目有 Servlet.class,DispatcherServlet.class 时起作用。

二、DisPatcherServletAutoConfiguration.class(中央调度器自动配置)

  • web.xml 在 SpringMVC 以 xml 文件配置 DispatcherServlet,现在有自动配置完成。

    xml 复制代码
    <servlet>
    	<servlet-name>dispatcher</servlet-name>
    	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    	<init-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>/WEB-INF/spring/dispatcher.xml</param-value>
    	</init-param>
    	<load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    	<servlet-name>dispatcher</servlet-name>
    	<url-pattern>/</url-pattern>
    </servlet-mapping>
  • DispatcherServletAutoConfiguration 自动配置 DispatcherServlet 。作用:

    • ① 创建 DispatcherServlet。
      • @Bean 创建 DispatcherServlet 对象,容器中的名称为 dispatcherServlet。作为 Servlet 的 url-pattern 为 "/"。
    • ② 将 DispatcherServlet 注册成 bean,放到 Spring 容器,设置 load-on-startup=1。
    • ③ 创建 MultipartResolver,用于上传文件。
    • ④ 它的配置类 WebMvcProperties.calss ,前缀是 spring.mvc。

三、WebMvcConfigurationSupport(SpringMVC组件配置类)

  • Spring MVC 组件的配置类,JavaConfig 方式创建 HandlerMappings 接口的多个对象,HandlerAdapters 接口多个对象, HandlerExceptionResolver 相关多个对象 ,PathMatchConfigurer, ContentNegotiationManager,OptionalValidatorFactoryBean, HttpMessageConverters 等这些实例。
    • HandlerMappings:RequestMappingHandlerMapping
    • HandlerAdapter:RequestMappingHandlerAdapter
    • HandlerExceptionResolver:DefaultHandlerExceptionResolver、ExceptionHandlerExceptionResolver(处理 @ExceptionHandler 注解)

四、ServletWebServerFactoryAutoConfiguration(Web服务器配置类)

  • ServletWebServerFactoryAutoConfiguration 配置嵌入式 Web 服务器。

    • EmbeddedTomcat
    • EmbeddedJetty
    • EmbeddedUndertow
  • SpringBoot 检测 classpath 上存在的类,从而判断当前使用的是 Tomcat/Jetty/Undertow 中的哪一个 Servlet Web 服务器,从而决定定义相应的工厂组件。也就是 Web 服务器。

  • 配置类:ServerProperties.class,配置 web server 服务器。

    properties 复制代码
    server.port=3133  #服务器端口(默认8080)
    server.servlet.context-path=/api #(上下文访问路径)
    server.servlet.encoding.charset=utf-8 #request、response 字符编码
    server.servlet.encoding.force=true #强制 request、response 设置 charset 字符编码
    
    server.tomcat.accesslog.directory=D:/logs #日志路径
    server.tomcat.accesslog.enabled=true #启用访问日志
    server.tomcat.accesslog.prefix=access_log #日志文件名前缀
    server.tomcat.accesslog.file-date-format=.yyyy-MM-dd #日志文件日期时间
    server.tomcat.accesslog.suffix=.log #日志文件名称后缀
    server.tomcat.max-http-form-post-size=2000000 #post 请求内容最大值,默认 2M
    server.tomcat.max-connections=8192 #服务器最大连接数

五、SpringMVC 配置文件

bash 复制代码
# 配置中央调度器的访问路径(默认 / 所有路径)
spring.mvc.servlet.path=/course
#Servlet 的加载顺序,越小创建时间越早
spring.mvc.servlet.load-on-startup=0
#时间格式,可以在接受请求参数使用
spring.mvc.format.date-time=yyyy-MM-dd HH:mm:ss
java 复制代码
//测试日期参数
@GetMapping("/param/date")
@ResponseBody public String paramDate(LocalDateTime date){
	return "日期:" + date;
}
//http://localhost:8001/api/course/param/date?date=2024-02-02 12:10:10
java 复制代码
//@DateTimeFormat 格式化日期,可以方法,参数,字段上使用。
//示例:控制器方法接受日期参数

@GetMapping("/test/date")
@ResponseBody public String paramDate(@DateTimeFormat(pattern = "yyyy-MM-ddHH:mm:ss") LocalDateTime date){
	return "日期:" + date;
}

//无需设置:spring.mvc.format.date-time=yyyy-MM-dd HH:mm:ss
//测试:http://localhost:8001/api/test/date?date=2002-10-02 11:22:19

相关推荐
考虑考虑16 小时前
Spingboot4.2.0中的属性
spring boot·后端·spring
RuoyiOffice16 小时前
Flowable只是起点:一套OA背后的单据、资源与协作设计
java·vue.js·spring boot·flowable·企业管理·oa·ruoyi office
迦蓝叶21 小时前
自建 MaaS 卡住的地方从来不是模型:JAiRouter 开源 AI 网关的四个答案
人工智能·spring boot·开源·架构设计·maas·ai 网关
头发还在的女程序员21 小时前
家政小程序系统源码IM已读记录:多端阅读状态怎样保持单调推进
java·spring boot·源码·家政·消息同步
Diligently_1 天前
第 08 篇|单机部署:本地安装与 Docker 最佳实践
运维·spring boot·docker
雪芽蓝域zzs2 天前
第15节:MyBatis-Plus 分页 + 条件查询(不带 Redis)
spring boot
雪芽蓝域zzs2 天前
第14章:SpringBoot3 + MyBatis‑Plus 基础 CRUD(无 Redis)
spring boot
雪芽蓝域zzs2 天前
第16节:全局统一异常优化 + 自定义业务异常
spring boot
CoderYanger3 天前
Java EE 进阶:12.Spring AOP(第二弹)
java·前端·数据库·spring boot·spring·java-ee·mybatis
CoderYanger3 天前
Java EE 进阶:12.Spring AOP(第一弹)
java·开发语言·spring boot·后端·spring·java-ee·交互