当前位置: 移动技术网 > IT编程>开发语言>Java > SpringBoot中SpringMVC的自动配置以及扩展

SpringBoot中SpringMVC的自动配置以及扩展

2020年01月09日  | 移动技术网IT编程  | 我要评论

一、问题引入

我们在ssm中使用springmvc的时候,需要由我们自己写springmvc的配置文件,需要用到什么就要自己配什么,配置起来也特别的麻烦。我们使用springboot的时候没有进行配置,直接就能进行使用,这是为什么呢?

这是因为springboot为我们自动配置好了springmvc

1)、我们首先参照官网来看一下关于springmvc的自动配置

​ https://docs.spring.io/spring-boot/docs/2.2.1.release/reference/htmlsingle/#boot-features-developing-web-applications

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:组合所有的视图解析器的;
    • ==如何定制:我们可以自己给容器中添加一个视图解析器;自动的将其组合进来
  • support for serving static resources, including support for webjars (covered later in this document)).

静态资源文件夹路径.webjars

  • automatic registration of converter, genericconverter, and formatter beans.

自动注册了converter, genericconverter, and formatter等

    • **converter:转换器,**比如也面提交的是一个数字,但是页面的数字是文本类型的,通过转换器,就能转换成integer类型
    • formatter :格式化器,2020-1-1===date;
  • support for httpmessageconverters (covered later in this document).

    • httpmessageconverter:springmvc用来转换http请求和响应的;
    • httpmessageconverter是从容器中确定;获取所有的httpmessageconverter;

自己给容器中添加httpmessageconverter,只需要将自己的组件注册容器中(@bean,@component)

定义错误代码生成规则

我们可以配置一个configurablewebbindinginitializer来替换默认的;(添加到容器)

初始化webdatabinder;

请求数据=====javabean;

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.

2、扩展springmvc

编写一个配置类(@configuration),是webmvcconfigureradapter类型;不能标注@enablewebmvc;

在这里插入图片描述

if you want to take complete control of spring mvc, you can add your own @configuration annotated with @enablewebmvc

3.全面接管springmvc

为什么@enablewebmvc自动配置就失效了?

1)@enablewebmvc的核心

@import(delegatingwebmvcconfiguration.class)

public @interface enablewebmvc {

2)、

@configuration

public class delegatingwebmvcconfiguration extends webmvcconfigurationsupport {

3)、

@configuration

@conditionalonwebapplication

@conditionalonclass({ servlet.class, dispatcherservlet.class,

webmvcconfigureradapter.class })

//容器中没有这个组件的时候,这个自动配置类才生效

@conditionalonmissingbean(webmvcconfigurationsupport.class)

@autoconfigureorder(ordered.highest_precedence + 10)

@autoconfigureafter({ dispatcherservletautoconfiguration.class,

validationautoconfiguration.class })

public class webmvcautoconfiguration {

4)、@enablewebmvc将webmvcconfigurationsupport组件导入进来;

5)、导入的webmvcconfigurationsupport只是springmvc最基本的功能;

4.原理

关于contentnegotiatingviewresolver

在这里插入图片描述
我们点进这个contentnegotiatingviewresolver ,在下面有一个resolveviewname方法
在这里插入图片描述

那么是怎么样获取候选的视图对象的呢?

我们点进getcandidateviews这个方法
在这里插入图片描述

那么这个组合逻辑又是什么呢?

返回到我们这里

在这里插入图片描述
看到首先是new了一个contentnegotiatingviewresolver对象

我们点进去,
在这里插入图片描述

我们所需要的viewresolvers是从哪里拿到的呢?

我们接着往下边看

在这里插入图片描述

我们看到,这里有个初始化方法,里边利用beanfactoryutils工具,从容器中获取所有的视图解析器,把这些视图解析器就作为要组合的所有视图解析器。

那么如何定制我们自己的视图解析器呢,通过上面的讲解,就是:我们只需要自己给容器中添加一个视图解析器;然后contentnegotiatingviewresolver就会将其

组合进来。

我们下面举个简单的例子给大家演示一下

为了方便,我们就在在springboot的启动类中创建一个viewresolver并将其添加到容器中
在这里插入图片描述

那么我们怎么判断是否其作用了呢,我们知道,视图解析器都会被dispatcherservlet所扫描到,所有的请求都会被dispatcherservlet类中的dodispatch方法所拦截

在dodispatch方法上打一个断点,debug运行,随便访问一个url,就能看到结果如下。
在这里插入图片描述

在这里插入图片描述

结果我们发现,我们自己加入的viewresovler确实生效了

二.总结

1.假如我们需要扩展springmvc,只需要编写一个配置类(@configuration),是webmvcconfigureradapter类型;不标注@enablewebmvc即可,这个时候我们既保留了所有的自动配置,也能用我们扩展的配置;

2.假如是需要全面接管,即所有的配置都需要我们自己来定义,我们就加上@enablewebmvc注解即可。

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网