【校招VIP】SpringBoot 面试必问:启动流程

10月17日 收藏 0 评论 1 java开发

【校招VIP】SpringBoot 面试必问:启动流程

转载声明:文章来源:https://blog.csdn.net/wu6cfp38/article/details/129731682

一、遇到问题

1.1 说说SpringBoot启动流程吧

参考答案:

首先从main找到run()方法,在执行run()方法之前new一个SpringApplication对象

进入run()方法,创建应用监听器SpringApplicationRunListeners开始监听

然后加载SpringBoot配置环境(ConfigurableEnvironment),然后把配置环境(Environment)加入监听对象中

然后加载应用上下文(ConfigurableApplicationContext),当做run方法的返回对象

最后创建Spring容器,refreshContext(context),实现starter自动化配置和bean的实例化等工作。

1.2 SpringBoot自动装配的原理

参考答案:

通过@EnableAutoConfiguration注解在类路径的META-INF/spring.factories文件中找到所有的对应配置类,然后将这些自动配置类加载到spring容器中。

二、详细探讨

2.1 SpringBoot核心注解:

@SpringBootApplication注解是Spring Boot的核心注解,它其实是一个组合注解,包括三个注解

1、@Configuration: 被标注的类等于在Spring的XML配置文件中(applicationContext.xml),装配所有bean事务,提供了一个Spring的上下文环境

2、@EnableAutoConfiguration: SpringBoot根据应用所声明的依赖来对Spring框架进行自动配置,其中包括两个重要注解:

@AutoConfigurationPackage:该注解上有一个@Import({Registrar.class})注解,其中Registrar类的作用是将启动类所在的包下的所有子包组件扫描注入到spring容器中。

因此这就是为什么将controller、service等包放在启动类的同级目录下的原因

@Import({AutoConfigurationImportSelector.class}):其中AutoConfigurationImportSelector类中有一个getCandidateConfigurations()方法,这个方法通过SpringFactoriesLoader.loadFactoryNames()查找位于META-INF/spring.factories文件中的所有自动配置类并加载这些类。

 3、@ComponentScan: 组件扫描,自动扫描和装配Bean,扫描SpringApplication的run方法中的ExammanagerApplication.class所在的包路径下的文件,因此将启动类(main)放在跟包路径下。它去寻找带有@Component注解的类,并为其创建bean。

2.2详细启动流程(结合源码)

1.首先进入run()方法,run方法中new创建了一个SpringApplication实例

第一个是传的启动类Class,第二是args参数。

2.在SpringApplication的构造方法里为SpringApplication对象赋一些初值。

 3.构造方法执行完后,回到run()方法

public ConfigurableApplicationContext run(String... args) {
long startTime = System.nanoTime();
DefaultBootstrapContext bootstrapContext = this.createBootstrapContext();
ConfigurableApplicationContext context = null;
this.configureHeadlessProperty();
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting(bootstrapContext, this.mainApplicationClass);

try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);
this.configureIgnoreBeanInfo(environment);
Banner printedBanner = this.printBanner(environment);
context = this.createApplicationContext();
context.setApplicationStartup(this.applicationStartup);
this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
this.refreshContext(context);
this.afterRefresh(context, applicationArguments);
Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), timeTakenToStartup);
}

listeners.started(context, timeTakenToStartup);
this.callRunners(context, applicationArguments);
} catch (Throwable var12) {
this.handleRunFailure(context, var12, listeners);
throw new IllegalStateException(var12);
}

try {
Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
listeners.ready(context, timeTakenToReady);
return context;
} catch (Throwable var11) {
this.handleRunFailure(context, var11, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var11);
}
}

该方法中实现了如下几个关键步骤:

创建了应用的监听器SpringApplicationRunListeners并开始监听

加载SpringBoot配置环境(ConfigurableEnvironment),如果是通过web容器发布,会加载StandardEnvironment,其最终也是继承了ConfigurableEnvironment

配置环境(Environment)加入到监听器对象中(SpringApplicationRunListeners)

创建run方法的返回对象:ConfigurableApplicationContext(应用配置上下文)

回到run方法内,prepareContext方法将listeners、environment、applicationArguments、banner等重要组件与上下文对象关联

接下来的refreshContext(context)方法(初始化方法如下)将是实现spring-boot-starter-*(mybatis、redis等)自动化配置的关键,包括spring.factories的加载,bean的实例化等核心工作。

配置结束后,Springboot做了一些基本的收尾工作,返回了应用环境上下文。回顾整体流程,Springboot的启动,主要创建了配置环境(environment)、事件监听(listeners)、应用上下文(applicationContext),并基于以上条件,在容器中开始实例化我们需要的Bean,至此,通过SpringBoot启动的程序已经构造完成。

C 1条回复 评论
Yolk

非常详细, 非常清晰, 代码测试可用。 教科书级别

发表于 2023-11-04 22:00:00
0 0