Spring Boot與嵌入式servlet容器

傳統(tǒng)的Spring MVC工程部署時需要將WAR文件放置在servlet容器的文檔目錄內,而Spring Boot工程使用嵌入式servlet容器省去了這一步驟屋摔,本文分析Spring Boot中嵌入式servlet容器的創(chuàng)建和啟動過程薛匪。

刷新應用上下文

Spring Boot的啟動過程一文指出在Spring Boot工程中四敞,Web環(huán)境下默認創(chuàng)建AnnotationConfigEmbeddedWebApplicationContext類型的應用上下文移剪,它的刷新方法定義在它的父類EmbeddedWebApplicationContext中号杏,相關代碼如下:

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

EmbeddedWebApplicationContext類重寫的refresh方法在內部調用了基類AbstractApplicationContext的refresh方法誓斥,其代碼如下所示:

@Override
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();
        }
        // 省略一些代碼
    }
}
  • refresh方法可以看成是模板方法综看,子類可以重寫prepareRefresh、onRefresh和finishRefresh等方法岖食。

EmbeddedWebApplicationContext類重寫的onRefresh和finishRefresh方法如下:

@Override
protected void onRefresh() {
    super.onRefresh();
    try {
        createEmbeddedServletContainer();
    }
    catch (Throwable ex) {
        throw new ApplicationContextException("Unable to start embedded container",
                ex);
    }
}

@Override
protected void finishRefresh() {
    super.finishRefresh();
    EmbeddedServletContainer localContainer = startEmbeddedServletContainer();
    if (localContainer != null) {
        publishEvent(
                new EmbeddedServletContainerInitializedEvent(this, localContainer));
    }
}
  • onRefresh方法首先調用基類的onRefresh方法红碑,然后創(chuàng)建嵌入式servlet容器;
  • finishRefresh方法首先調用基類的finishRefresh方法,然后啟動嵌入式servlet容器析珊。

創(chuàng)建嵌入式servlet容器

EmbeddedWebApplicationContext類的createEmbeddedServletContainer方法創(chuàng)建嵌入式servlet容器羡鸥,代碼如下:

private void createEmbeddedServletContainer() {
    EmbeddedServletContainer localContainer = this.embeddedServletContainer;
    ServletContext localServletContext = getServletContext();
    if (localContainer == null && localServletContext == null) {
        EmbeddedServletContainerFactory containerFactory = getEmbeddedServletContainerFactory();
        this.embeddedServletContainer = containerFactory
                .getEmbeddedServletContainer(getSelfInitializer());
    }
    else if (localServletContext != null) {
        try {
            getSelfInitializer().onStartup(localServletContext);
        }
        catch (ServletException ex) {
            throw new ApplicationContextException("Cannot initialize servlet context",
                    ex);
        }
    }
    initPropertySources();
}
  • 嵌入式servlet容器由EmbeddedServletContainer接口抽象,該接口的實現(xiàn)類有TomcatEmbeddedServletContainer忠寻、JettyEmbeddedServletContainer和UndertowEmbeddedServletContainer惧浴,分別包裝了嵌入式Tomcat、Jetty和Undertow奕剃;
  • EmbeddedServletContainerFactory接口用于實際創(chuàng)建嵌入式servlet容器衷旅,該接口的實現(xiàn)類有TomcatEmbeddedServletContainerFactory、JettyEmbeddedServletContainerFactory和UndertowEmbeddedServletContainerFactory纵朋,分別用于創(chuàng)建上述三種嵌入式servlet容器柿顶;
  • getEmbeddedServletContainerFactory方法從當前應用上下文中取得唯一的EmbeddedServletContainerFactory類型的bean,若有多個則報錯操软。使用默認的自動配置時嘁锯,該bean一定是TomcatEmbeddedServletContainerFactory、JettyEmbeddedServletContainerFactory或者UndertowEmbeddedServletContainerFactory中的一個聂薪。

ServletContextInitializer接口

在上述創(chuàng)建嵌入式servlet容器的過程中家乘,EmbeddedServletContainerFactory接口方法的實參是getSelfInitializer方法的返回值,類型是ServletContextInitializer藏澳。ServletContextInitializer接口用于動態(tài)配置ServletContext仁锯,只有一個回調方法onStartup在容器啟動時被調用。

public interface ServletContextInitializer {

    void onStartup(ServletContext servletContext) throws ServletException;
}

ServletContextInitializer的類層次結構如下圖所示翔悠,可見ServletRegistrationBean和FilterRegistrationBean都實現(xiàn)了該接口扑馁,它們分別向容器添加新的servlet和過濾器。


ServletContextInitializer接口.png

配置ServletContext

getSelfInitializer方法的代碼如下凉驻,只是調用了selfInitialize方法。

private org.springframework.boot.web.servlet.ServletContextInitializer getSelfInitializer() {
    return new ServletContextInitializer() {
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            selfInitialize(servletContext);
        }
    };
}

容器啟動時具體的配置動作由selfInitialize方法完成复罐,其代碼如下:

