1、SpringBoot启动流程

2、SpringBoot原理
2.1 流程图
●
2.2 具体流程分析
- SpringBootConfiguration
- Configuration
- EnableAutoConfiguration
- 这个注解的作用是告诉SpringBoot开启自动配置功能,这样就减少了我们的配置
- AutoConfigurationPackage
- 观察其内部实现,内部是采用了@Import,来给容器导入一个Registrar组件
- 通过源码跟踪,我们知道,程序运行到这里,会去加载启动类所在包下面的所有类
- 就是为什么,默认情况下,我们要求定义的类,比如controller,service必须在启动类的同级目录或子级目录的原因
- Import(AutoConfigurationImportSelector.class)
- 发现默认加载了好多的自动配置类,这些自动配置类,会自动给我们加载每个场景所需的所有组件,并配置好这些组件,这样就省去了很多的配置
- AutoConfigurationPackage
- 这个注解的作用是告诉SpringBoot开启自动配置功能,这样就减少了我们的配置
- 深入理解SpringBoot启动机制(starter机制)
- @Configuration注解的类可以看作是能生产让Spring IoC容器管理的Bean实例的工厂。
- @Bean注解告诉Spring,一个带有@Bean的注解方法将返回一个对象,该对象应该被注册到spring容器中。
- DataSourceAutoConfiguration
- EnableConfigurationProperties
- @EnableConfigurationProperties注解的作用是使@ConfigurationProperties注解生效。如果只配置@ConfigurationProperties注解,在spring容器中是获取不到yml或者properties配置文件转化的bean的.
- ConfigurationProperties
- @ConfigurationProperties注解的作用是把yml或者properties配置文件转化为bean。
- EnableConfigurationProperties
- Bean的发现
- 实际上重要的只有三个Annotation:
- @Configuration(@SpringBootConfiguration里面还是应用了@Configuration)
- @Configuration的作用上面我们已经知道了,被注解的类将成为一个bean配置类
- _@_ComponentScan
- @ComponentScan的作用就是自动扫描并加载符合条件的组件,比如@Component和@Repository等,最终将这些bean定义加载到spring容器中
- @EnableAutoConfiguration
- @AutoConfigurationPackage
- @AutoConfigurationPackage的作用就是自动配置的包
- _@_Import
- @Import导入需要自动配置的组件。
- @AutoConfigurationPackage
- @Configuration(@SpringBootConfiguration里面还是应用了@Configuration)
- 每个xxxAutoConfiguration都是一个基于java的bean配置类。实际上,这些xxxAutoConfiguratio不是所有都会被加载,会根据xxxAutoConfiguration上的@ConditionalOnClass等条件判断是否加载;通过反射机制将spring.factories中@Configuration类实例化为对应的java实列
- 实际上重要的只有三个Annotation:
- Bean 加载
- 如果要让一个普通类交给Spring容器管理,通常有以下方法:
- 使用 @Configuration与_@_Bean 注解
- 使用_@_Controller _@_Service _@_Repository _@_Component 注解标注该类,然后启用@ComponentScan自动扫描
- 使用_@_Import 方法
- springboot中使用了_@_Import 方法
- @EnableAutoConfiguration注解中使用了@Import({AutoConfigurationImportSelector.class})注解,AutoConfigurationImportSelector实现了DeferredImportSelector接口,
- DeferredImportSelector接口继承了ImportSelector接口,ImportSelector接口只有一个selectImports方法。
- selectImports方法返回一组bean,@EnableAutoConfiguration注解借助@Import注解将这组bean注入到spring容器中,springboot正式通过这种机制来完成bean的注入的。
- 如果要让一个普通类交给Spring容器管理,通常有以下方法:
3、总结
1.@SpringbootAplication底层可拆分为三个注解@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan2.@SpringBootConfiguration是对spring注解@Configuration的包装3.@ComponentScan定义 了默认的扫包范围为当前启动类所在包下的所有类4.@EnableAutoConfiguration中主要引入了AutoConfigurationImportSelector这个类选择器5.AutoConfigurationImportSelector中将org.springframework.boot.autoconfigure这个依赖jar中的spring.factories文件中的类全部加载6.spring.factories中定义了很多配置类,其中包括DispatcherServletAutoConfiguration(springMVC核心类)和ServletWebServerFactoryAutoConfiguration(容器类)7.ServletWebServerFactoryAutoConfiguration中 通过@Import将三种web容器注入IOC:EmbeddedTomcat、EmbeddedJetty和EmbeddedUndertow8.以默认的tomcat为例,IOC:EmbeddedTomcat中通过@Bean的方式将TomcatServletWebServerFactory注入IOC9.TomcatServletWebServerFactory的getWebServer会创建tomcat服务器,并将本地class文件交由其运行管理启动运行流程1.springboot启动时,会先创建sprinApplication对象,再执行其run方法2.创建springApplication对象时会先判断当前启动时哪一类型的WebApplicationType3.通过setInitializers和setListeners会将所有jar中/resources/META-INF/下的spring.factories文件中的类注入IOC4.执行run方法时,会开启计时5.获取所有的启动listener6.将环境加载7.打印轮播图8.创建springboot的上下文对象9.装配上下文10.刷新上下文(和spring注解方式启动原理一致)11.刷新之后的后续操作(扩展)12.发布订阅的方式广播通知listener,context要启动了13.发布订阅的方式广播通知listener,context已经启动
