SpringBoot(二)SpringMVC自动配置

一、SpringMVC自动配置
根据SpringBoot官方所说的,SpringBoot为SpringMVC做了如下的默认配置:

  1. Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
    SpringBoot自动装配了视图解析器,ContentNegotiatingViewResolver是用来组合所有的视图解析器的。

  2. Support for serving static resources, including support for WebJars (covered later in this document)).
    支持静态文件的路径,支持webjars。

  3. Automatic registration of Converter, GenericConverter, and Formatter beans.
    自动注册了转换器和格式化器。转换器和格式化器分别如下所示:
    转换器:public String hello(User user):类型转换使用Converter,也就是自动将User对象变成String对象。(应该是调用user的toString()方法)
    格式化器:一般和日期格式化有关。

  4. Support for HttpMessageConverters (covered later in this document).
    HttpMessageConverters是SpringMVC用来转换Http请求和响应的。
    如果要自定义的话,就把自己的组件注册到容器中。

  5. Automatic registration of MessageCodesResolver (covered later in this document).
    定义错误代码生成规则

  6. Automatic use of a ConfigurableWebBindingInitializer bean (covered later in this document)
    ConfigurableWebBindingInitializer是Web数据绑定器,请求数据绑定到JavaBean中。如果没有自定绑定器,它会调用默认的绑定器。

二、添加部分自定义的SpringMVC配置
上面讲的都是SpringBoot提供的默认的SringMVC配置,也可以自定义部分的SpringMVC配置。
关于自定义配置,官方原文: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.
翻译一下就是:假如你需要扩展配置SpringBoot的话,你只需要创建一个配置类继承,给这个类添加 @Configuration 标注,并继承WebMvcConfigurer ,并且不能有@EnableWebMvc。或者申明一个WebMvcRegistrationsAdapter实例并且将其加入到容器当中。
原理:我们的扩展配置类会和自动配置类放入同一个容器,SpringBoot在装载配置的时候,会将所有配置都取出,所以我们的配置和SpringBoot都会生效。当然,以上代码只是对WebMvc的扩展配置,其他配置可以参考以上模式,只要找到相对应的XXXconfigurer接口进行实现即可。

例子:

@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
 
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //浏览器请求/baozhou,映射到success
        registry.addViewController("/baozhou").setViewName("success");
    }
}

三、全面自定义SpringMVC配置(不实用SpringBoot的默认配置)
假如我们要完全自己配置SpringMVC,我只要在我们的配置文件上添加@EnableWebMvc,就可以做到完全托管。(实际上用的可能性很小,大部分都是用默认的额配置)
@EnableWebMVC的原理:这是一个组合注解,包含内容如下:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(DelegatingWebMvcConfiguration.class)
public @interface EnableWebMvc {
}

其中的DelegatingWebMvcConfiguration.class继承了WebMvcConfigurationSupport,所以@EnableWebMVC会引入WebMvcConfigurationSupport,所以就导致了SpringBoot用于自动配置SpringMVC的WebMvcAutoConfiguration类的失效。

总结:pringBoot在自动配置很多组件的时候,都会先判断用户有没有自己配置(@Bean,@Component),如果有,就使用用户的配置,没有就自动配置。但是有些组件可以有多个(ViewResolver),那么SpringBoot就将用户配置和自己默认的配置组合起来(也就是如果某个功能是可以设置多个组件的,那么用户自定义的配置不会覆盖掉默认的配置,而是多个配置同时存在)。在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置,在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值