5)、嵌入式Servlet容器啟動原理说敏;
什么時候創(chuàng)建嵌入式的Servlet容器工廠鸥跟?什么時候獲取嵌入式的Servlet容器并啟動Tomcat;
獲取嵌入式的Servlet容器工廠:
1)、SpringBoot應(yīng)用啟動運(yùn)行run方法
2)医咨、refreshContext(context);SpringBoot刷新IOC容器【創(chuàng)建IOC容器對象枫匾,并初始化容器,創(chuàng)建容器中的每一個組件】拟淮;如果是web應(yīng)用創(chuàng)建AnnotationConfigEmbeddedWebApplicationContext干茉,否則:AnnotationConfigApplicationContext
3)、refresh(context);刷新剛才創(chuàng)建好的ioc容器惩歉;
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();
}
}
}
4)等脂、 onRefresh(); web的ioc容器重寫了onRefresh方法
5)、webioc容器會創(chuàng)建嵌入式的Servlet容器撑蚌;createEmbeddedServletContainer();
6)上遥、獲取嵌入式的Servlet容器工廠:
EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory();
? 從ioc容器中獲取EmbeddedServletContainerFactory 組件;TomcatEmbeddedServletContainerFactory創(chuàng)建對象争涌,后置處理器一看是這個對象粉楚,就獲取所有的定制器來先定制Servlet容器的相關(guān)配置;
7)亮垫、使用容器工廠獲取嵌入式的Servlet容器:this.embeddedServletContainer = containerFactory .getEmbeddedServletContainer(getSelfInitializer());
8)模软、嵌入式的Servlet容器創(chuàng)建對象并啟動Servlet容器;
先啟動嵌入式的Servlet容器饮潦,再將ioc容器中剩下沒有創(chuàng)建出的對象獲取出來燃异;
==IOC容器啟動創(chuàng)建嵌入式的Servlet容器==