知其所以然之web.xml

今天有人問我web.xml的配置,早些年看過的配置就用了一次,后來又都用了Java Config,把web.xml都忘了個(gè)干凈,略是尷尬,因此再開個(gè)專題《知其所以然》來細(xì)究一些java雜七雜八的細(xì)節(jié)問題。
今天先來談?wù)剋eb.xml的前身今世。
首先明確一個(gè)概念,web.xml不是必須的用xml形式存在的,一樣可以使用Java Config完成.
當(dāng)時(shí)被人唬住了。web.xml 不是在項(xiàng)目中必須出現(xiàn)的,可以使用JavaConfig 替代。??!在朝花夕拾中之Spring WebMvc 中提到繼承了 AbstractAnnotationConfigDispatcherServletInitializer 的WebMVCInitializer的類,就是web.xml的java config 形式。
web.xml 主要作用是通知 Servlet Containers (Tomcat) 從哪些類加載 & Url映射 & filter是哪些 & 等等。在servlet 3.0之后,@WebServlet, @WebListener 這些annotations 可以做這類的標(biāo)記工作了,Spring 也早就在框架里集成了這些功能

下面放出博主實(shí)現(xiàn)的web.xml的Java Config 版本。

WebMVCInitializer
public class WebMVCInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[]{
            Application.class, MyBatisConfig.class
        };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{
                "/"
        };
    }
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);
        servletContext.addListener(new SessionListener());
    }
}
父類AbstractAnnotationConfigDispatcherServletInitializer
public abstract class AbstractAnnotationConfigDispatcherServletInitializer
        extends AbstractDispatcherServletInitializer {

    /**
     * {@inheritDoc}
     * <p>This implementation creates an {@link AnnotationConfigWebApplicationContext},
     * providing it the annotated classes returned by {@link #getRootConfigClasses()}.
     * Returns {@code null} if {@link #getRootConfigClasses()} returns {@code null}.
     */
    @Override
    protected WebApplicationContext createRootApplicationContext() {
        Class<?>[] configClasses = getRootConfigClasses();
        if (!ObjectUtils.isEmpty(configClasses)) {
            AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext();
            rootAppContext.register(configClasses);
            return rootAppContext;
        }
        else {
            return null;
        }
    }

    /**
     * {@inheritDoc}
     * <p>This implementation creates an {@link AnnotationConfigWebApplicationContext},
     * providing it the annotated classes returned by {@link #getServletConfigClasses()}.
     */
    @Override
    protected WebApplicationContext createServletApplicationContext() {
        AnnotationConfigWebApplicationContext servletAppContext = new AnnotationConfigWebApplicationContext();
        Class<?>[] configClasses = getServletConfigClasses();
        if (!ObjectUtils.isEmpty(configClasses)) {
            servletAppContext.register(configClasses);
        }
        return servletAppContext;
    }

    /**
     * Specify {@link org.springframework.context.annotation.Configuration @Configuration}
     * and/or {@link org.springframework.stereotype.Component @Component} classes to be
     * provided to the {@linkplain #createRootApplicationContext() root application context}.
     * @return the configuration classes for the root application context, or {@code null}
     * if creation and registration of a root context is not desired
     */
    protected abstract Class<?>[] getRootConfigClasses();

    /**
     * Specify {@link org.springframework.context.annotation.Configuration @Configuration}
     * and/or {@link org.springframework.stereotype.Component @Component} classes to be
     * provided to the {@linkplain #createServletApplicationContext() dispatcher servlet
     * application context}.
     * @return the configuration classes for the dispatcher servlet application context or
     * {@code null} if all configuration is specified through root config classes.
     */
    protected abstract Class<?>[] getServletConfigClasses();

}

博主一直都覺得注釋寫的真好,加上函數(shù)名,基本上作什么用,改怎么用都解釋的很清楚了。

最后插一句,xml配置不一定一直好于Java Config。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,535評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,262評論 6 342
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 34,638評論 18 399
  • 一. Java基礎(chǔ)部分.................................................
    wy_sure閱讀 4,010評論 0 11
  • 我看到笑靨如花 我想到打馬天涯 我送走滿眼滿嘴風(fēng)光無華 我想著迎來一樹一程梨花 如花的笑里含著無奈的期盼 天涯的馬...
    小至夏柯閱讀 265評論 0 1

友情鏈接更多精彩內(nèi)容