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

相关推荐
White graces3 小时前
正则表达式效验邮箱格式, 手机号格式, 密码长度
前端·spring boot·spring·正则表达式·java-ee·maven·intellij-idea
奋斗的袍子0076 小时前
Spring AI + Ollama 实现调用DeepSeek-R1模型API
人工智能·spring boot·深度学习·spring·springai·deepseek
wolf犭良6 小时前
19、《Springboot+MongoDB整合:玩转文档型数据库》
数据库·spring boot·mongodb
小万编程6 小时前
基于SpringBoot+Vue奖学金评比系统(高质量源码,可定制,提供文档,免费部署到本地)
java·spring boot·后端·毕业设计·计算机毕业设计·项目源码
楠枬7 小时前
网页五子棋——匹配模块
java·spring boot·websocket
qq_12498707537 小时前
Java+SpringBoot+Vue+数据可视化的综合健身管理平台(程序+论文+讲解+安装+调试+售后)
java·开发语言·spring boot·毕业设计
qq_12498707538 小时前
Java+SpringBoot+Vue+数据可视化的美食餐饮连锁店管理系统
java·spring boot·毕业设计·美食
m0_748240548 小时前
Springboot项目:使用MockMvc测试get和post接口(含单个和多个请求参数场景)
java·spring boot·后端
Long_poem9 小时前
【自学笔记】Spring Boot框架技术基础知识点总览-持续更新
spring boot·笔记·后端
楠枬10 小时前
网页五子棋——对战后端
java·开发语言·spring boot·websocket·spring