SpringBoot启动流程

@SpringBootApplication
public class AdminApplication {
    public static void main(String[] args) {
    	// 执行main方法
        SpringApplication.run(AdminApplication.class, args);
    }
}
	public static ConfigurableApplicationContext run(Class<?> primarySource, String... args) {
		// 调用run方法
		return run(new Class<?>[] { primarySource }, args);
	}
	public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
		// new了一个SpringApplication对象并执行它的run方法
		return new SpringApplication(primarySources).run(args);
	}
	public ConfigurableApplicationContext run(String... args) {
		// 一个计时器,用来记录启动时间
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		DefaultBootstrapContext bootstrapContext = createBootstrapContext();
		ConfigurableApplicationContext context = null;
		// 设置环境变量
		configureHeadlessProperty();
		// 开启事件监听
		SpringApplicationRunListeners listeners = getRunListeners(args);
		listeners.starting(bootstrapContext, this.mainApplicationClass);
		try {
			//把参数封装一下
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
			// 准备环境,并且把环境跟Spring上下文绑定
			ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
			configureIgnoreBeanInfo(environment);
			// 打印BANNER
			Banner printedBanner = printBanner(environment);
			// 创建ApplicationContext对象
			context = createApplicationContext();
			context.setApplicationStartup(this.applicationStartup);
			// 准备上下文
			prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
			// 这个方法加载了bean,并且启动了内置的Tomcat容器
			refreshContext(context);
			afterRefresh(context, applicationArguments);
			stopWatch.stop();
			if (this.logStartupInfo) {
				new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
			}
			listeners.started(context);
			callRunners(context, applicationArguments);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, listeners);
			throw new IllegalStateException(ex);
		}

		try {
			listeners.running(context);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, null);
			throw new IllegalStateException(ex);
		}
		return context;
	}

总结:

  1. new了一个SpringApplication对象;
  2. 调用SpringApplication对象的run方法;
  3. 调用createApplicationContext()方法创建上下文对象;
  4. 调用refreshContext(context)方法启动Spring容器和内置的Tomcat容器
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值