SpringBoot运行原理
SpringBoot是一个基于Spring开发,集成了大量第三方库配置的javaweb开发框架
pom.xml
父依赖
其中它主要是依赖一个父项目,主要是管理项目的资源过滤及插件。以后我们导入依赖默认是不需要写版本的。

启动器spring-boot-starter

springboot-boot-starter-xxx:spring-boot的场景启动器
spring-boot-starter-web:帮我们导入了web模块正常运行所依赖的组件。
springBoot将所有的功能场景都抽取出来,做成一个个的starter(启动器),只需要在项目中引入这些starter即可,所有相关的依赖都会被引进来,我们要用什么功能就导入什么样的场景启动器即可。

@SpringBootApplication
作用:标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot运行这个类的main方法来启动SpringBoot应用。
进入这个注解,里面包含了很多其他注解

@ComponentScan作用:自动扫描并加载符合条件的组件或者bean,将这个bean定义加载到IOC容器中。
@SpringBootConfiguration作用:SpringBoot的配置类,标注在某个类上,表示这是一个SpringBoot的配置类。

进入@SpringBootConfiguration注解查看,这里的@Configuration说明这是一个配置类,配置类对应Spring的xml配置文件。
继续查看@SpringBootConfiguration包含的其他注解
@EnableAutoConfiguration:开启自动配置功能
进入@EnableAutoConfiguration注解查看

@AutoConfigurationPackage自动配置包
@import:Spring底层注解@import,给容器中导入一个组件
@Import({AutoConfigurationImportSelector.class})给容器导入组件
AutoConfigurationImportSelector:自动配置导入选择器。那么它导入哪些组件的选择器呢

这个类中有这样一个方法:getCandidateConfiguration,而在这个方法中有调用了SpringFactoriesLoader类的静态方法loadFactoryNames()方法

进入loadSpringFactories方法

根据全局搜索Spring.factories,打开后是自动配置的文件。

随便打开一个其中的自动配置类看,它们都是javaConfig配置类,都注入了一些Bean

所以,自动配置真正实现是从classpath中搜寻所有的META-INF/spring.factories配置文件,并将其中对应的org.springframework.boot.autoconfigure包下的配置项通过反射实例化为对应标注了@Configuration的javaConfig形式的IOC容器配置类,然后将这些都汇总成为一个实例并加载到IOC容器中。
结论:
1.SpringBoot在启动的时候从类路径下的META-INF/spring.factories中获取EnableAutoConfiguration指定的值
2.将这些值作为自动配置类导入容器,自动配置类就生效,帮我们进行自动配置工作。
3.整个J2EE的整体解决方案和自动配置都在springboot-autoConfigure的jar包中。
4.它会给容器中导入非常多的自动配置类(xxxAutoConfiguration),就是给容器中导入这个场景需要的所有组件,并配置好这些组件。
5.有了自动配置类,免去了我们手动编写配置注入功能组件等的工作。
SpringApplication


这个类主要做了以下四件事
1.推断应用的类型是普通的项目还是web项目
2.查找并加载所有可用初始化器,设置到initializers属性中
3.找出所有的应用程序监听器,设置到listeners属性中
4.推断并设置main方法的定义类,找到运行的主类