9.SpringMVC自动配置原理

官方文档相关说明

在进行项目编写前,我们还需要知道SpringBoot对SpringMVC还做了哪些配置,包括如何扩展,如何定制

我们先去看看SpringBoot的官方文档,点击此处查看


If you want to build servlet-based web applications, you can take advantage of Spring Boot’s auto-configuration for Spring MVC or Jersey.

如果您想构建基于 servlet 的 Web 应用程序,您可以利用 Spring Boot 的 Spring MVC 或 Jersey 自动配置。


The Spring Web MVC framework (often referred to as “Spring MVC”) is a rich “model view controller” web framework. Spring MVC lets you create special @Controller or @RestController beans to handle incoming HTTP requests. Methods in your controller are mapped to HTTP by using @RequestMapping annotations.

Spring Web MVC 框架(通常称为“Spring MVC”)是一个丰富的“模型视图控制器”Web 框架。Spring MVC 允许您创建特殊的@Controller@RestControllerbean 来处理传入的 HTTP 请求。控制器中的方法通过使用@RequestMapping注解映射到 HTTP。


Spring Boot provides auto-configuration for Spring MVC that works well with most applications.

Spring Boot 为 Spring MVC 提供了自动配置,它可以很好地与大多数应用程序一起工作。


The auto-configuration adds the following features on top of Spring’s defaults:

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
  • Support for serving static resources, including support for WebJars (covered later in this document).
  • Automatic registration of Converter, GenericConverter, and Formatter beans.
  • Support for HttpMessageConverters (covered later in this document).
  • Automatic registration of MessageCodesResolver (covered later in this document).
  • Static index.html support.
  • Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).

自动配置在Spring默认设置的基础上添加了以下功能:

  • 包括ContentNegotiatingViewResolverBeanNameViewResolver这两个视图解析器。
  • 支持提供静态资源文件夹的路径,包括对 WebJars 的支持(本文档后面会介绍)。
  • 自动注册Converter、(这就是我们网页提交数据到后台自动封装成为对象的东西,比如把"1"字符串自动转换为 int类型)GenericConverterFormatter(格式化器,比如页面给我们了一个2022-1-1,它会给我们自动格式化为Date对象)bean。
  • 支持HttpMessageConverters(SpringMVC用来转换Http请求和响应的的,比如我们要把一个User对象转换为JSON字符串)(本文档稍后介绍)。
  • 自动注册MessageCodesResolver(用来定义错误代码生成规则的)(本文档稍后介绍)。
  • 静态index.html支持。
  • ConfigurableWebBindingInitializerbean( 初始化数据绑定器:帮我们把请求数据绑定到JavaBean中)的自动使用(本文档稍后会介绍)。

If you want to keep those Spring Boot MVC customizations and make more MVC customizations (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc.

如果您想保留 Spring Boot MVC 功能并进行更多MVC 自定义(拦截器、格式化程序、视图控制器和其他功能),您可以添加自己的@Configuration的类,类型为WebMvcConfigurer添加 @EnableWebMvc


If you want to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, and still keep the Spring Boot MVC customizations, you can declare a bean of type WebMvcRegistrations and use it to provide custom instances of those components.

如果您想提供RequestMappingHandlerMapping``RequestMappingHandlerAdapterExceptionHandlerExceptionResolver 的自定义实例,并且仍然保留 Spring Boot MVC 自定义,则可以声明一个类型为WebMvcRegistrations的 bean并使用它来提供这些组件的自定义实例。


If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc, or alternatively add your own @Configuration-annotated DelegatingWebMvcConfiguration as described in the Javadoc of @EnableWebMvc.

如果你想完全控制 Spring MVC,你可以添加你自己的@Configuration并用@EnableWebMvc进行 注释,或者添加你自己的@Configuration 带注释的 DelegatingWebMvcConfiguration,如@EnableWebMvc


ContentNegotiatingViewResolver

现在我们就来看看这个由 SpringBoot 自动配置了 SpringMVC 的ContentNegotiatingViewResolver 内容协商视图解析器,这就是我们之前学习的SpringMVC的视图解析器,它会根据方法的返回值取得视图对象(View),然后由视图对象决定如何渲染(转发,重定向)。

我们再来看看源码,找到 WebMvcAutoConfiguration 中的 ContentNegotiatingViewResolver,发现这个 ContentNegotiatingViewResolver 是 WebMvcAutoConfigurationAdapter 类的 viewResolver方法返回的结果

		@Bean
		@ConditionalOnBean(ViewResolver.class)
		@ConditionalOnMissingBean(name = "viewResolver", value = ContentNegotiatingViewResolver.class)
		public ContentNegotiatingViewResolver viewResolver(BeanFactory beanFactory) {
   
			//实例化了一个内容协商视图解析器
            ContentNegotiatingViewResolver resolver = new ContentNegotiatingViewResolver();
			//对他的内容协商管理属性进行了设置
            resolver.setContentNegotiationManager(beanFactory.getBean(ContentNegotiationManager.class));
            //ContentNegotiatingViewResolver使用所有其他视图解析器来定位
			// ContentNegotiatingViewResolver uses all the other view resolvers to locate
            //所以它应该有很高的优先级
			// a view so it should have a high precedence
            //设置顺序
			resolver.setOrder(Ordered.HIGHE
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值