springboot 的启动流程
2024-10-05 01:36:12
1.我们springboot 项目的启动类如下。
方式1
@SpringBootApplication
public class SpringbootZkLockApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootZkLockApplication.class, args);
}
}
点击 run 方法源码进入如下,
/**
* Static helper that can be used to run a {@link SpringApplication} from the
* specified source using default settings.
* @param primarySource the primary source to load
* @param args the application arguments (usually passed from a Java main method)
* @return the running {@link ApplicationContext}
*/
public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
return run(new Class<?>[] { primarySource }, args);
}
继续 点击 run 方法进入源码,
/**
* Static helper that can be used to run a {@link SpringApplication} from the
* specified sources using default settings and user supplied arguments.
* @param primarySources the primary sources to load
* @param args the application arguments (usually passed from a Java main method)
* @return the running {@link ApplicationContext}
*/
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
return new SpringApplication(primarySources).run(args);
}
我们看到 run() 方法的返回值为 ConfigurableApplicationContext。
看到这里我们就知道了SpringBoot 启动类的底层 是 new SpringApplication(primarySources).run(args);
这时候我们的启动类可以可以换成 如下方式启动。
方式2
@SpringBootApplication
public class SpringbootZkLockApplication { public static void main(String[] args) {
//SpringApplication.run(SpringbootZkLockApplication.class, args);
new SpringApplication(SpringbootZkLockApplication.class).run(args);
}
new SpringApplication(SpringbootZkLockApplication.class).run(args); 可以拆分为2个部分启动
方式3
@SpringBootApplication
public class SpringbootZkLockApplication { public static void main(String[] args) {
//SpringApplication.run(SpringbootZkLockApplication.class, args);
SpringApplication application = new SpringApplication(SpringbootZkLockApplication.class);
application.run(args);
}
}
上面这三种启动方式都是等价的,都可以启动 SpringBoot 项目。只不过是第一种方式是我们常用的,经过SpringBoot 封装过的启动方式。
@EnableAutoConfiguration 注解 加载了我们第三方配置的信息进行 Tomcat 启动。
DispatcherServletAutoConfiguration ---》ServletWebServerFactoryAutoConfiguration 创建 tomcat。
最新文章
- Spring基础知识
- DIV+CSS布局网站基本框架
- Java语言的编写规范
- UITextField属性
- Css:背景色透明,内容不透明之终极方法!兼容所有浏览器
- HTML to DOM
- MapReduce中使用SequenceFile的方式上传文件到集群中
- 如何嗅闻交换网络和ARP骗子-ARP解释的原则
- NSIndexSet 浅析
- c# ProxyServer 代理服务器 不是很稳定
- 【python】函数说明文档
- 0 - Dao层(数据访问层设计)
- git步骤
- MySql 5.7.20版本免安装版配置过程
- txt2xls
- ComponetOne 2014 v3版本正式发布
- 深入详解美团点评CAT跨语言服务监控(八)报表持久化
- GoLang学习之数据类型
- linux内核分析第一次实验
- 【js】用正则表达式对文字进行局部替换
热门文章
- JS数组常见方法的深浅拷贝分类
- Xcode: Xcode中Command Line Tools的安装方法
- MySQL not equal to operator <;>; &;&; !=
- autoComplete TextView
- 关于TCP粘包和拆包的终极解答
- 003-结构型-02-装饰模式(Decorator)
- DELPHI 数据库操作类(工具类)
- IBM System x3650 M3_RAID服务器进入阵列卡配置界面(webBIOS)
- [转]Ubuntu 上创建常用磁盘阵列
- Difference between Process and thread?