知其所以然之web.xml

今天有人问我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。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,991评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,971评论 6 342
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,769评论 18 399
  • 一. Java基础部分.................................................
    wy_sure阅读 3,839评论 0 11
  • 我看到笑靥如花 我想到打马天涯 我送走满眼满嘴风光无华 我想着迎来一树一程梨花 如花的笑里含着无奈的期盼 天涯的马...
    小至夏柯阅读 226评论 0 1