SpringBoot:深入探索内嵌tomcat启动流程

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.

 

 

 

转载于:https://my.oschina.net/wii01/blog/1511249

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值