SpringBoot的自动配置(如何添加自己的视图解析器(扩展MVC(全面接管SpringMVC

本篇文章为观看尚硅谷视频的理解与笔记内有老师讲解的知识点以及自己理解的知识点,用于记录自己的学习过程

本篇主要围绕视图解析器展开

 

首先我们拿出官方文档:

https://docs.spring.io/spring-boot/docs/1.5.10.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications

Spring Boot 自动配置好了SpringMVC

以下是SpringBoot对SpringMVC的默认配置:类:WebMvcAutoConfiguration 

 

如下图:

SpringBoot为我们配置视图解析器,视图解析器用来用来转发或者重定向请求

在SpringBoot中ContentNegotiatingViewResolver类中可以组合所有的视图解析器,使他们全部生效

实例:添加自己的视图解析器

如果我们想让我们的视图解析器生效,首先要让SpringBoot知道我们这个视图解析器的存在(在方法上加上@Bean注解

其次我们自己的视图解析器要实现VieResolver接口,然后由myViewReosover新建注册到容器中,这样就可以使用了

检测我们的视图解析器是否起了作用:

我们打上断点,检测一下

如图所示,我们添加的视图解析器已经被加到了里面

SpringMVC的其他默认配置

Spring MVC auto-configuration

Spring Boot 自动配置好了SpringMVC

以下是SpringBoot对SpringMVC的默认配置:(WebMvcAutoConfiguration)

Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
自动配置了ViewResolver(视图解析器:根据方法的返回值得到视图对象(View),视图对象决定如何渲染(转发或者重定向))
ContentNegotiatingViewResolver:组合所有的视图解析器的
如何定制:我们可以自己给容器中添加一个视图解析器;自动的将其组合进来
Support for serving static resources, including support for WebJars (see below).静态资源文件夹路径,webjars
Static index.html support. 静态首页访问
Custom Favicon support (see below).  favicon.ico
自动注册了 of Converter, GenericConverter, Formatter beans.
Converter:转换器;  public String hello(User user):类型转换使用Converter,前台发送数据后台接收时候String转为Integer等
Formatter  格式化器;  2017.12.17===Date;
@Bean
@ConditionalOnProperty(prefix = "spring.mvc", name = "date-format")//在文件中配置日期格式化的规则
public Formatter<Date> dateFormatter() {
        return new DateFormatter(this.mvcProperties.getDateFormat());//日期格式化组件
}
//自己添加的格式化器转换器,我们只需要放在容器中即可
Support for HttpMessageConverters (see below)
​
HttpMessageConverter:SpringMVC用来转换Http请求和响应的;
返回User数据时候使用json数据返回回去
HttpMessageConverters 是从容器中确定;(因为他只有一个有参函数的值)获取所有的HttpMessageConverter
如何自己定义:
自己给容器中添加HttpMessageConverter,只需要将自己的组件注册容器中(用@Bean或者@Component扫描进容器)

自己配置HttpMessageConverter的官方文档:

Automatic registration of MessageCodesResolver (see below)  定义错误代码生成规则

Automatic use of a ConfigurableWebBindingInitializer bean (see below)
我们可以配置一个ConfigurableWebBindingInitializer来替换默认的;(将其添加到容器)
用来初始化WebDataBinder(数据绑定器);
请求数据=====JavaBean;

**org.springframework.boot.autoconfigure.web:配置了web的所有自动场景;**

如何修改SpringBoot的默认配置

配置方法(结论):
    1)、SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)

如果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(ViewResolver)将用户配置的和自己默认的组合起来;

    2)、在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置

    3)、在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置
 

SpringBoot扩展MVC

使用SpringBoot实现以下功能

    <mvc:view-controller path="/hello" view-name="success"/>
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/hello"/>
            <bean></bean>
        </mvc:interceptor>
    </mvc:interceptors>

1.编写一个配置类(加上@Configuration让其变为配置类),是WebMvcConfigurerAdapter类型;不能标注@EnableWebMvc;

既保留了所有的自动配置,也能用我们扩展的配置;

