一文带你了解SpringBoot的启动原理
作者:算.子 时间:2023-11-28 20:44:42
Spring Boot 的启动原理可以概括为以下几个步骤:
加载 Spring Boot 应用程序的启动类
根据启动类所在的包路径扫描相关的类
根据扫描到的类自动配置 Spring 应用程序
启动嵌入式的 Web 服务器
运行 Spring 应用程序
Spring Boot 启动过程的源码剖析:
@SpringBootApplication // 标记该类为 Spring Boot 应用程序的启动类
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args); // 执行 Spring Boot 应用程序
}
}
在 Spring Boot 应用程序的启动类中,需要使用 @SpringBootApplication 注解标记该类,以告诉 Spring Boot 这是应用程序的启动类。@SpringBootApplication 注解包含了 @Configuration、@EnableAutoConfiguration 和 @ComponentScan 这三个注解,分别表示该类是配置类、开启自动配置和扫描组件。
在 main
方法中,调用 SpringApplication.run
方法来启动 Spring Boot 应用程序。该方法接受两个参数,第一个参数是启动类的类对象,第二个参数是主方法的参数。在 SpringApplication.run
方法中,会执行以下几个步骤:
创建一个 SpringApplication 对象,该对象包含了所有的 Spring Boot 应用程序配置信息。
根据 SpringApplication 对象中的配置信息,创建一个 ApplicationContext 对象,该对象是 Spring 应用程序的上下文。
将所有的 @Configuration 类型的类注册到 ApplicationContext 对象中。
根据 @EnableAutoConfiguration 注解,自动配置 Spring 应用程序。
扫描所有的 @Component 注解的类,并将其注册到 ApplicationContext 中。
启动嵌入式的 Web 服务器。
运行 Spring 应用程序。
下面是 SpringApplication.run
方法的源码注释:
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
return run(new Class<?>[] { primarySource }, args);
}
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
// 创建 SpringApplication 对象,包含了所有的 Spring Boot 应用程序配置信息
SpringApplication application = new SpringApplication(primarySources);
// 运行 Spring Boot 应用程序,并返回上下文对象
return application.run(args);
}
public ConfigurableApplicationContext run(String... args) {
// 创建并启动 ConfigurableApplicationContext 对象,返回该对象
ConfigurableApplicationContext context = createApplicationContext();
// 执行 Spring 应用程序的 *
listeners.starting(this.applicationArguments);
try {
// 准备 ApplicationContext 环境
prepareEnvironment(context, this.environment);
// 配置 ApplicationContext
configureIgnoreBeanInfo(context);
// 执行所有的 ApplicationContextInitializer
applyInitializers(context);
// 执行所有的 SpringApplicationRunListener 的 starting 方法
listeners.contextPrepared(context);
// 打印 Spring Boot 应用程序的 Banner
Banner printedBanner = printBanner();
// 创建 ApplicationContext
context.refresh();
// 将 ApplicationContext 注册到 JVM 关闭钩子中
prepareContext(context, printedBanner);
// 执行所有的 ApplicationContextInitializer 的 postProcessApplicationContext 方法
postProcessApplicationContext(context);
// 执行所有的 SpringApplicationRunListener 的 contextLoaded 方法
listeners.contextLoaded(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}
try {
// 执行所有的 SpringApplicationRunListener 的 started 方法
listeners.started(context);
// 启动嵌入式的 Web 服务器
callRunners(context, this.applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}
try {
// 执行所有的 SpringApplicationRunListener 的 running 方法
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}
return context;
}
在 SpringApplication.run 方法中,还会调用一些其他的方法,如 prepareEnvironment、applyInitializers、postProcessApplicationContext、callRunners 等,这些方法是 Spring Boot 应用程序启动的关键步骤,它们会对应用程序进行配置、初始化、启动等操作。
来源:https://blog.csdn.net/wangshuai6707/article/details/130694688
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
J2EE Servlet上传文件到服务器并相应显示功能的实现代码
![](https://img.aspxhome.com/file/2023/9/96049_0s.png)
Java算法之时间复杂度和空间复杂度的概念和计算
![](https://img.aspxhome.com/file/2023/9/79519_0s.png)
C++多重继承与虚继承分析
![](https://img.aspxhome.com/file/2023/8/117788_0s.png)
spring的jdbctemplate的crud的基类dao
详解Java反射创建对象
![](https://img.aspxhome.com/file/2023/4/78024_0s.png)
java 获取日期的几天前,几个月前和几年前的实例
c#使用filesystemwatcher实时监控文件目录的添加和删除
C# Chart控件标记问题详解
![](https://img.aspxhome.com/file/2023/7/119557_0s.webp)
使用Springboot自定义注解,支持SPEL表达式
![](https://img.aspxhome.com/file/2023/4/99874_0s.png)
Android实现仿通讯录侧边栏滑动SiderBar效果代码
![](https://img.aspxhome.com/file/2023/6/118966_0s.png)
Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)的过程解析
![](https://img.aspxhome.com/file/2023/1/72651_0s.jpg)
java开发分布式服务框架Dubbo原理机制详解
![](https://img.aspxhome.com/file/2023/0/71210_0s.png)
Unity实现仿3D轮转图效果
![](https://img.aspxhome.com/file/2023/7/83247_0s.gif)
Spring Boot与Kotlin 整合全文搜索引擎Elasticsearch的示例代码
C#自定义处理xml数据类实例
将本地jar包安装进入maven仓库(实现方法)
Java常用内置注解用法分析
Maven属性与版本管理详细步骤分解
![](https://img.aspxhome.com/file/2023/1/59531_0s.png)
C#算法之整数反转
Android四大组件之广播BroadcastReceiver详解
![](https://img.aspxhome.com/file/2023/1/139411_0s.png)