`
m635674608
  • 浏览: 4929224 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

Spring MVC-ContextLoaderListener和DispatcherServlet

 
阅读更多

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
分享到:
评论

相关推荐

    Spring MVC 入门实例

    在 Spring MVC 中, jsp 文件中尽量不要有 Java 代码, 只有 HTML 代码和"迭代(forEach)"与"判断(if)"两个jstl标签. jsp 文件只作为渲染(或称为视图 View)模板使用. 好了, 我们开始吧. 首先我们需要一个放在 WEB-INF...

    gradle-spring-4-mvc-boilerplate

    gradle-spring-4-mvc-样板 如该软件包所指定的那样,DelegatingFilterProxy被认为可与Spring Web MVC一起使用,并且仅与...&lt;listener&gt;org.springframework.web.context.ContextLoaderListener 1在Web xml中定义的S

    springweb3.0MVC注解(附实例)

    -- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 --&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation. AnnotationMethodHandlerAdapter"/&gt; &lt;!-- ③:对模型视图名称的解析,即在模型...

    Spring MVC 框架应用实例

    org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt; &lt;filter-class&gt; org.springframework.web.filter.CharacterEncodingFilter...

    struts2-spring-plugin-2.1.2.jar

    struts2与spring的整合。导入struts2-spring-... spring监听器对应的API类为:org.springframework.web.context.ContextLoaderListener。 struts2-spring-plugin包为我们将struts2的对象工厂设置为spring的IoC容器,

    简单spring MVC 配置

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;servlet-name&gt;test&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet...

    DispatcherServlet 和 ContextLoaderListener 区别

    NULL 博文链接:https://angie.iteye.com/blog/2334955

    Spring的监听器ContextLoaderListener的作用

    Spring的监听器ContextLoaderListener的作用

    spring_MVC源码

    本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,现在这一篇补上。下面开始贴代码。 文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下。 先说...

    spring-web-2.5.jar

    org.springframework.web.context.ContextLoaderListener.class org.springframework.web.context.ContextLoaderServlet.class org.springframework.web.context.ServletConfigAware.class org.springframework.web....

    Spring-5.1.5源码

    Spring framework 5.1.5源码 Main: spring-web/org.springframework.web.context.ContextLoaderListener

    iLink:拉丁云代码测试Spring MVC

    参考(2)Eclipse和IDEA配置Maven2.Spring版本:5.0.73.MVC:Spring MVC配置(1)jar包:spring-webmvc.jar(2)web.xml配置SpringMVC监听类:org.springframework.web.context.ContextLoaderListener(3)SpringMVC核心配置...

    java解决org.springframework.web.context.ContextLoaderListener

    java解决org.springframework.web.context.ContextLoaderListener

    Web项目中使用Spring, 使用 Spring 的器监听器 ContextLoaderListener.docx

    二、 使用 Spring 的器监听器 ContextLoaderListener o1. maven依赖pom.xml o2. 注册监听器 ContextLoaderListener o3. 指定 Spring 配置文件的位置 o4. 获取Spring容器对象 在 Web 项目中使用 Spring 框架,首先...

    spring-simple-web:使用 Spring Framework 的简单 Web (WAR) 项目

    Web 应用程序使用 Spring Web 侦听器初始化,例如web.xml org.springframework.web.context.ContextLoaderListener 。 Spring Web 侦听器使用web.xml的contextConfigLocation上下文参数进行初始化。 此设置的...

    DOS命令使用方法(超全).

    &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;!-- 利用spring监听 编码设置 --&gt; &lt;filter-name&gt;SpringCharacterEncodingFilter&lt;/filter-name&gt; &lt;filter-...

    整合struts2和spring源代码(可以直接在tomcat中运行)

    可以直接运行,并对整合spring和struts2步骤及需要注意的事项进行类总结 整合spring和struts2总结 1.将struts2和spring中的库文件复制到项目下(可以查看WEB-INF\lib目录下的文件) 注意:struts2-spring-...

    spring和hibernate整合

    org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt; ...

    springmybatis

    mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in action之八mybatis 动态sql语句 mybatis实战教程mybatis in ...

    web.xml中ContextLoaderListener的运行过程解析

    web.xml中ContextLoaderListener的运行

Global site tag (gtag.js) - Google Analytics