2019独角兽企业重金招聘Python工程师标准>>>

Spring MVC-ContextLoaderListener和DispatcherServlet 博客分类: spring java

Tomcat或Jetty作为Servlet容器会为每一个Web应用构建一个ServletContext用于存放所有的Servlet, Filter, Listener。Spring MVC 启动的时候主要涉及到DispatcherServlet 与 ContextLoaderListener。

关于ContextLoaderListener和DispatcherServlet
ContextLoaderListener
  1. ContextLoaderListener 作为一个Listener会首先启动,创建一个WebApplicationContext用于加载除Controller等Web组件以外的所有bean,这个ApplicationContext作为根容器存在,对于整个Web应用来说,只能存在一个,也就是父容器,会被所有子容器共享,子容器可以访问父容器里的bean,反过来则不行。
  2. A. XML配置下会直接创建ContextLoaderListener,然后在contextInitialized方法中初始化WebApplicationContext。
    B. 如果使用的是AnnotationConfig,则通过AnnotationConfigWebApplicationContext获取一个WebApplicationContext之后传给ContextLoadListener。
    之后再contextInitialized方法中调用父类ContextLoader的initWebApplicationContext进行初始化。

    public class ContextLoader {/*** The root WebApplicationContext instance that this loader manages.*/private WebApplicationContext context;public WebApplicationContext initWebApplicationContext(ServletContext servletContext) {this.context = createWebApplicationContext(servletContext);servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);return this.context;}
    }

    可以看到ContextLoader作为ContextLoaderListener的父类在创建了WebApplicationContext之后(针对的是XML配置,使用contextConfigLocation参数指定的配置文件),会通过WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE这个属性将ApplicationContext保存到ServletContext之中,而ContextLoader在创建WebApplicationContext之前也会检查这个属性是否有关联的对象,保证了整个Servletcontext之中只会存在一个根WebApplicationContext。

DispatcherServlet
  1. 在加载了Listener,Filter之后,DispatcherServlet作为ServletContext之中很可能唯一的一个Servlet被初始化,作为整个Web应用的Front Controller进行请求转发。
  2. A. XML配置,生成默认的DispatcherServlet,init时通过init-param中的contextConfigLocation指定的配置文件创建WebApplicationContext。
    B.AnnotationConfig,通过AnnotationConfigWebApplicationContext读取JavaConfig后生成WebApplicationContext,传递给DispatcherServlet进行构造。
  3. DispatcherServlet构造完成之后,调用init方法进行初始化,将DispatcherServlet关联的WebApplicationContext的父容器设为之前ContextLoaderListener创建的WebApplicationContext。
  4. DispatcherServlet关联的WebApplicationContext会在请求到来的时候被设为request的attribute暴露给handlers和之后的web组件。

由此可知,对于一个使用SpringMVC构建的Web应用来说,ContextLoaderListener虽然只能加载一个根容器,但其实是可选的,而DispatcherServlet作为请求转发处理返回结果的核心是比不可少的,而且可以不唯一。
但在Web应用中还是建议使用这种分层的容器结构,更为清晰,也便于之后的扩展。

web.xml与Servlet3.0

传统的web.xml配置适用于Servlet3规范之前的Servlet容器,而Servlet3之后的Servlet容器可以使用注解或是Java代码的方式进行配置。
Servlet3.0环境中,容器会在classpath下寻找ServletContainerInitializer接口的实现类,用于配置Servlet容器。Spring的SpringServletContainerInitializer类就实现了这个接口,并会寻找WebApplicationInitializer接口的实现类完成上面两个组件的加载。

public interface WebApplicationInitializer {/*** Configure the given {@link ServletContext} with any servlets, filters, listeners* context-params and attributes necessary for initializing this web application. See* examples {@linkplain WebApplicationInitializer above}.* @param servletContext the {@code ServletContext} to initialize* @throws ServletException if any call against the given {@code ServletContext}* throws a {@code ServletException}*/void onStartup(ServletContext servletContext) throws ServletException;
}

