SpringBoot自动配置原理 - Go语言中文社区

SpringBoot自动配置原理


一、简介

使用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帮助我们进行定制配置。

 

 

 

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_37230121/article/details/89537314
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-03-08 10:51:02
  • 阅读 ( 784 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