深入分析Springboot启动原理的底层源码

本文深入剖析SpringBoot的启动过程,从入口类分析@SpringBootApplication注解,探讨SpringApplication对象实例化,详述run()方法的每一步,包括初始化、设置系统属性、创建Spring环境、启动计时、上下文刷新等关键步骤,揭示Spring Boot应用启动的全貌。
摘要由CSDN通过智能技术生成

文章目录
一、入口类及其源码剖析
二、实例化SpringApplication对象的源码剖析
1. 设置初始化器(Initializer)
2. 设置监听器
3. 推断主应用入口类
三、run() 方法源码剖析
1. 开启计时器
2. 设置系统属性的值
3. 监听器
4. 初始化默认参数
5.创建 Spring 环境
6. 打印器
7. 创建Spring应用上下文
8. 实例化异常报告器
9. Spring上下文前置处理
10. Spring上下文刷新
11. Spring上下文后置处理
12. 停止计时器
13. 发布Spring上下文启动完成事件
14. 执行所有 Runner 运行器
15. 发布Spring上下文就绪事件
一、入口类及其源码剖析
入口类
 

@SpringBootApplication
public class DevServiceApplication {

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

在这里插入图片描述

首先从注解入手,进行分析:

@SpringBootApplication 注解

Spring Boot应用标注在某个类上说明这个类是SpringBoot的主配置类,SpringBoot就应该运行这个类的main方法来启动SpringBoot应用

源码剖析

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
		@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {

 

从源码可以看出,这个注解是@SpringBootConfiguration,@EnableAutoConfiguration以及@ComponentScan这三个注解的组合

① @SpringBootConfiguration

Spring Boot的配置类;标注在某个类上,表示一个类提供了Spring Boot应用程序
 

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {

@Configuration:配置类上来标注这个注解;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {

注意:

配置类相当于配置文件;配置类也是容器中的一个组件,它使用了@Component这个注解。

② @EnableAutoConfiguration

告诉SpringBoot开启自动配置功能,这样自动配置才能生效
借助@import,扫描并实例化满足条件的自动配置的bean,然后加载到IOC容器中
 

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {

	String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

	/**
	 * Exclude specific auto-configuration classes such that they will never be applied.
	 * @return the classes to exclude
	 */
	Class<?>[] exclude() default {};

	/**
	 * Exclude specific auto-configuration class names such that they will never be
	 * applied.
	 * @return the class names to exclude
	 * @since 1.3.0
	 */
	String[] excludeName() default {};

}

@AutoConfigurationPackage:自动配置包
@Import(EnableAutoConfigurationImportSelector.class):给容器中导入组件

在这里插入图片描述

 

使用@EnableAutoConfiguration
这个注解开启自动扫描,然后使用select选择挑选满足条件的文件,并且使用SpringFactoriesLoader进行实例化。最后加载到IOC容器里面,即ApplicationContext中。

③ @ComponentScan

@ComponentScan就是自动扫描并加载符合条件的组件(比如@Component和@Repository等)或者bean定义,最终将这些bean定义加载到IOC容器中去 。

二、实例化SpringApplication对象的源码剖析
源码剖析
 

/**
 * Create a new {@link SpringApplication} instance. The application context will load
 * beans from the specified primary sources (see {@link SpringApplication class-level}
 * documentation for details. The instance can be customized before calling
 * {@link #run(String...)}.
 * @param resourceLoader the resource loader to use
 * @param primarySources the primary bean sources
 * @see #run(Class, String[])
 * @see #setSources(Set)
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
	// 初始化资源加载器
	this.resourceLoader = resourceLoader;
	// 资源加载类不能为 null
	Assert.notNull(primarySources, "PrimarySources must not be null");
	// 初始化加载资源类集合并去重
	this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
	// 推断应用程序是不是web应用
	this.webApplicationType = WebApplicationType.deduceFromClasspath();
	// 设置初始化器(Initializer)
	setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
	// 设置监听器 
	setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
	// 推断出主应用入口类
	this.mainApplicationClass = deduceMainApplicationClass();
}

其中,在推断应用程序是不是web应用的时候调用了dedu

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青藤伽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值