private void selfInitialize(ServletContext servletContext) throws ServletException {
    prepareEmbeddedWebApplicationContext(servletContext);
    ConfigurableListableBeanFactory beanFactory = getBeanFactory();
    ExistingWebApplicationScopes existingScopes = new ExistingWebApplicationScopes(
            beanFactory);
    WebApplicationContextUtils.registerWebApplicationScopes(beanFactory,
            getServletContext());
    existingScopes.restore();
    WebApplicationContextUtils.registerEnvironmentBeans(beanFactory,
            getServletContext());
    for (ServletContextInitializer beans : getServletContextInitializerBeans()) {
        beans.onStartup(servletContext);
    }
}

該方法主要做了以下工作:

  • prepareEmbeddedWebApplicationContext方法將應用上下文設置到ServletContext的屬性中涝登,過程與Spring MVC的啟動過程一文中分析的ContextLoader初始化根應用上下文非常相似;
    protected void prepareEmbeddedWebApplicationContext(ServletContext servletContext) {
        Object rootContext = servletContext.getAttribute(
                WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        if (rootContext != null) {
            if (rootContext == this) {
                throw new IllegalStateException(
                        "Cannot initialize context because there is already a root application context present - "
                                + "check whether you have multiple ServletContextInitializers!");
            }
            return;
        }
        Log logger = LogFactory.getLog(ContextLoader.class);
        servletContext.log("Initializing Spring embedded WebApplicationContext");
        try {
            servletContext.setAttribute(
                    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this);
            if (logger.isDebugEnabled()) {
                logger.debug(
                        "Published root WebApplicationContext as ServletContext attribute with name ["
                                + WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
                                + "]");
            }
            setServletContext(servletContext);
            if (logger.isInfoEnabled()) {
                long elapsedTime = System.currentTimeMillis() - getStartupDate();
                logger.info("Root WebApplicationContext: initialization completed in "
                        + elapsedTime + " ms");
            }
        }
        catch (RuntimeException ex) {
            logger.error("Context initialization failed", ex);
            servletContext.setAttribute(
                    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
            throw ex;
        }
        catch (Error ex) {
            logger.error("Context initialization failed", ex);
            servletContext.setAttribute(
                    WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ex);
            throw ex;
        }
    }
    
  • 調用其他ServletContextInitializer的回調方法效诅,如ServletRegistrationBean和FilterRegistrationBean分別向容器添加新的servlet和過濾器胀滚。

啟動嵌入式servlet容器

EmbeddedWebApplicationContext類的startEmbeddedServletContainer方法啟動先前創(chuàng)建的嵌入式servlet容器,在內部調用EmbeddedServletContainer的start接口方法:

private EmbeddedServletContainer startEmbeddedServletContainer() {
    EmbeddedServletContainer localContainer = this.embeddedServletContainer;
    if (localContainer != null) {
        localContainer.start();
    }
    return localContainer;
}

嵌入式Tomcat

在Spring Boot中乱投,嵌入式Tomcat由TomcatEmbeddedServletContainer類包裝咽笼,該類的實例創(chuàng)建于TomcatEmbeddedServletContainerFactory。
TomcatEmbeddedServletContainerFactory類實現(xiàn)了EmbeddedServletContainerFactory接口戚炫,實現(xiàn)的接口方法如下:

@Override
public EmbeddedServletContainer getEmbeddedServletContainer(
        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);
    return getTomcatEmbeddedServletContainer(tomcat);
}

protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
        Tomcat tomcat) {
    return new TomcatEmbeddedServletContainer(tomcat, getPort() >= 0);
}
  • 首先新建Tomcat實例剑刑,然后設置工作目錄,最后綁定并自定義Connector、Engine和Context等Tomcat組件施掏,關于這些組件的功能可以參考筆者以前的Tomcat分析系列钮惠;
  • getTomcatEmbeddedServletContainer方法返回包裝有嵌入式Tomcat的TomcatEmbeddedServletContainer實例。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末七芭,一起剝皮案震驚了整個濱河市素挽,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌狸驳,老刑警劉巖预明,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異耙箍,居然都是意外死亡撰糠,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門究西,熙熙樓的掌柜王于貴愁眉苦臉地迎上來窗慎,“玉大人,你說我怎么就攤上這事卤材≌诔猓” “怎么了?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵扇丛,是天一觀的道長术吗。 經常有香客問我,道長帆精,這世上最難降的妖魔是什么较屿? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮卓练,結果婚禮上隘蝎,老公的妹妹穿的比我還像新娘。我一直安慰自己襟企,他們只是感情好嘱么,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著顽悼,像睡著了一般曼振。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上蔚龙,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天冰评,我揣著相機與錄音,去河邊找鬼木羹。 笑死甲雅,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播务荆,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼妆距,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了函匕?” 一聲冷哼從身側響起娱据,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎盅惜,沒想到半個月后中剩,有當地人在樹林里發(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡抒寂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年结啼,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片屈芜。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡郊愧,死狀恐怖,靈堂內的尸體忽然破棺而出井佑,到底是詐尸還是另有隱情属铁,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布躬翁,位于F島的核電站焦蘑,受9級特大地震影響,放射性物質發(fā)生泄漏盒发。R本人自食惡果不足惜例嘱,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望宁舰。 院中可真熱鬧拼卵,春花似錦、人聲如沸蛮艰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽印荔。三九已至,卻和暖如春详羡,著一層夾襖步出監(jiān)牢的瞬間仍律,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工实柠, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留水泉,地道東北人。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓,卻偏偏與公主長得像草则,于是被迫代替她去往敵國和親钢拧。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內容