SpringBoot的作用
1. 自动依赖管理
在Springboot-dependency中定义各种jar包的版本,进行导入,省去自己去导入的过程
2. 自动配置原理
-
springboot.autoconfigure包保存了大量的自动配置类,对应每个常用的框架,使用Java代码对框架进行配置
-
每个自动配置类生效的条件是:导入了对应的依赖 @ConditionOnClass({类.class})
-
在META-INF/spring.factores中把所有自动配置类的全名定义出来
-
在SpringBoot类上有@SpringBootApplication注解
-
该注解由三个注解组成:
- SpringbootConfiguration 代表该类作为配置类使用
- ComponentScan 对包进行扫描
- EnableAutoConfiguration 启动自动配置
-
在EnableAutoConfiguration注解的XXSelector源码中,会读取spring.factores文件,通过反射将所有的自动配置类加载到内存中,启动了自动配置
@SpringBootApplication
@EnableAutoConfiguration
AutoConfigurationImportSelector
selectImports
getAutoConfigurationEntry
getCandidateConfigurations
loadFactoryNames
loadSpringFactories
classLoader.getResources(“META-INF/spring.factories”);