SpringBoot自动装配原理及启动分析
1.pom依赖分析
在我们自己创建的 SpringBoot 项目的 pom.xml 文件中,通常使用parent
节点引入父项目。
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.5.3</version>
</parent>
使用 ctrl+鼠标左键 点击进入spring-boot-starter-parent
,我们发现在此文件中依旧存在parent
节点。
我们再次进入spring-boot-starter-parent
,此处,才是真正管理 SpringBoot 应用里面所有依赖版本的地方。
在该文件中,properties
节点下的各个子节点定义了待引入依赖的版本信息;dependencyManagement
集中进行了依赖管理即依赖的声明。
因此,通常在我们自己创建的 SpringBoot 项目中,引入依赖时无需进行版本指定。但是,如果我们引入的依赖在该文件中无声明则需要引入完整的依赖坐标。
2.spring-boot-starter启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>2.5.3</version>
</dependency>
springboot-boot-starter-xxx:就是 spring-boot 的场景启动器。
<!-- 引入 spring-boot-starter-web 以后,springboot 就帮我们导入了 web 模块运行的所有组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.3</version>
</dependency>
SpringBoot 将所有的功能场景都抽取出来,做成一个个的 starter(启动器),通常只需要在项目中引入这些 starter ,就可以见将相关的依赖都会导入进来 ,我们要用什么功能就导入什么样的场景启动器即可 ,也可以自己自定义 starter。
3.原理分析(以 spring-boot-starter-web 为例)
- 为什么启动类所在的包及其子孙包能被默认扫描?
- 为什么引入 spring-boot-starter-web 后能直接集成 SpringMVC?
3.1 注解功能划分
SpringBoot 项目的启动入口是@SpringBootApplication
注解标注类中的main()
方法,@SpringBootApplication
能够自动扫描 Spring 组件并自动装配 SpringBoot。
3.2 如何自动扫描(默认扫描启动类所在的包及其子孙包)
找到@Import(AutoConfigurationPackages.Registrar.class)
,进入AutoConfigurationPackages.Registrar
。
在 Registrar
中,找到 register()
方法
3.3 如何加载自动配置类
找到@Import(AutoConfigurationImportSelector.class)
,进入AutoConfigurationImportSelector
。
- 获得候选自动配置类
- 筛选并过滤出当前应用环境所需的自动配置类
- 小结
getCandidateConfiguration(annotationMetadata, attributes)
:从 SpringBoot 提供的自动配置依赖META/spring.factories
文件中获得所有候选自动配置类。
filter(configurations, autoConfigurationMetadata)
:对所有候选自动配置类进行筛选,根据项目 pom.xml 文件中引入的依赖筛选出最终符合当前运行环境的自动配置类。
3.4 启动分析
- 创建
DispatcherServlet
对象
- 注册
DispatcherServlet
3.5 SpringBoot 的 SpringBootApplication 类
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
// ResourceLoader
this.resourceLoader = resourceLoader;
// 资源类
this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
// 依据 classpath 推断容器类型
this.webApplicationType = WebApplicationType.deduceFromClasspath();
// 从 spring.factories 加载所有 BootstrapRegistryInitializer
this.bootstrapRegistryInitializers = getBootstrapRegistryInitializersFromSpringFactories();
// 从 spring.factories 加载所有 ApplicationContextInitializer
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
// 从 spring.factories 加载所有 ApplicationListener
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
// 推断主类
this.mainApplicationClass = deduceMainApplicationClass();
}
SpringApplication.run
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
DefaultBootstrapContext bootstrapContext = createBootstrapContext();
ConfigurableApplicationContext context = null;
configureHeadlessProperty();
// 1,获取SpringApplication初始化的SpringApplicationRunListener监听器并运行
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting(bootstrapContext, this.mainApplicationClass);
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
// 2,项目运行环境Environment的预配置
ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
// 3,项目应用上下文ApplicationContext的预配置
context = createApplicationContext();
context.setApplicationStartup(this.applicationStartup);
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
}
// 4,由项目运行监听器启动配置好应用上下文ApplicationContext
listeners.started(context);
// 5,调用应用上下文中ApplicationContext中配置的程序执行器XxxRunner
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}
try {
// 6,由项目运行监听器持续运行配置好的应用上下文ApplicationContext
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, null);
throw new IllegalStateException(ex);
}
return context;
}