ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringBoot自动配置原理

2019-04-26 11:50:19  阅读:339  来源: 互联网

标签:SpringBoot SpringMVC 配置 自动 WebMvcConfigurer 原理 Configuration


一、简介

使用SpringBoot:

  1. 使用IDE的初始化器创建SpringBoot应用,选中我们需要的模块start;
  2. SpringBoot已经默认将这些场景配置好了,我们只需要在配置文件中指定少量配置就可以运行起来;
  3. 自己编写业务代码;

问题:为什么我们没有像之前那样在配置文件中为SpringMVC配置视图解析器、类型转换器等,程序就可以直接运行?

自动配置原理?

这个场景SpringBoot帮我们配置了什么?能不能修改?能修改哪些配置?能不能扩展?xxx

xxxAutoConfiguration:帮我们给容器中自动配置组件
xxxProperties:配置类来封装配置文件的内容

结论:SpringBoot为大多数应用程序的运行提供了对SpringMVC的默认配置


二、分析

问题:SpringBoot对SpringMVC做了哪些默认配置?

参考官方文档:https://docs.spring.io/spring-boot/docs/2.1.4.RELEASE/reference/htmlsingle/#boot-features-developing-web-applications

SpringBoot自动配置好了SpringMVC

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

结论:通过解析我们发现,SpringBoot为大多数应用程序的运行提供了对SpringMVC的默认配置,例如视图解析器、类型转换器等,所以才不需要我们再做任何配置程序即可直接运行。当然了,以上仅仅是SpringBoot对SpringMVC做的所有自动配置,而对整个web的自动配置则放在如下包中:org.springframework.boot.autoconfigure.web

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.

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.


三、扩展

问题:但是,在实际开发中,紧靠SpringBoot提供的这些默认配置是远远不够用的,如果我们想添加视图映射器、拦截器等时,应如何做呢?

  • 首先,看我们之前是如何配置的:
<mvc:view-controller path="/hello" view-name="login"/>
<mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/hello"/>
        <bean class="com.cyn.springmvc.interceptor.LoginInterceptor"/>
    </mvc:interceptor>
</mvc:interceptors>
  • 然后,根据官方文档:

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.

  • 添加视图解析器:

编写一个配置类(@Configuration),是WebMvcConfigurer类型,不能标注@EnableWebMvc

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

//使用WebMvcConfigurer可以来扩展SpringMVC的功能
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //浏览器发送/hello请求,也来到login.html界面
        registry.addViewController("/hello").setViewName("login");
    }
}
  • 原理
  1. WebMvcAutoConfiguration是SpringMVC的自动配置类
  2. 在做其他自动配置时会导入:@Import(EnableWebMvcConfiguration.class)
  3. 容器中所有的WebMvcConfigurer都会一起起作用
  4. 我们的配置类也会被调用

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


四、覆盖

问题:当我们一点也不想使用SpringBoot的默认配置,而想要使用我们自定的配置,如何实现呢?

  • 根据官方文档:

If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.

  • 做如下配置:

我们仅需要在自动配置类中添加@EnableWebMvc注解即可

//禁用SpringBoot对SpringMVC做的所有自动配置
@EnableWebMvc
//使用WebMvcConfigurer可以来扩展SpringMVC的功能
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //浏览器发送/hello请求,也来到login.html界面
        registry.addViewController("/hello").setViewName("login");
    }
}

结论:SpringBoot对SpringMVC的自动配置就不需要了,所有都是我们自己配置;所有的SpringMVC的自动配置均失效,例如:静态资源映射器、视图解析器等,当初在SSM框架的配置文件中所需做的配置应全部由我们来手动实现,否则程序就会因缺乏相对应组件的支持而无法直接运行起来。

  • 原理

问题:为什么添加了@EnableWebMVC注解,SpringBoot的自动配置就失效了?


五、总结

问题:如何修改SpringBoot的默认配置?

模式:

  1. SpringBoot在自动配置很多组件的时候,先看容器中有没有用户自己配置的(@Bean、@Component)如果有就用用户配置的,如果没有,才启用自动配置;如果有些组件可以有多个例如:ViewResolver,那么用户配置的将会和SpringBoot默认配置的组合起来使用。
  2. 在SpringBoot中会有非常多的xxxConfigurer帮助我们进行扩展配置。
  3. 在SpringBoot中会有很多的xxxCustomizer帮助我们进行定制配置。

 

标签:SpringBoot,SpringMVC,配置,自动,WebMvcConfigurer,原理,Configuration
来源: https://blog.csdn.net/qq_37230121/article/details/89537314

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有