14.springboot对springmvc的自动配置

Spring MVC auto-configuration

springboot自动配置好了springmvc
以下是springboot对springmvc的默认:

    1.Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
        。自动配置了ViewResolver(视图解析器:根据方法的返回值得到视图对象(view)),视图对象决定如何渲染(转发/重定向)
        。ContentNegotiatingViewResolver:组合所有的视图解析器的
        。如何定制自己的视图解析器:我们可以自己给容器中添加一个视图解析器;ContentNegotiatingViewResolver会自动将其组合进来
    2.Support for serving static resources, including support for WebJars (see below).:静态资源文件夹,webjars
    3.Automatic registration of Converter, GenericConverter, Formatter beans.
        。Converter:转换器;页面参数和后台的实体类进行格式换转化:例如页面参数的18是text要转换为后台的int
        。Formatter 格式花器:例如时间:2020-09-20=====Date
        源码如下:
            @Bean
            @Override//需要在application.properties配置文件中配置格式化的规格:spring.mvc.format.date=yy-MM-dd
            public FormattingConversionService mvcConversionService() {
               Format format = this.mvcProperties.getFormat();
               WebConversionService conversionService = new WebConversionService(new DateTimeFormatters()
                     .dateFormat(format.getDate()).timeFormat(format.getTime()).dateTimeFormat(format.getDateTime()));
               addFormatters(conversionService);
               return conversionService;
            }
        自己添加的格式花转换器,我们只需要放到容器中即可
        使用参考上述手册:https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications
        
    4.Support for HttpMessageConverters (see below).
        。HttpMessageConverters :springmvc用来转换http请求和响应的:例如user对象转json字符串
        。HttpMessageConverters 是从容器中获取;获取所有的HttpMessageConverters ;
        自己给容器中太你家HttpMessageConverters ,只需要将自己的组件注册到容器中(@Bean,@Component)
        使用参考上述手册:https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications
        
   5. Automatic registration of MessageCodesResolver (see below).定义错误代码生成规则
   6. Static index.html support.:静态首页
    Custom Favicon support (see below).
    7.Automatic use of a ConfigurableWebBindingInitializer bean (see below).
        初始化webDataBinder:web数据绑定器
        请求数据和javaBean的绑定
         
    If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) 
    you can add your own @Configuration class of type WebMvcConfigurerAdapter, but without @EnableWebMvc.
     If you wish to provide custom instances of RequestMappingHandlerMapping,
     RequestMappingHandlerAdapter or ExceptionHandlerExceptionResolver you can declare a WebMvcRegistrationsAdapter instance providing such components.
    If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.
    
5.如何修改springboot的默认配置呢:
首先了解springboot的默认配置模式:
1).springboot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean,@Component)如果有就用用户自己配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户的配置和自己默认的组合起来!
2).如何扩展springmvc
如spring配置的,springboot如何实现呢
如果发送的请求不想通过controller,只想直接地跳转到目标页面
<mvc:view-controller path="/hello" view-name="success"/>
拦截器
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/hello"/>
        <bean></bean>
    </mvc:interceptor>
</mvc:interceptors>

springboot可以这么做:
    1.使用@Configuration注解告诉springboot这是一个配置类
    2.类上不能标注@EnableWebMvc标签()
    2.继承于WebMvcConfigurerAdapter
    3.重写WebMvcConfigurerAdapter里的方法
    示例如下:
        //使用WebMvcConfigurerAdapter来扩展springmvc功能
        @Configuration
        public class Myconfig extends WebMvcConfigurerAdapter {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                //浏览器发送hello1请求,直接来到success页面:不用通过控制层@requ
                registry.addViewController("/hello1").setViewName("success");
            }
        }
    结论:这样既保留了springboot所有对spring的自动配置,也能用我们的扩展 
3.全面接管springmvc
springboot对springmvc的自动配置不需要了,所有的都是我们自己配置;所有的springmvc都失效了
如何做呢:我们只需要在配置类上加上:@EnableWebMvc如下(尽量不要用)

@EnableWebMvc(尽量不要加)
@Configuration
public class Myconfig extends WebMvcConfigurerAdapter {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //浏览器发送hello请求,直接来到success页面:不用通过控制层@requ
        registry.addViewController("/hello1").setViewName("success");
    }
}

 

posted @ 2022-05-10 21:47  努力的达子  阅读(364)  评论(0编辑  收藏  举报