SpringBoot原理初探
pom.xml
- spring-boot-dependencies:核心依赖在父工程中!
- 我们引入一些SpringBoot依赖的时候,不需要指定版本,就因为有这些版本仓库
启动器:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
注解:
获得候选的配置
protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(this.getSpringFactoriesLoaderFactoryClass(), this.getBeanClassLoader());
Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you are using a custom packaging, make sure that file is correct.");
return configurations;
}
META-INF/spring.factories:自动配置的核心文件
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
//将所有资源加载到配置类中!
自动配置分析思维导图地址:https://www.processon.com/view/link/6120a79b0e3e743b3277c663
结论:
- 1、springboot在启动的时候,从类路径下/META-INF/spring.factories获取指定的值
- 2、将这些自动配置的类导入容器,自动配置就会生效,帮我们自动配置
- 3、整合javaEE,解决方案和自动配置的东西都在spring-boot-autoconfigure-xxx包下
- 4、他会把所有需要导入的组件,一类名的方式返回,这些组件就会被添加到容器
- 5、容器中会存在很多xxxAutoConfiguration得文件(@Bean),就是这些类给容器中导入了这个场景需要的所有组件,并自动配置
SpringApplication
这个类主要做了以下四件事情:
1、推断应用的类型是普通的项目还是Web项目
2、查找并加载所有可用初始化器 , 设置到initializers属性中
3、找出所有的应用程序监听器,设置到listeners属性中
4、推断并设置main方法的定义类,找到运行的主类
学习博客:https://www.cnblogs.com/hellokuangshen/p/12450327.html
面试题:
关于SpringBoot,谈谈你的理解:
1、自动配置的过程
2、run方法