///使用WebMvcConfigurer可以扩展SpringMVC的功能
@Configuration//标注这是一个配置类
public class MyMvcConfig implements WebMvcConfigurer {
    //ctrl+o打开能重写的列表
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //拦截请求“xiaolang”并转发到"s"
        registry.addRedirectViewController("MyHello","s");
    }
}
    @RequestMapping("/MyHello")
    public String sayingCiaoLang() {
        System.out.println("运行了");
        return "扩展MVC";
    }

遇到的错误

//错误1.
//无返回值,返回不到,也映射不到任何界面
  @RequestMapping("/MyHello")
    public void sayingCiaoLang() {
    }
​
//错误2.
//方法中不能使用“xiao”,否则出现不会显示报错的错误
    @RequestMapping("/MyHello")
    public String xiaolang() {
        System.out.println("运行了");
        return "扩展MVC";
    }

原理:

​ 1)、WebMvcAutoConfiguration是SpringMVC的自动配置类

2)、在做其他自动配置时会导入;({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})

 @Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
    @EnableConfigurationProperties({WebMvcProperties.class, ResourceProperties.class})
    @Order(0)
    public static class WebMvcAutoConfigurationAdapter implements WebMvcConfigurer {

EnableWebMvcConfiguration.class:

@Configuration(
        proxyBeanMethods = false
    )
    public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration implements ResourceLoaderAware {
        private final ResourceProperties resourceProperties;

=·=源码看不懂了,和老师的不一样,老师的类都失效了

​ 3)、容器中所有的WebMvcConfigurer都会一起起作用;

​ 4)、我们的配置类也会被调用;

​ 效果:SpringMVC的自动配置和我们的扩展配置都会起作用;

 

全面接管SpringMVC

全面接管代表SpringBoot对SpringMVC的自动配置不需要了,所有都是我们自己配置;所有的SpringMVC的自动配置都失效了

我们需要在配置类中添加@EnableWebMvc即可;

//使用WebMvcConfigurer可以扩展SpringMVC的功能
@EnableWebMvc//全面接管SpringMVC
@Configuration//标注这是一个配置类
public class MyMvcConfig implements WebMvcConfigurer {
    //ctrl+o打开能重写的列表
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //拦截请求“xiaolang”并转发到"s"
        registry.addRedirectViewController("MyHello","s");
    }
}

原理:

@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}

导入的类:

public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
    private final WebMvcConfigurerComposite configurers = new WebMvcConfigurerComposite();
​

父类:

都是如此的springMVC配置文件
    protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
        return new RequestMappingHandlerMapping();
    }

原理就是:

SpringMVC自动配置类生效条件是没有WebMvcConfigurationSupport.class这个类

 

@EnableWebMvc将WebMvcConfigurationSupport组件导入进来;这时候我们的这个MVC自动配置(WebMvcAutoConfiguration)就会失效了

@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
//容器中没有这个组件的时候,这个自动配置类才能生效
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class})
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})
public class WebMvcAutoConfiguration {

 

Spring boot导入的WebMvcConfigurationSupport只是SpringMVC最基本的功能

 

配置我们的视图解析器(开发常用

//使用WebMvcConfigurer可以扩展SpringMVC的功能
//@EnableWebMvc//全面接管SpringMVC
@Configuration//标注这是一个配置类
public class MyMvcConfig implements WebMvcConfigurer {
    //ctrl+o打开能重写的列表
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //拦截请求“xiaolang”并转发到"s"
        registry.addRedirectViewController("MyHello","s");
    }

 

    //所有WebMVCConfigureAdapter组件会一起起作用
    //前提是SpringBoot知道有这个组件
    //所以加上@Bean注解注册上
    @Bean
    public WebMvcConfigurer webMvcConfigurer() {
        WebMvcConfigurer webMvcConfigurer = new WebMvcConfigurer() {
            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("a").setViewName("index1");
                registry.addViewController("b").setViewName("index1");
            }
        };
        return webMvcConfigurer;
    }

}
 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值