springboot启动流程

创建环境Environment
加载环境变量、系统变量、启动参数到环境的PropertySource中

org.springframework.core.env.StandardEnvironment#customizePropertySources
org.springframework.boot.SpringApplication#configurePropertySources

执行SpringApplicationRunListener.environmentPrepared,发布环境准备事件,其中有一个ApplicationListener实现类EnvironmentPostProcessorApplicationListener,它调用EnvironmentPostProcessor.postProcessEnvironment,其中有一个实现类ConfigDataEnvironmentPostProcessor负责将配置文件读取到Env的PropertySource

ConfigDataEnvironmentPostProcessor读取配置文件

  1. 获取配置文件路径,取当前Env中spring.config.location、spring.config.import、spring.config.additional-location的值。获取配置文件名称,取spring.config.name的值。获取配置文件后缀,取org.springframework.boot.env.PropertySourceLoader#getFileExtensions的值,默认实现有PropertiesPropertySourceLoader和YamlPropertySourceLoader,因此支持yml,yaml,properties、xml。
  2. 读取配置文件内容,读到PropertySource ,加到Env,org.springframework.boot.env.PropertySourceLoader#load
  3. 确定profiles, spring.profiles.active+spring.profiles.include+spring.profiles.group中生效的,如果为空,使用spring.profiles.default的值
  4. 重复1,2步,读取带profile的配置文件,完成
public interface PropertySourceLoader {
	String[] getFileExtensions();
	List<PropertySource<?>> load(String name, Resource resource) throws IOException;
}

创建Spring容器
org.springframework.boot.ApplicationContextFactory#create,

ConfigurableApplicationContext create(WebApplicationType webApplicationType);

WebApplicationType 根据类路径是否有Servlet和Reactive的类来确定,如果有DispatcherHandler,并且没有DispatcherServlet和org.glassfish.jersey.servlet.ServletContainer,用Reactive,创建AnnotationConfigReactiveWebServerApplicationContext;否则如果有DispatcherServlet,用Servlet,创建AnnotationConfigServletWebServerApplicationContext;如果都没有,为NONE,创建AnnotationConfigApplicationContext

org.springframework.boot.WebApplicationType#deduceFromClasspath

初始化容器
org.springframework.context.ApplicationContextInitializer#initialize

加载容器
加载bean定义,org.springframework.boot.SpringApplication#primarySources和sources声明的类。通过org.springframework.boot.BeanDefinitionLoader#load()加载

//run()参数中的类,是容器配置类
Set<Class<?>> primarySources
//也是容器配置类,后于primarySources加载,并且可以根据类名、包名、spring.xml来加载配置类
Set<String> sources

刷新容器
org.springframework.context.ConfigurableApplicationContext#refresh
创建并执行bean工厂的后置处理器,创建bean的后置处理器,创建并启动web容器,创建非懒加载单例bean,同时执行bean的后置处理器

执行ApplicationRunner和CommandLineRunner
CommandLineRunner 获取到的是原生的、未解析的main方法参数,
ApplicationRunner既能获取原生的,又能获取解析过的main方法参数。
org.springframework.core.env.SimpleCommandLineArgsParser#parse解析

ApplicationRunner和CommandLineRunner在容器而不是spring.factories中定义的,因为此时容器已经刷新完成,执行顺序取决于Order接口和注解

public interface CommandLineRunner {
	void run(String... args) throws Exception;
}
public interface ApplicationRunner {
	void run(ApplicationArguments args) throws Exception;
}
public interface ApplicationArguments {
	//原来的main参数
	String[] getSourceArgs();
//带等号的参数名称
	Set<String> getOptionNames();
	
	boolean containsOption(String name);
	//带等号的参数值
	List<String> getOptionValues(String name);
	//不带等号的参数
	List<String> getNonOptionArgs();
}

springboot生命周期

  1. 应用开始(ApplicationStartingEvent)
  2. 环境准备(ApplicationEnvironmentPreparedEvent):创建环境、并且配置好系统变量,操作系统变量,main方法参数之后
  3. 容器初始化(ApplicationContextInitializedEvent):创建并初始化spring容器之后,ApplicationContextInitializer.initialize
  4. 应用准备(ApplicationPreparedEvent):加载primarySource和source到容器bean定义后
  5. 应用启动完成(ApplicationStartedEvent):容器刷新后
  6. 应用准备完成(ApplicationReadyEvent):CommandLineRunner 和ApplicationRunner执行后
  7. 应用启动失败(ApplicationFailedEvent):抛异常时,但不包括准备完成的异常
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot启动流程可以分为以下几个步骤: 1. 确定应用程序类型。在启动SpringBoot时,首先需要确定应用程序的类型。这可以通过设置启动类的注解来实现,比如使用@SpringBootApplication注解。 2. 创建SpringBoot应用程序上下文。在确定应用程序类型后,SpringBoot会创建一个应用程序上下文(ApplicationContext)对象。这个上下文对象是整个应用程序的核心,包含了所有的配置信息和Bean定义。 3. 加载配置文件。SpringBoot会自动加载并解析应用程序的配置文件,包括application.properties或application.yml等。这些配置文件可以用来配置应用程序的各种属性,如数据库连接、端口号等。 4. 扫描和注册Bean。SpringBoot会扫描应用程序中的所有类,并将符合条件的类注册为Bean。这可以通过@ComponentScan注解来实现,它会扫描指定包及其子包中的所有类。 5. 执行Bean的初始化和依赖注入。在注册Bean后,SpringBoot会执行Bean的初始化操作,并将其依赖的其他Bean注入到其中。这可以通过使用@Autowired注解来实现。 6. 启动应用程序。在完成上述步骤后,SpringBoot启动应用程序。这将导致应用程序开始监听指定的端口,并处理来自客户端的请求。 总而言之,SpringBoot启动流程包括确定应用程序类型、创建应用程序上下文、加载配置文件、扫描和注册Bean、执行Bean的初始化和依赖注入,最后启动应用程序。 <span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [9千字长文带你了解SpringBoot启动过程--史上最详细 SpringBoot启动流程-图文并茂](https://blog.csdn.net/weixin_44947701/article/details/124055713)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值