知其所以然之web.xml

作者: Tony_HQ | 来源:发表于2016-10-24 22:00 被阅读0次

今天有人问我web.xml的配置,早些年看过的配置就用了一次,后来又都用了Java Config,把web.xml都忘了个干净,略是尴尬,因此再开个专题《知其所以然》来细究一些java杂七杂八的细节问题。
今天先来谈谈web.xml的前身今世。
首先明确一个概念,web.xml不是必须的用xml形式存在的,一样可以使用Java Config完成.
当时被人唬住了。web.xml 不是在项目中必须出现的,可以使用JavaConfig 替代。!!在朝花夕拾中之Spring WebMvc 中提到继承了 AbstractAnnotationConfigDispatcherServletInitializer 的WebMVCInitializer的类,就是web.xml的java config 形式。
web.xml 主要作用是通知 Servlet Containers (Tomcat) 从哪些类加载 & Url映射 & filter是哪些 & 等等。在servlet 3.0之后,@WebServlet, @WebListener 这些annotations 可以做这类的标记工作了,Spring 也早就在框架里集成了这些功能

下面放出博主实现的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();

}

博主一直都觉得注释写的真好,加上函数名,基本上作什么用,改怎么用都解释的很清楚了。

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

相关文章

  • 2017-10-13

    知其然,知其所以然,知其可为之然。

  • 知其所以然之web.xml

    今天有人问我web.xml的配置,早些年看过的配置就用了一次,后来又都用了Java Config,把web.xml...

  • 经典思考2020-05-19

    本末倒置 知其然,知其所以然,知其不以为之然 如果我们知道我们在做什么,那么这就不叫科学研究了,不是吗?-----...

  • 知其“然”易,知“所以然”难

    选择德泮数学,究竟多久才能让孩子有明显提高呢?这是个我在面对家长提问时最常见的几个问题之一。 这个“多久”,其实很...

  • Retrofit之知其所以然

    知其所以然 知其然 知其所以然 造其然 源码分析首先从入口开始,先来回忆Retrofit的使用步骤: 1.创建Re...

  • JDK源码剖析与最佳实践—ArrayList

    知其然,需知其所以然。——古语 知其所以然,需引而伸之,触类而长之;——虫草 最近准备研究下JDK源码,把常用的一...

  • 庄子  大宗师(一)

    知天之所为,知人之所为者,至矣!知天之所为者,天而生也;知 人之所为者,以其知之所知以养其知之所不知,终其天...

  • 爆款文案

    读完关健明写的《爆款文案》,突然间有了拔开乌云见月明之感,对照微信公众号里的卖货文案,有了“之其然,并知其所以然”...

  • 以医行道,修自己助别人,外面没有别人,遇到的都是自己,疗愈自己=

    以医入道,修己助人。长夏暑湿难耐其中的典型症状我们来启动真我的感觉找到之其然也知其所以然的道理后的疗愈细胞的道路中...

  • 2019-08-12

    知其然知其所以然

网友评论

    本文标题:知其所以然之web.xml

    本文链接:https://www.haomeiwen.com/subject/rcnluttx.html