这个接口的两个主要实现基类为AbstractContextLoaderInitializer与AbstractDispatcherServletInitializer,分别负责将ContextLoaderListener与DispatcherServlet加载到ServletContext之中,但是这两个类分别获取WebApplicationContext的函数并没有实现,如果如需要,你可以通过自己实现通过XML获取WebApplicationContext,而Spring提供了一个AbstractAnnotationConfigDispatcherServletInitializer供你来继承,如名可知,是使用JavaConfig来加载WebApplicationContext的。
当然你也可以自己实现WebApplicationInitializer手动将Servlet和Listener这些加载如ServletContext。
强调:Servlet3.0容器会自己检查classpath下实现了ServletContainerInitializer的类,使用这个接口的onStartup函数进行ServletContext的初始化。如果不适用这个,那么就用传统的web.xml进行ContextLoaderListener与DispatcherServlet的装配。

强调

对于DispatcherServlet与ContextLoaderListener的配置分为web.xml与Servlet容器发现两种,而对于这两个组件的WebApplicationContext的配置也分为XML方式和JavaConfig方式。

  1. web.xml + application.xml =

    <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param><param-name>contextConfigLocation</param-name><param-value>classpath*:config/spring-context.xml</param-value>
    </context-param>
    //<br/>
    <servlet><servlet-name>ebook-manage</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/spring-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup><async-supported>true</async-supported>
    </servlet>
    <servlet-mapping><servlet-name>ebook-manage</servlet-name><url-pattern>/</url-pattern>
    </servlet-mapping>
  2. web.xml + JavaConfig =

    <context-param>
    <param-name>contextClass</param-name>
    <param-value>
    org.springframework.web.context.support.
    ➥ AnnotationConfigWebApplicationContext
    </param-value>
    </context-param>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>com.habuma.spitter.config.RootConfig</param-value>
    </context-param>
    <listener>
    <listener-class>
    org.springframework.web.context.ContextLoaderListener
    </listener-class>
    </listener>
    <servlet>
    <servlet-name>appServlet</servlet-name>
    <servlet-class>
    org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
    <param-name>contextClass</param-name>
    <param-value>
    org.springframework.web.context.support.
    ➥ AnnotationConfigWebApplicationContext
    </param-value>
    </init-param>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    com.habuma.spitter.config.WebConfigConfig
    </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>appServlet</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
  3. ServletContainerInitializer(WebApplicationInitializer) + JavaConfig =
    public class SpittrWebAppInitializer
    extends AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    protected String[] getServletMappings() {
    return new String[] { "/" };
    }
    @Override
    protected Class<?>[] getRootConfigClasses() {
    return new Class<?>[] { RootConfig.class };
    }
    @Override
    protected Class<?>[] getServletConfigClasses() {
    return new Class<?>[] { WebConfig.class };
    }
    }
  4. ServletContainerInitializer(WebApplicationInitializer) + application.xml =
    需要继承AbstractDispatcherServletInitializer然后自己实现通过application.xml加载ApplicationContext的函数。
http://www.jianshu.com/p/9d77e49852c6

转载于:https://my.oschina.net/xiaominmin/blog/1598439

