码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot自动配置原理

时间:2019-04-26 12:45:04      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:转换器   one   pca   尺寸   覆盖   log   cte   有用   ali   

 

一、简介

使用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技术图片?

技术图片

  1. 容器中所有的WebMvcConfigurer都会一起起作用
  2. 我们的配置类也会被调用

效果: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自动配置原理

标签:转换器   one   pca   尺寸   覆盖   log   cte   有用   ali   

原文地址:https://www.cnblogs.com/cuiyanan/p/10773552.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!