ServletWebServerApplicationContext -带有*WebxxxApplication的容器

ServletWebServerApplicationContext.java

创建一个容器,在run()方法中被调用。比如调用onRefresh(),把ServletWebServerApplicationContext注册到ServletContext属性中,并调用ServletContextInitializer启动tomcat容器。

protected void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) {...}
beanFactory.addBeanPostProcessor(new WebApplicationContextServletContextAwareProcessor(this));
添加WebApplicationContextServletContextAwareProcessor这个BeanPostProcessor,是为了实现ServletContextAware接口的类能获得ServletContext

protected void onRefresh() {...}
createWebServer()
调用createWebServer(),详解如下

-createWebServer() {...}
getSelfInitializer().onStartup(servletContext);
调用selfInitialize(),把Spring容器(当前实例)注入ServletContext,ServletContextInitializer-》启动容器

-private void selfInitialize(ServletContext servletContext) throws ServletException {...}

prepareWebApplicationContext(servletContext);
// <1> 添加 Spring 容器到 servletContext 属性中。

registerApplicationScope(servletContext);
// <2> 注册 ServletContextScope

WebApplicationContextUtils.registerEnvironmentBeans(getBeanFactory(), servletContext);
// <3> 注册 web-specific environment beans ("contextParameters", "contextAttributes")

for (ServletContextInitializer beans : getServletContextInitializerBeans()) {...}
beans.onStartup(servletContext);
// <4> 获得所有 ServletContextInitializer ,并逐个进行启动

-protected void prepareWebApplicationContext(ServletContext servletContext) {...}
Object rootContext = servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
获取根容器

servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this);
如果rootContext为空,则把容器设置到servletContext中。

-finishRefresh(){...}
super.finishRefresh();

WebServer webServer = startWebServer();
启动WebServer

publishEvent(new ServletWebServerInitializedEvent(webServer, this));
如果WebServer启动成功,则发布ServletWebServerInitializedEvent事件

-private WebServer startWebServer() {...}
webServer.start();
启动webServer

TomcatServer

-public void start() throws WebServerException {...}
启动tomcat容器
Connector connector = this.tomcat.getConnector();
performDeferredLoadOnStartup();

======
带有*WebxxxApplication的容器。会创建创建SpringMVC

ServletWebServerApplicationContext -带有*WebxxxApplication的容器

上一篇:allegro在移动元件或过孔时,相连接的走线都消失了,怎么解决?


下一篇:MyBatis(五)动态SQL 之 内置参数:_parameter&_databaseId