springboot 的启动流程

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。

转载于:https://www.cnblogs.com/ming-blogs/p/11596870.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值