通过阅读源码看看springboot的启动流程 是怎么调用getWebServer这个方法的

1.启动类调用了run方法点进去
在这里插入图片描述
2.返回一个run继续点进去
在这里插入图片描述
3.创建SpringApplication
返回了两部分 第一部分 new SpringApplication第二部分是调用run 方法先看看第一部分
在这里插入图片描述
4.调了个this点进去看看
在这里插入图片描述
5.其实SpringBoot启动就着两个步骤,先创建ConfigurableApplicationContext ,然后再调用Run方法。

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		this.resourceLoader = resourceLoader;
		Assert.notNull(primarySources, "PrimarySources must not be null");
		//保存主类
		this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
		//判断当前是什么类型项目
		this.webApplicationType = WebApplicationType.deduceFromClasspath();
	//从类路径下找到META-INF/spring.factories配置的所有ApplicationContextInitializer
		setInitializers((Collection) getSpringFactoriesInstances(
				ApplicationContextInitializer.class));
	//从类路径下找到META-INF/spring.factories配置的所有ApplicationListener
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		this.mainApplicationClass = deduceMainApplicationClass();

6.返回去看看run方法
在这里插入图片描述
7.运行spring应用程序创建并刷新一个新的

/**
	 * Run the Spring application, creating and refreshing a new
	 * {@link ApplicationContext}.
	 * @param args the application arguments (usually passed from a Java main method)
	 * @return a running {@link ApplicationContext}
	 */
	public ConfigurableApplicationContext run(String... args) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
		configureHeadlessProperty();
		//从类路径下META‐INF/spring.factories,取得SpringApplicationRunListeners;
		SpringApplicationRunListeners listeners = getRunListeners(args);
		//回调所有的获取SpringApplicationRunListener.starting()方法
		listeners.starting();
			try {
		    //封装命令行参数
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(
					args);
		    //准备环境
			ConfigurableEnvironment environment = prepareEnvironment(listeners,
					applicationArguments);
			configureIgnoreBeanInfo(environment);
            //创回调SpringApplicationRunListener.environmentPrepared();
//表示环境准备完成

			//打印Banner 
			Banner printedBanner = printBanner(environment);
            //根据环境创建context
			context = createApplicationContext();
		    //错误的异常报表
			exceptionReporters = getSpringFactoriesInstances(
					SpringBootExceptionReporter.class,
					new Class[] { ConfigurableApplicationContext.class }, context);
			 //准备上下文环境;
			//将environment保存到ioc中;
			//applyInitializers()调用所有的ApplicationContextInitializer的initialize方法
			//调用所有的SpringApplicationRunListener的contextPrepared();
			prepareContext(context, environment, listeners, applicationArguments,
					printedBanner);
			//SpringApplicationRunListener的contextLoaded
			//刷新容器
			//扫描,创建,加载所有组件;
			refreshContext(context);
			afterRefresh(context, applicationArguments);
			stopWatch.stop();
			if (this.logStartupInfo) {
				new StartupInfoLogger(this.mainApplicationClass)
						.logStarted(getApplicationLog(), stopWatch);
			}
			//所有的SpringApplicationRunListener回调started方法
			listeners.started(context);
			//获取所有的ApplicationRunner和CommandLineRunner进行调用
			callRunners(context, applicationArguments);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, exceptionReporters, listeners);
			throw new IllegalStateException(ex);
		}
		try {
			//所有的SpringApplicationRunListener的running();
			listeners.running(context);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, exceptionReporters, null);
			throw new IllegalStateException(ex);
		}
		return context;
	}

8.点进去这个refreshContext方法看看有个refresh(context);刷新上下文继续点进去

在这里插入图片描述
9.应用上下文刷新继续点进去
在这里插入图片描述
10.可以看到这是spring创建IOC容器的时候执行的代码 找到onRefresh();方法点进去

	@Override
	public void refresh() throws BeansException, IllegalStateException {
		synchronized (this.startupShutdownMonitor) {
			// Prepare this context for refreshing.
			prepareRefresh();

			// Tell the subclass to refresh the internal bean factory.
			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

			// Prepare the bean factory for use in this context.
			prepareBeanFactory(beanFactory);

			try {
				// Allows post-processing of the bean factory in context subclasses.
				postProcessBeanFactory(beanFactory);

				// Invoke factory processors registered as beans in the context.
				invokeBeanFactoryPostProcessors(beanFactory);

				// Register bean processors that intercept bean creation.
				registerBeanPostProcessors(beanFactory);

				// Initialize message source for this context.
				initMessageSource();

				// Initialize event multicaster for this context.
				initApplicationEventMulticaster();

				// Initialize other special beans in specific context subclasses.
				onRefresh();

				// Check for listener beans and register them.
				registerListeners();

				// Instantiate all remaining (non-lazy-init) singletons.
				finishBeanFactoryInitialization(beanFactory);

				// Last step: publish corresponding event.
				finishRefresh();
			}

11.可以看到这个抽象类里的这个方法 点旁边蓝色小圆圈可以看到继承它的子类
ServletWebServerApplicationContext点进去
在这里插入图片描述
在这里插入图片描述
12.调用了createWebServer();创建web服务器 点进去
在这里插入图片描述
13.可以看到在这里调用了getWebServer()这个方法 然后启动tomcat
在这里插入图片描述
14.看看实现这个接口的类 点进去Tomcat那个
在这里插入图片描述
在这里插入图片描述
15.调用了getWebServer()这个方法
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值