MVC配置原理

MVC配置原理

我们参考官方文档:https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/html/spring-boot-features.html#boot-features-developing-web-applications

可以看到SpringMVC的自动配置支持许多的功能比如:视图解析器、静态资源、支持webjars、支持类型转换器、格式化器、http消息转换、定义错误消息、首页映射、图标映射、数据初始化绑定;

如果我们想保持这些东西并且添加一些其他的扩展MVC配置,比如:拦截器、格式化器、试图控制和其他等等,添加注解@Configuration,实现WebMvcConfigurer这个接口。

package com.star.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//扩展Spring MVC
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    
}

可以重写的方法:

可以重写方法实现对应的功能!

我们用第一个对应的类来测试

找到这个类

可以看到这个类实现了viewResolver视图解析器这个接口,我们就可以将这个类当做试图解析器

视图解析器中只有一个方法resolveViewName

这个类重写了该方法:

可以看到调用候选试图的方法,我们点进去:

该方法遍历获取所有的视图解析器,封装成对象然后添加到候选视图,返回出去!

接下来我们自己写一个视图解析器,写在MyMvcConfig类里面

    //注册bean
    @Bean
    public ViewResolver MyMvcViewResolver(){
        return new MyMvcViewResolver();
    }

    //自定以的视图解析器
    public static class MyMvcViewResolver implements ViewResolver{
        @Override
        public View resolveViewName(String viewName, Locale locale) throws Exception {
            return null;
        }
    }

我们知道所有的请求都会经过DispatchServlet这个类的doDispatch方法:

我们打一个断点来DeBug一下

启动项目,访问localhost:8080

IDEA进行跳转:

我们点开this,寻找viewResolver

可以看到有系统自带的,bean注册的,thymeleaf的以及我们自定义的!

结论:如果我们想自定义一些定制化的功能,只要写这个组件,然后将它交给springboot(在springMVC中注册bean),springboot就会帮我们自动装配!

修改SpringBoot的默认配置

方式一

这么多的自动配置,原理都是一样的,通过这个WebMVC的自动配置原理分析,我们要学会一种学习方式,通过源码探究,得出结论:这个结论一定是属于自己的,而且一通百通。

SpringBoot的底层,大量用到了这个ie设计思想,所有需要阅读源码得出结论。

SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己设置的,如果有就用用户配hi的,如果没有就用自动装配的;如果有些组件可以存在多个,比如我们的视图解析器,就将用户配置的和自己默认的组合起来!

扩展使用SpringMVC

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.

上面所讲到的,比如我们重写一个视图跳转的方法

package com.star.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


//扩展Spring MVC
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    //路径/star就会跳转到test.html页面
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/star").setViewName("test");
    }
}

在resources下的templates下编写一个test.html

package com.star.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


//扩展Spring MVC
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("star").setViewName("test");
    }
}

测试结果:

官方文档中说不能加@EnableWebMvc这个注解,为什么呢?

我们先加上这个注解,查看它的源码:

这个注解导入了DelegatingWebMvcConfiguration这个类,查看源码:

作用:从容器中获取所有的WebMvcConfig

现在我们进入WebMvcAutoConfig这个类

可以看到@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)这个注解

表示没有WebMvcConfigurationSupport这个类这些自动配置才会生效,但是刚才我们看到DelegatingWebMvcConfiguration这个类继承了WebMvcConfigurationSupport这个类;

所以说当我们添加@EnableWebMvc这个注解,就相当于导入了WebMvcConfigurationSupport这个类,但是这个类就会到导致WebMvc所有的自动配置失效。

所以切记:不能加@EnableWebMvc这个注解!

在SpringBoot中,有非常多的xxxConifguration帮助我们行进扩展配置,只要看见了这个东西,我们就要注意了!

posted @ 2020-03-12 16:53  陌星灬  阅读(253)  评论(0编辑  收藏  举报