1、SpringBoot启动流程

    SpringBoot自动配置原理 - 图1

    2、SpringBoot原理

    2.1 流程图

    SpringBoot自动配置原理 - 图2

    2.2 具体流程分析

    • SpringBootConfiguration
    • Configuration
    • EnableAutoConfiguration
      • 这个注解的作用是告诉SpringBoot开启自动配置功能,这样就减少了我们的配置
        • AutoConfigurationPackage
          • 观察其内部实现,内部是采用了@Import,来给容器导入一个Registrar组件
          • 通过源码跟踪,我们知道,程序运行到这里,会去加载启动类所在包下面的所有类
          • 就是为什么,默认情况下,我们要求定义的类,比如controller,service必须在启动类的同级目录或子级目录的原因
        • Import(AutoConfigurationImportSelector.class)
          • 发现默认加载了好多的自动配置类,这些自动配置类,会自动给我们加载每个场景所需的所有组件,并配置好这些组件,这样就省去了很多的配置
    • 深入理解SpringBoot启动机制(starter机制)
      • @Configuration注解的类可以看作是能生产让Spring IoC容器管理的Bean实例的工厂。
      • @Bean注解告诉Spring,一个带有@Bean的注解方法将返回一个对象,该对象应该被注册到spring容器中。
    • DataSourceAutoConfiguration
      • EnableConfigurationProperties
        • @EnableConfigurationProperties注解的作用是使@ConfigurationProperties注解生效。如果只配置@ConfigurationProperties注解,在spring容器中是获取不到yml或者properties配置文件转化的bean的.
      • ConfigurationProperties
        • @ConfigurationProperties注解的作用是把yml或者properties配置文件转化为bean。
    • Bean的发现
      • 实际上重要的只有三个Annotation:
        • @Configuration(@SpringBootConfiguration里面还是应用了@Configuration)
          • @Configuration的作用上面我们已经知道了,被注解的类将成为一个bean配置类
        • _@_ComponentScan
          • @ComponentScan的作用就是自动扫描并加载符合条件的组件,比如@Component和@Repository等,最终将这些bean定义加载到spring容器中
        • @EnableAutoConfiguration
          • @AutoConfigurationPackage
            • @AutoConfigurationPackage的作用就是自动配置的包
          • _@_Import
            • @Import导入需要自动配置的组件。
      • 每个xxxAutoConfiguration都是一个基于java的bean配置类。实际上,这些xxxAutoConfiguratio不是所有都会被加载,会根据xxxAutoConfiguration上的@ConditionalOnClass等条件判断是否加载;通过反射机制将spring.factories中@Configuration类实例化为对应的java实列
    • Bean 加载
      • 如果要让一个普通类交给Spring容器管理,通常有以下方法:
      • springboot中使用了_@_Import 方法
      • @EnableAutoConfiguration注解中使用了@Import({AutoConfigurationImportSelector.class})注解,AutoConfigurationImportSelector实现了DeferredImportSelector接口,
      • DeferredImportSelector接口继承了ImportSelector接口,ImportSelector接口只有一个selectImports方法。
      • selectImports方法返回一组bean,@EnableAutoConfiguration注解借助@Import注解将这组bean注入到spring容器中,springboot正式通过这种机制来完成bean的注入的。

    3、总结

    1. 1.@SpringbootAplication底层可拆分为三个注解@SpringBootConfiguration@EnableAutoConfiguration@ComponentScan
    2. 2.@SpringBootConfiguration是对spring注解@Configuration的包装
    3. 3.@ComponentScan定义 了默认的扫包范围为当前启动类所在包下的所有类
    4. 4.@EnableAutoConfiguration中主要引入了AutoConfigurationImportSelector这个类选择器
    5. 5.AutoConfigurationImportSelector中将org.springframework.boot.autoconfigure这个依赖jar中的spring.factories文件中的类全部加载
    6. 6.spring.factories中定义了很多配置类,其中包括DispatcherServletAutoConfigurationspringMVC核心类)和ServletWebServerFactoryAutoConfiguration(容器类)
    7. 7.ServletWebServerFactoryAutoConfiguration 通过@Import将三种web容器注入IOCEmbeddedTomcatEmbeddedJettyEmbeddedUndertow
    8. 8.以默认的tomcat为例,IOCEmbeddedTomcat中通过@Bean的方式将TomcatServletWebServerFactory注入IOC
    9. 9.TomcatServletWebServerFactorygetWebServer会创建tomcat服务器,并将本地class文件交由其运行管理
    10. 启动运行流程
    11. 1.springboot启动时,会先创建sprinApplication对象,再执行其run方法
    12. 2.创建springApplication对象时会先判断当前启动时哪一类型的WebApplicationType
    13. 3.通过setInitializerssetListeners会将所有jar中/resources/META-INF/下的spring.factories文件中的类注入IOC
    14. 4.执行run方法时,会开启计时
    15. 5.获取所有的启动listener
    16. 6.将环境加载
    17. 7.打印轮播图
    18. 8.创建springboot的上下文对象
    19. 9.装配上下文
    20. 10.刷新上下文(和spring注解方式启动原理一致)
    21. 11.刷新之后的后续操作(扩展)
    22. 12.发布订阅的方式广播通知listenercontext要启动了
    23. 13.发布订阅的方式广播通知listenercontext已经启动