SpringMVC 自动配置

本文详细介绍了SpringMVC的自动配置过程,包括WebMvcAutoConfiguration、DispatcherServletAutoConfiguration、WebMvcConfigurationSupport等关键类的作用,以及它们在静态资源支持、HTTP消息转换器和Web服务器配置等方面的应用。
摘要由CSDN通过智能技术生成


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

一、WebMvcAutoConfiguration(SpringMVC自动配置)

  • WebMvcAutoConfiguration 是 SpringMVC 自动配置类。
    @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,现在有自动配置完成。
    <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 服务器。

    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 配置文件

# 配置中央调度器的访问路径(默认 / 所有路径)
spring.mvc.servlet.path=/course
#Servlet 的加载顺序,越小创建时间越早
spring.mvc.servlet.load-on-startup=0
#时间格式,可以在接受请求参数使用
spring.mvc.format.date-time=yyyy-MM-dd HH:mm:ss
//测试日期参数
@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
//@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

  • 26
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小宝945

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值