springboot 自动配置好了springmvc
自动配置了viewresolver(视图解析器:根据方法的返回值得到视图对象(view),视图对象决定如何渲染(转发?重定向?))
contentnegotiatingviewresolver:组合所有的视图解析器的;
如何定制:我们可以自己给容器中添加一个视图解析器;自动的将其组合进来;
support for serving static resources, including support for webjars (see below).静态资源文件夹路径,webjars
static support. 静态首页访问
custom favicon
support (see below). favicon.ico
自动注册了 of converter
, genericconverter
, formatter
beans.
converter:转换器; public string hello(user user):类型转换使用converter
formatter
格式化器; 2019.07.08 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)==
automatic registration of messagecodesresolver
(see below).定义错误代码生成规则
automatic use of a configurablewebbindinginitializer
bean (see below).
==我们可以配置一个configurablewebbindinginitializer来替换默认的;(添加到容器)==
初始化webdatabinder; 请求数据=====javabean;
org.springframework.boot.autoconfigure.web:web的所有自动场景;
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
.
<mvc:view-controller path="/hello" view-name="success"/> <mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/hello"/> <bean></bean> </mvc:interceptor> </mvc:interceptors>
编写一个配置类(@configuration),是webmvcconfigureradapter类型;不能标注@enablewebmvc
既保留了所有的自动配置,也能用我们扩展的配置;
//使用webmvcconfigureradapter可以来扩展springmvc的功能 @configuration public class mymvcconfig extends webmvcconfigureradapter { @override public void addviewcontrollers(viewcontrollerregistry registry) { // super.addviewcontrollers(registry); //浏览器发送 /atguigu 请求来到 success registry.addviewcontroller("/atguigu").setviewname("success"); } }
原理: 1)、webmvcautoconfiguration是springmvc的自动配置类 2)、在做其他自动配置时会导入;@import(enablewebmvcconfiguration.class) @configuration public static class enablewebmvcconfiguration extends delegatingwebmvcconfiguration { private final webmvcconfigurercomposite configurers = new webmvcconfigurercomposite(); //从容器中获取所有的webmvcconfigurer @autowired(required = false) public void setconfigurers(list<webmvcconfigurer> configurers) { if (!collectionutils.isempty(configurers)) { this.configurers.addwebmvcconfigurers(configurers); //一个参考实现;将所有的webmvcconfigurer相关配置都来一起调用; @override // public void addviewcontrollers(viewcontrollerregistry registry) { // for (webmvcconfigurer delegate : this.delegates) { // delegate.addviewcontrollers(registry); // } } } }
3)、容器中所有的webmvcconfigurer都会一起起作用;
4)、我们的配置类也会被调用;
效果:springmvc的自动配置和我们的扩展配置都会起作用;
springboot对springmvc的自动配置不需要了,所有都是我们自己配置;所有的springmvc的自动配置都失效了
我们需要在配置类中添加@enablewebmvc即可;
//使用webmvcconfigureradapter可以来扩展springmvc的功能 @enablewebmvc @configuration public class mymvcconfig extends webmvcconfigureradapter { @override public void addviewcontrollers(viewcontrollerregistry registry) { // super.addviewcontrollers(registry); //浏览器发送 /atguigu 请求来到 success registry.addviewcontroller("/atguigu").setviewname("success"); } }
原理:
为什么@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最基本的功能;
模式:
1)、springboot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@bean、@component)如
果有就用用户配置的,如果没有,才自动配置;如果有些组件可以有多个(viewresolver)将用户配置的和自己默
认的组合起来;
2)、在springboot中会有非常多的xxxconfigurer帮助我们进行扩展配置
3)、在springboot中会有很多的xxxcustomizer帮助我们进行定制配置
如对本文有疑问, 点击进行留言回复!!
现在微服务这么火,你还不了解吗?阿里P8推荐的微服务学习指南
论文笔记:SlowFast Networks for Video Recognition
网友评论