Spring MVC-ContextLoaderListener和DispatcherServlet相关推荐

  1. Spring mvc ContextLoaderListener 原理解析

    对于熟悉Spring MVC功能,首先应从web.xml 开始,在web.xml 文件中我们需要配置一个监听器 ContextLoaderListener,如下. <!-- 加载spring上下 ...

  2. spring mvc DispatcherServlet详解之前传---前端控制器架构

    前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响应发送到客户端.前端控制器既可以使用Filter实现 ...

  3. Spring MVC的DispatcherServlet – Java开发人员应该知道的10件事

    如果您使用过Spring MVC,那么您应该知道什么是DispatcherServlet? 它实际上是Spring MVC的心脏,确切地说是MVC设计模式或控制器的C语言. 应该由Spring MVC ...

  4. Spring MVC 原理探秘 - 一个请求的旅行过程

    1.简介 在前面的文章中,我较为详细的分析了 Spring IOC 和 AOP 部分的源码,并写成了文章.为了让我的 Spring 源码分析系列文章更为丰富一些,所以从本篇文章开始,我将来向大家介绍一 ...

  5. 使用Spring MVC 4构建Restful服务

    使用Spring MVC 4构建RESTful服务相对于其它框架来说,有很多优势.首先,Spring MVC 4作为Spring的框架之一,可以很好地与Spring进行集成.其次,Spring MVC ...

  6. Spring MVC-使用Spring Tool Suite IDE搭建Spring MVC开发环境

    Spring MVC 概述 新建Spring MVC Project 分析IDE建立的工程 Maven dependencies configuration Spring MVC configurat ...

  7. IDEA中Spring MVC实现图片上传并显示

    我们都知道web项目需要部署到tomcat服务器中运行 那么,我们又是如何通过tomcat来访问存放在本地磁盘中的图片呢?,通过tomcat访问本地图片,需要配置虚拟路径,下面介绍两种配置虚拟路径的方 ...

  8. 第7章 使用Spring MVC构建Web程序(一)

    7.1 Spring MVC起步 7.1.1 跟踪Spring MVC的请求 在spring MVC中,DispatcherServlet是前端控制器,客户端的请求以及各种请求(处理器映射器,处理器适 ...

  9. java架构-Spring MVC 与 Servlet

    相信大家都能够在上网上看到Spring MVC的核心类其实就是DispatherServlet,也就是Spring MVC处理请求的核心分发器.其实核心分发器几乎是所有MVC框架设计中的核心概念,像在 ...

  10. 基于Spring + Spring MVC + Mybatis 高性能web构建

    一直想写这篇文章,前段时间 痴迷于JavaScript.NodeJs.AngularJS,做了大量的研究,对前后端交互有了更深层次的认识. 今天抽个时间写这篇文章,我有预感,这将是一篇很详细的文章,详 ...

最新文章

  1. Java 使用ZeroMQ 2.2 进行通信编程
  2. ECCV 2020《Linguistic Structure Guided Context Modeling for Referring Image Segmentation》论文笔记
  3. linux内外部命令,Shell、内外部命令――Linux基本命令(2)
  4. Reason: image not found
  5. WINCC冗余、上位机冗余详细教程
  6. 银盛支付银账通进件接口php demo 签名 上传图片 进件 获取token 超简洁sdk
  7. 分步图解分析排序方法-冒泡排序
  8. 给未来的电子工程师nbsp;---电子牛人给…
  9. 人工智能服务器中涉及到哪些技术
  10. People Counter - People Counting
  11. rust-crate
  12. includes() 方法
  13. JavaScript数字转字符串,字符串转数字
  14. 电脑屏幕仅计算机,一台主机有两个显示屏,计算机只能检测到一个,而另一台则显示黑屏...
  15. 机器人 homework2
  16. 神童频现,这到底是人性的扭曲还是?
  17. 一招教你怎么注册/登陆芒果xo悠闲社区网
  18. zoj3344 第一类斯特林数+java大数
  19. mysql tmp目录权限_MySQL因/tmp目录读写权限启动失败
  20. 幼儿园计算机教育培训,幼儿园教育教师计算机学习培训计划.doc

热门文章

  1. windows7下,Java中利用JNI调用c++生成的动态库的使用步骤
  2. 【FFmpeg】降低转码延迟方法、打印信息详解、refcounted_frames详解
  3. python中str的index什么意思_python中index的用法是什么
  4. echarts datazoom 显示的位置设置
  5. Mybatis 获取当前序列和下一个序列值 以及在一个方法中写多条SQL 语句
  6. 第一天写,希望能坚持下去。
  7. 通过分离dataSource 让我们的code具有更高的复用性.
  8. React路由 react-router-dom
  9. if else 你以为你把它吃透了吗?我让你惊讶一下
  10. Linux上重启服务的正确命令