• 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

  • 相关阅读:
    ECSHOP给分类添加图
    windows2008一键安装环境的配置说明
    在css中定义滚动条样式
    登录不到phpmyadmin
    dedecms程序给栏目增加缩略图的方法
    httpd.conf
    关于 equals() 与 hashCode() 个人理解总结
    postman 安装失败 Failed to install the .NET Framework, try installingthe latest version manully
    docker 私有仓库The push refers to repository [x:5000/test] Get https://x:5000/v2/: dial tcp x:5000: conn
    Redis window 和 Linux 环境下的搭建
  • 原文地址:https://www.cnblogs.com/kltsee/p/15217083.html
Copyright © 2020-2023  润新知