SpringBoot:深入探索内嵌tomcat启动流程
博客专区 > wiitht 的博客 > 博客详情
SpringBoot:深入探索内嵌tomcat启动流程
wiitht 发表于9个月前
SpringBoot:深入探索内嵌tomcat启动流程
  • 发表于 9个月前
  • 阅读 147
  • 收藏 0
  • 点赞 0
  • 评论 0

移动开发云端新模式探索实践 >>>   

1.SpringBoot启动过程中所涉及的类:

springApplication -> run
stopwatch
AnnotationConfigApplicationContext: register, refresh  ->
AbstractApplicationContext -> refresh
AnnotationConfigEmbeddedWebApplicationContext
XmlBeanDefinitionReader
RepositoryConfigurationDelegate 
ClassPathMapperScanner
ConfigurationClassPostProcessor
GenericScope
AutowiredAnnotationBeanPostProcessor
TomcatEmbeddedServletContainer
StandardService
StandardEngine
WebApplicationContext
ContextLoader
ServletRegistrationBean
FilterRegistrationBean
RequestMappingHandlerMapping
EndpointHandlerMapping
SimpleUrlHandlerMapping
RequestMappingHandlerAdapter
ExceptionHandlerExceptionResolver
arterDeprecationWarningAutoConfiguration
URLConfigurationSource
AnnotationMBeanExporter
DefaultLifecycleProcessor
DocumentationPluginsBootstrapper
ApiListingReferenceScanner
CachingOperationNameGenerator
TomcatEmbeddedServletContainer
Application

2.初步分析SpringBoot启动过程

先看看在SpringApplication.run方法做了什么:

public ConfigurableApplicationContext run(String... args) {
		StopWatch stopWatch = new StopWatch();
		stopWatch.start();
		ConfigurableApplicationContext context = null;
		FailureAnalyzers analyzers = null;
		configureHeadlessProperty();
		SpringApplicationRunListeners listeners = getRunListeners(args);
		listeners.started();
		try {
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(
					args);
			ConfigurableEnvironment environment = prepareEnvironment(listeners,
					applicationArguments);
			Banner printedBanner = printBanner(environment);
			context = createApplicationContext();
			analyzers = new FailureAnalyzers(context);
			prepareContext(context, environment, listeners, applicationArguments,
					printedBanner);
			refreshContext(context);
			afterRefresh(context, applicationArguments);
			listeners.finished(context, null);
			stopWatch.stop();
			if (this.logStartupInfo) {
				new StartupInfoLogger(this.mainApplicationClass)
						.logStarted(getApplicationLog(), stopWatch);
			}
			return context;
		}
		catch (Throwable ex) {
			handleRunFailure(context, listeners, analyzers, ex);
			throw new IllegalStateException(ex);
		}
	}

其中refreshContext内部执行了refresh方法

protected void refresh(ApplicationContext applicationContext) {
		Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);
		((AbstractApplicationContext) applicationContext).refresh();
	}

这个方法会刷新整个应用需要启动的东西,具体的在AbstractApplicationContext中refresh方法内部可以看到所有要启动加载的东西:

 public void refresh() throws BeansException, IllegalStateException {
        Object var1 = this.startupShutdownMonitor;
        synchronized(this.startupShutdownMonitor) {
            this.prepareRefresh();
            ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
            this.prepareBeanFactory(beanFactory);

            try {
                this.postProcessBeanFactory(beanFactory);
                this.invokeBeanFactoryPostProcessors(beanFactory);
                this.registerBeanPostProcessors(beanFactory);
                this.initMessageSource();
                this.initApplicationEventMulticaster();
                this.onRefresh();
                this.registerListeners();
                this.finishBeanFactoryInitialization(beanFactory);
                this.finishRefresh();
            } catch (BeansException var9) {
                if(this.logger.isWarnEnabled()) {
                    this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
                }

                this.destroyBeans();
                this.cancelRefresh(var9);
                throw var9;
            } finally {
                this.resetCommonCaches();
            }

        }
    }

在这个过程中会加载相应的xml,启动注册要加载的bean,启动tomcat等等

tomcat启动的过程会异步启动一个守护监听的scoket线程,这个线程通过NIOChannel包装;当第一次收到请求的时候会初始化相关的scoketWrapper数据,并在调用的过程中将socket的数据以http协议的方式转化成HttpRequest对象,并且在过程中会通过wrapper找到相应的dispacthservelt然后调用filter, 具体的controller.

 

 

 

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 2
博文 158
码字总数 113789
×
wiitht
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: