天天看点

springboot 之 tomact 启动

springboot 之 tomact 启动

“先脉络后细节”

我们知道自servlet3.0开始已经支持@WebServlet 用于将一个类声明为 Servlet,该注解将会在部署时被容器处理,容器将根据具体的属性配置将相应的类部署为 Servlet。也就是说我们可以不像以前那样将servlet、listener配置在web.xml中,使用注解的方式提供了非常方便的扩展性。比如我们即将和后续研究的 springboot 启动tomact并加载servlet(DispatcherServlet)

话不多说,直奔主题~

springboot 项目启动后会通过 SpringApplication.run() 方法启动服务,我们从入口进入springboot 的源码。通过run 方法一层一层跟入(

中间废柴代码略~

)在SpringApplication找到了如下代码

  1. 创建SpringApplication对象
public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
		//创建SpringApplication对象,并调用run()
		return new SpringApplication(primarySources).run(args);
	}
           
  1. 自动装载(先不深究自动装载哪些类)

我们看一下通过有参构造创建SpringApplication都做了哪事情:

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
		//设置classloader
		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,而且这个中涉及到了一个重要的类 SpringFactoriesLoader(为了不打断主流程,这个类后续单独介绍)
		setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
		//从META-INF/spring.facotories 中获取 ApplicationContextLinister
		setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
		this.mainApplicationClass = deduceMainApplicationClass();
	}
           
springboot 之 tomact 启动

通过上边的代码我们知道,在创建SpringApplication过程中自动装载了spring.factories 的相关,我们继续完后走

  1. 调用run方法

    在步骤1中创建对象后调用run方法

public ConfigurableApplicationContext run(String... args) {
		//StopWatch 用于启动计时
   	StopWatch stopWatch = new StopWatch();
   	stopWatch.start();
   	ConfigurableApplicationContext context = null;
   	Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
   	configureHeadlessProperty();
   	//获取SpringApplicationRunListeners,从类路径下的META-INF/spring.factories
   	SpringApplicationRunListeners listeners = getRunListeners(args);
   	//回调所有的获取SpringApplicationRunListener.starting()方法
   	listeners.starting();
   	try {
   		//封装命令行参数
   		ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
   		//加载外部化配置的资源到environment,并触发ApplicationEnvironmentPreparedEvent事件
   		ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
   		configureIgnoreBeanInfo(environment);
   		// 打印banner图
   		Banner printedBanner = printBanner(environment);
   		//创建ApplicationContext,并通过webApplicationType 决定返回ConfigurableApplicationContext的具体类型
   		context = createApplicationContext();
   		exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
   				new Class[] { ConfigurableApplicationContext.class }, context);
   		//ConfigurableApplicationContext 配置或处理
   		prepareContext(context, environment, listeners, applicationArguments, printedBanner);
   		//刷新ConfigurableApplicationContext 
   		refreshContext(context);
   		afterRefresh(context, applicationArguments);
   		stopWatch.stop();
   		if (this.logStartupInfo) {
   			new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
   		}
   		//所有的SpringApplicationRunListener回调finished方法
   		listeners.started(context);
   		//处理回调
   		callRunners(context, applicationArguments);
   	}
   	catch (Throwable ex) {
   		handleRunFailure(context, ex, exceptionReporters, listeners);
   		throw new IllegalStateException(ex);
   	}

   	try {
   		listeners.running(context);
   	}
   	catch (Throwable ex) {
   		handleRunFailure(context, ex, exceptionReporters, null);
   		throw new IllegalStateException(ex);
   	}
   	return context;
   }
           
  1. 启动重点获取ConfigurableApplicationContext
protected ConfigurableApplicationContext createApplicationContext() {
   	Class<?> contextClass = this.applicationContextClass;
   	if (contextClass == null) {
   		try {
   			switch (this.webApplicationType) {
   			//web 项目返回 AnnotationConfigServletWebServerApplicationContext
   			case SERVLET:
   				contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
   				break;
   			case REACTIVE:
   				contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
   				break;
   			default:
   				contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
   			}
   		}
   		catch (ClassNotFoundException ex) {
   			throw new IllegalStateException(
   					"Unable create a default ApplicationContext, " + "please specify an ApplicationContextClass",
   					ex);
   		}
   	}
   	//并初始化
   	return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
   }
           

AnnotationConfigServletWebServerApplicationContext 通过这个东东的名字我们大概就能猜出和web容器的加载、启动有关,我们看这哥们究竟是干什么的

  1. create web server & start

    我们看代码发现WebApplicationContextServletContextAwareProcessor 继承自 ServletContextAwareProcessor,我们继续看 ServletContextAwareProcessor 里都有什么

    springboot 之 tomact 启动
    看图中的方法你是不是已经看到些端倪了,我们通过上面的 第4步 已经知道,createApplicationContext 如果是web项目会返回AnnotationConfigServletWebServerApplicationContext,而AnnotationConfigServletWebServerApplicationContex继承自ServletWebServerApplicationContext,在 第3步 中返回AnnotationConfigServletWebServerApplicationContext 后执行了 refreshContext(context);
    springboot 之 tomact 启动
    我们继续看refersh,发现代码
protected void refresh(ApplicationContext applicationContext) {
		Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);
		//ApplicationContxt refresh
		((AbstractApplicationContext) applicationContext).refresh();
	}
           

refresh 方法中的context 即 AnnotationConfigServletWebServerApplicationContext ,所以AnnotationConfigServletWebServerApplicationContext.refresh()即调用了父类的refresh

  1. refresh()
public final void refresh() throws BeansException, IllegalStateException {
		try {
			super.refresh();
		}
		catch (RuntimeException ex) {
			stopAndReleaseWebServer();
			throw ex;
		}
	}
           

