spingboot学习笔记
原理初探
自动配置:pom.xml
- spring-boot-dependencies:核心依赖在父工程中!
- 我们在写入或者引入一些springboot依赖的时候,不需要指定版本号,就是因为有这些版本仓库
启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 启动器:就是springboot的启动场景;
- 比如spring-boot-starter-web,他会帮我们导入web环境所有的依赖!
- springboot会将所有的功能场景,都变成一个个的启动器;
- 我们需要使用什么功能,只需要找到对应的启动器就可以了;
主程序
- @springbootApplication //标注这个类是一个springboot的应用,启动类下所有资源被加载
- 注解
@SpringBootConfiguration:springboot的配置
@Configuration:spring配置类
@Component:spring组件
@EnableAutoConfiguration:自动配置
@AutoConfigurationPackage:自动配置包
@Import({Registrar.class}):自动配置‘包注册’
@Import({AutoConfigurationImportSelector.class}):导入选择器,有选择的导入
//获取所有配置
List<String> configurations = this.getCandidateConfigurations(annotationMetadata, attributes);
-获取后选的配置
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:自动配置的核心文件