Spring Boot下的SpringMVC自动配置

本文深入探讨SpringBoot中SpringMVC的自动配置机制,包括视图解析器、静态资源处理、转换器、格式化器及HTTP消息转换器等关键组件的自动注册与自定义方法。了解如何通过扩展配置类实现MVC功能的增强与完全控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringMVC 的自动配置

官方文档:https://docs.spring.io/spring-boot/docs/2.1.3.RELEASE/reference/htmlsingle/#boot-features-spring-mvc
在WebMvcAutoConfiguration 这个类中

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

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

  • Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
    自动配置了ViewResolver(视图解析器:根据方法的返回值得到视图对象(View),视图决定如何渲染(转发?重定向))
    ContentNegotiatingViewResolver: 组合所有的试图解析器
    如何定制:可以自己给容器中添加一个试图解析器;springboot会自动将其组合起来
@Bean  添加组件  MyViewsolver
	public ViewResolver  MyViewsolver(){
		return new HelloViewResolver();
	}
	public static class HelloViewResolver implements ViewResolver{
		@Override
		public View resolveViewName(String s, Locale locale) throws Exception {
			return null;
		}
	}
  • Support for serving static resources, including support for WebJars (covered later in this document)). 静态资源文件夹
  • Automatic registration of Converter, GenericConverter, and Formatter beans.
Converter:转换器(页面上的数据都是文本,根据不一样的要求会进行不一样的类型转换)
Formatter: 格式化(页面上的数据按照一定的格式转化)
自己要自定义格式化转化器:添加到容器中(Converter)

Formatter: 格式化(页面上的数据按照一定的格式转化)

  • Support for HttpMessageConverters (covered later in this document).
    HttpMessageConverter :SpringMVC 用来转换http 请求和响应;例如user 转为JSON 格式
    HttpMessageConverter 是从容器中确定的,获取所有的HttpMessageCOnverter

  • Automatic registration of MessageCodesResolver (covered later in this document).
    错误代码信息生成

  • Static index.html support. 静态首页访问

  • Custom Favicon support (covered later in this document). favicon.ico

  • Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document).初始化:WebDataBinder (请求数据---javaBean )相关的设置也是从容器BeanFactory中获取的,所以我们也可以自己向容器中添加
    If you want to keep Spring Boot MVC features and you want to add additional MVC configuration (interceptors, formatters, view controllers, and other features), you can add your own @Configuration class of type WebMvcConfigurer but without @EnableWebMvc. If you wish to provide custom instances of RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver, you can declare a WebMvcRegistrationsAdapter instance to provide such components.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.

如何修改SpringBoot 的默认设置

模式:

  1. Springboot在自动配置的时候先看用户是否自己配置了(扫描到@Bean/@Compoment),如果用户自己配置,就是用用户自己配置的。如果没有就自动配置;有些组件可以存在多个(ViewResolver),那么会将用户配置和自动配置组合起来
  2. 进行SpringMVC的扩展:重写方法
    自己进行配置 编写配置类@Configuration,是WebConfigurerAdapter 类型(extends);不能标注@EnableWebMvc
    WebConfigurerAdapter 实现了接口WebMvcConfigurer 包含了许多空方法,都是MVC的扩展

设置试图映射:

      单纯的页面的跳转:写一个配置类扩展SpringMVC 
      @Configuration
public void addViewControllers(ViewControllerRegistry registry) {
       //设置一个视图映射
        registry.addViewController("/view").setViewName("message");
    }
如果有数据的传递,还是要使用:
@Controller
 @RequestMapping("/success")
    public String success(Map<String,Object> map){
        map.put("hello","<h1>你好!!</h1>");  //这个数据会被放到请求域中
        map.put("users", Arrays.asList("zhangsan","lisi","wangwu"));
        //返回的页面的路径是:classpath:/thymeleaf/
        return "success";
    }

原理:
SpringMVC在自动配置的时候,容器中所有的webMvcConfigurer 都会一起起作用,包括我们自己的扩展配置也会起作用

  1. 全面接管SpringMVC:在写配置类的时候添加注解@EnableWebMvc 表示:Spring Boot的配置全部都不要,全部由我们自己配置
    在Spring Boot 中会有非常多的xxx.Configuere 帮助我们进行扩展配置
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值