我勒个去这是个啥(有点儿绕 =_=!! ~),别急,继续看super.refresh()

springboot 之 tomact 启动

开始有点儿柳暗花明了 _~

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();
			}

			catch (BeansException ex) {
				if (logger.isWarnEnabled()) {
					logger.warn("Exception encountered during context initialization - " +
							"cancelling refresh attempt: " + ex);
				}

				// Destroy already created singletons to avoid dangling resources.
				destroyBeans();

				// Reset 'active' flag.
				cancelRefresh(ex);

				// Propagate exception to caller.
				throw ex;
			}

			finally {
				// Reset common introspection caches in Spring's core, since we
				// might not ever need metadata for singleton beans anymore...
				resetCommonCaches();
			}
		}
	}
           
springboot 之 tomact 启动
springboot 之 tomact 启动

上下两张图有木有~ 有木有~ 如果之前不清楚如何启动的你是否已经开始“哦~”

  1. 获取 TomactServletWebServerFactory 、WebServer

    createWebServer() 方法就是tomact启动的关键入口,这里边有两个比较关键的步骤

    1). getWebServerFactory()

    2). getSelfInitializer().onStartup(servletContext);

    我们先看getWebServerFactory(),在看这个方法之前我们先看一下这个方法的返回值

    ServletWebServerFactory ,看接口的实现你应该大概看到你想要东东了

    springboot 之 tomact 启动
    但Springboot 是如何知道实例化TomactServletWebServerFacotry的呢,我们看一下getWebServerFactory里做了什么
    protected ServletWebServerFactory getWebServerFactory() {
    	// 通过beanFactory 获取ServletWebServerFactory 类型的bean,经过跟代码我们知道,实际上返回的是 tomcatServletWebServerFactory
    	String[] beanNames = getBeanFactory().getBeanNamesForType(ServletWebServerFactory.class);
    	if (beanNames.length == 0) {
    		throw new ApplicationContextException("Unable to start ServletWebServerApplicationContext due to missing "
    				+ "ServletWebServerFactory bean.");
    	}
    	if (beanNames.length > 1) {
    		throw new ApplicationContextException("Unable to start ServletWebServerApplicationContext due to multiple "
    				+ "ServletWebServerFactory beans : " + StringUtils.arrayToCommaDelimitedString(beanNames));
    	}
    	return getBeanFactory().getBean(beanNames[0], ServletWebServerFactory.class);
    }
               
    springboot 之 tomact 启动

    那 TomcatServletWebServerFactory 是如何(何时)被加载的呢?

    根据我们的猜测一般提供多种实现方式,但最终使用哪种实现一般会根据什么congfig或满足什么条件来决定,我们通过查找代码发现了(当然可能有其他捷径) ServletWebServerFactoryConfiguration

    springboot 之 tomact 启动

    不用多说一看就明白~

    获取TomactServlerWebServerFactory后,我们看类的getWebServer方法都做了什么

public WebServer getWebServer(ServletContextInitializer... initializers) {
		//终于。。。终于。。。。
		Tomcat tomcat = new Tomcat();
		File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");
		tomcat.setBaseDir(baseDir.getAbsolutePath());
		Connector connector = new Connector(this.protocol);
		tomcat.getService().addConnector(connector);
		customizeConnector(connector);
		tomcat.setConnector(connector);
		tomcat.getHost().setAutoDeploy(false);
		configureEngine(tomcat.getEngine());
		for (Connector additionalConnector : this.additionalTomcatConnectors) {
			tomcat.getService().addConnector(additionalConnector);
		}
		prepareContext(tomcat.getHost(), initializers);
		//上边对tomact进行的封装,再通过调用getTomcatWebServer返回tomact,我们看一下这个方法
		return getTomcatWebServer(tomcat);
	}
	
	......
	
	protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
		//貌似没什么东西,我们再看一下构造方法里做了什么
		return new TomcatWebServer(tomcat, getPort() >= 0);
	}
	
	......
	
	public TomcatWebServer(Tomcat tomcat, boolean autoStart) {
		Assert.notNull(tomcat, "Tomcat Server must not be null");
		this.tomcat = tomcat;
		this.autoStart = autoStart;
		//重点来了
		initialize();
	}
	private void initialize() throws WebServerException {
		logger.info("Tomcat initialized with port(s): " + getPortsDescription(false));
		synchronized (this.monitor) {
			try {
				addInstanceIdToEngineName();

				Context context = findContext();
				context.addLifecycleListener((event) -> {
					if (context.equals(event.getSource()) && Lifecycle.START_EVENT.equals(event.getType())) {
						// Remove service connectors so that protocol binding doesn't
						// happen when the service is started.
						removeServiceConnectors();
					}
				});
				
				//画星星的地方 !!!!
				// Start the server to trigger initialization listeners
				this.tomcat.start();

				// We can re-throw failure exception directly in the main thread
				rethrowDeferredStartupExceptions();

				try {
					ContextBindings.bindClassLoader(context, context.getNamingToken(), getClass().getClassLoader());
				}
				catch (NamingException ex) {
					// Naming is not enabled. Continue
				}

				// Unlike Jetty, all Tomcat threads are daemon threads. We create a
				// blocking non-daemon to stop immediate shutdown
				startDaemonAwaitThread();
			}
			catch (Exception ex) {
				stopSilently();
				destroySilently();
				throw new WebServerException("Unable to start embedded Tomcat", ex);
			}
		}
	}
           

至此我们已经将tomact的启动流程介绍完了,当然后续还有还有其他的流程:tomact 的启动配置、servlet / filter 加载还有一些启动过程中的事件广播等不在本文中展开长篇大论,本文旨在梳理流程 ”先脉络后细节“,也是为了将自己日常的梳理进行一个记录~

继续阅读