0%

springboot 自动装配(一):加载自动装配类

pom.xml

  • spring-boot-dependencies 核心依赖在父工程中

  • 我们在写或者引入一些SpringBoot依赖的还是,不需要指定版本,就是因为这些版本仓库

启动器

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
  • 启动器:说白了就是SpringBoot的启动场景

  • SpringBoot回家所有的功能场景都变成启动器

  • 我们要使用什么功能,只需要找到对应的启动器就可以了 starter

主程序

1
2
3
4
5
6
7
8
9
10
// @SpringBootApplication 标注这是一个 SpringBoot 应用
@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}

注解

1
2
3
4
5
6
7
8
9
10
11
@SpringBootApplication 
@SpringBootConfiguration :SpringBoot的配置
@Configuration :Spring 配置类
@Component :说明这也是一个 spring 组件
@EnableAutoConfiguration :自动配置
@AutoConfigurationPackage :自动配置包
@Import(AutoConfigurationPackages.Registrar.class) :自动配置‘包注册’
@Import(AutoConfigurationImportSelector.class) :自动配置导入选择

// 获取所有的配置
List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);

获取候选的配置

1
2
3
4
5
6
7
	protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
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;
}

1613726771124

META-INF/spring.factories

自动配置的核心文件 META-INF/spring.factories

1613727186380

loadFactoryNames

1613727327723

参考:

https://my.oschina.net/funcy/blog/4870868


----------- 本文结束啦感谢您阅读 -----------